gpt4 book ai didi

java - TestNG异常: Cannot Instantiate class

转载 作者:行者123 更新时间:2023-12-02 11:58:47 29 4
gpt4 key购买 nike

我收到 java 空指针异常。我无法解决它。我已在 testbase 类中初始化驱动程序,并希望在我的 Testing_TVO 类中使用相同的驱动程序

这是我的测试基类

public class testbase {

public static WebDriver driver;
String baseURL = "https://mpower.tvo.org/educators/#/login/";
String browser = "firefox";


public void init()
{
selectBrowser(browser);
getUrl(baseURL);
}

public void selectBrowser(String browser)
{
if(browser.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe ");
driver = new FirefoxDriver();
}
if (browser.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver", "H:\\geckodriver-v0.17.0-win64\\chromedriver.exe");
driver = new ChromeDriver();
}
if(browser.equalsIgnoreCase("edge"))
{
System.setProperty("webdriver.edge.driver", "H:\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}

}

public void getUrl(String url)
{
driver.get(url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

这是我的测试类,我想在其中初始化 Web 元素并编写脚本

public class Testing_TVO_Website extends testbase {


@BeforeClass
public void beforeClass() {
init();

}

WebElement register_btn = driver.findElement(By.xpath(".//*[@id='loginPageRegister1']"));
WebElement firstName_tbx = driver.findElement(By.xpath(".//*[@id='firstName']"));
WebElement lastName_tbx = driver.findElement(By.xpath(".//*[@id='lastName']"));

@FindBy(xpath = ".//*[@id='educatorRoleId']")
WebElement role;
Select role_sel = new Select(role);

@FindBy(xpath = ".//*[@id='boardId']")
WebElement board;
Select board_sel = new Select(board);

@FindBy(xpath = ".//*[@id='schoolId']")
WebElement school;
Select school_sel = new Select(school);


WebElement email_tbx = driver.findElement(By.xpath(".//*[@id='email']"));
WebElement emailConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userEmailConfirm']"));
WebElement password_tbx = driver.findElement(By.xpath(".//*[@id='userPassword']"));
WebElement passwordConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userPasswordConfirm']"));

@FindBy(xpath = ".//*[@id='source']")
WebElement source;
Select source_sel = new Select(source);

WebElement agreement_chbx = driver.findElement(By.xpath(".//*[@id='agreement']"));
WebElement registerSubmit_btn = driver.findElement(By.xpath(".//*[@id='registerSubmit']"));



@Test
public void TC_001_Verifying_Registeration() {
register_btn.click();
firstName_tbx.sendKeys("Shivam");
lastName_tbx.sendKeys("Patel");
role_sel.selectByIndex(1);
board_sel.selectByValue("Algoma DSB");
school_sel.selectByVisibleText("Blind River");

email_tbx.sendKeys("patelshivam3033@adsb.on.ca");
emailConfirm_tbx.sendKeys("patelshivam3033@adsb.on.ca");
password_tbx.sendKeys("shivampatel");
passwordConfirm_tbx.sendKeys("shivampatel");

source_sel.selectByIndex(2);
agreement_chbx.click();
registerSubmit_btn.click();

assertEquals(driver.findElement(By.xpath(".//*[@id='app-content']/div[3]/div/div/div[2]/h2")), "Thank you for registering!");




}

但是我遇到了 java 空指针异常。这是日志

  org.testng.TestNGException: 
Cannot instantiate class com.test.TVO_Assignment.Testing_TVO_Website
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191)
at org.testng.TestClass.getInstances(TestClass.java:99)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85)
at org.testng.TestClass.init(TestClass.java:77)
at org.testng.TestClass.<init>(TestClass.java:42)
at org.testng.TestRunner.initMethods(TestRunner.java:423)
at org.testng.TestRunner.init(TestRunner.java:250)
at org.testng.TestRunner.init(TestRunner.java:220)
at org.testng.TestRunner.<init>(TestRunner.java:169)
at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28)
at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616)
at org.testng.SuiteRunner.init(SuiteRunner.java:185)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:131)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1383)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1363)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1217)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
... 25 more
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
at com.test.TVO_Assignment.Testing_TVO_Website.<init>(Testing_TVO_Website.java:34)
... 30 more

最佳答案

根据您的日志,错误不是空指针异常,实际错误是 IllegalStateException,因为未设置 gecko 驱动程序。这里的问题是,壁虎驱动程序路径的末端有额外的空间。可能是拼写错误。

System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe ");

请删除上一行末尾的空格。它解决了你的问题。

此外,您的代码中存在问题,例如您无法将页面工厂代码和测试代码组合在单个类中,使其分开。

关于java - TestNG异常: Cannot Instantiate class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47409183/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com