Java 无法实例化类错误 - Selenium WebDriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32525776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot Instantiate class error - Selenium WebDriver
提问by Uziii
I am facing this 'Cannot insantiate class' error on running one of my test cases in selenium webdriver using java.
我在使用 java 在 selenium webdriver 中运行我的测试用例之一时遇到了这个“无法实例化类”错误。
Below is the class of the functionality of the test,
以下是测试功能的类,
package Pages;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import Lib.lib;
public class KnowledgeBase extends lib{
By sortBylink = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/div");
By featuredOption = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]");
By mostRcnt = By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[2]");
String featOption = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]")).getText();
String mostRecent = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[2]")).getText();
public void initSBy() throws Exception
{
driver.findElement(sortBylink).click();
Thread.sleep(1500);
}
public void selectfO() throws Exception
{
driver.findElement(featuredOption).click();
Thread.sleep(5000);
}
public void selectMr() throws Exception
{
driver.findElement(mostRcnt).click();
Thread.sleep(5000);
}
public void sortBy(String sProp) throws Exception
{
this.initSBy();
if (sProp == featOption) {
this.selectfO();
}
else if (sProp == mostRecent){
this.selectMr();
}
else {
System.out.println("Incorrect option. Test failed.");
}
}
}
Below is my Test Case Class
下面是我的测试用例类
package TestCases;
import org.testng.annotations.Test;
import Lib.lib;
import Pages.KnowledgeBase;
import Pages.LoginPage;
public class sortingTextKB extends lib {
LoginPage uLogin = new LoginPage();
KnowledgeBase sortObj = new KnowledgeBase();
//Logging In
@Test (priority = 1)
public void loggingIn() throws Exception
{
uLogin.loginToKB("[email protected]", "uziiiii");
}
//Sorting
@Test (priority = 2)
public void sortIn() throws Exception
{
sortObj.sortBy("Most Recent");
}
}
Below is my Lib class, that contains the chrome driver configuration
下面是我的 Lib 类,其中包含 chrome 驱动程序配置
package Lib;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class lib {
protected static WebDriver driver = null;
@BeforeTest
public void chrome_extension()
{
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("http://www.testsite.com");
}
@AfterTest
public void quit()
{
driver.quit();
}
}
When I run my test case class, I am getting the following error,
当我运行我的测试用例类时,我收到以下错误,
org.testng.TestNGException:
Cannot instantiate class TestCases.sortingTextKB
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.NullPointerException
at Pages.KnowledgeBase.<init>(KnowledgeBase.java:22)
at TestCases.sortingTextKB.<init>(sortingTextKB.java:12)
... 26 more
Following is the 22 line of KnowledgeBase class,
以下是知识库类的 22 行,
String featOption = driver.findElement(By.xpath("html/body/div[1]/featured-studies-grid/div[2]/featured-studies-toolbar/div/div[2]/div[2]/div/ul/li[1]")).getText();
Please let me know why I am facing this 'cannot instantiate class' error. Thanks
请让我知道为什么我面临这个“无法实例化类”错误。谢谢
回答by nater
You should try instantiating your object. This should remove the error being presented.
您应该尝试实例化您的对象。这应该消除出现的错误。
回答by Hari
Change the code to like this , may be it works.
把代码改成这样,可能就行了。
@Test (priority = 2)
public void sortIn() throws Exception
{ `enter code here
sortObj = new KnowledgeBase();
sortObj.sortBy("Most Recent");
}
回答by Atul Chavan
Try something like below. i created the WebDriver object outside @Test annotation and instantiated it in @Test as.
尝试类似下面的内容。我在 @Test 注释之外创建了 WebDriver 对象,并在 @Test 中将其实例化为。
public class TestNGClass
{
WebDriver driver;
public String baseUrl = "http://newtours.demoaut.com/";
@Test
public void verifyHomepageTitle()
{
driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.quit();
}
}
回答by Don Lee
You can download "chromedriver.exe" then add this to your project. You no need to use this System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
. You can you ChromeDriver driver = new ChromeDriver();
If still get error, please provide permission for the file "chromedrive.exe".
您可以下载“chromedriver.exe”,然后将其添加到您的项目中。你不需要使用这个System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
。你可以你ChromeDriver driver = new ChromeDriver();
如果仍然出现错误,请提供该文件“chromedrive.exe”权限。