gpt4 book ai didi

java - Selenium : Java Null pointer exception

转载 作者:行者123 更新时间:2023-11-29 04:25:04 24 4
gpt4 key购买 nike

我有下面的 selenium 结构,在浏览器启动和最大化后我遇到错误。当@Test 方法开始执行时,出现 Java 空指针异常。

请提出解决方案

基类:

public class BaseClass

{


public static WebDriver driver ;

public BaseClass(WebDriver _driver)
{
driver = _driver;
}

public WebElement FirstName = driver.findElement(By.name("firstname"));
public WebElement LastName = driver.findElement(By.name("LastName"));




public String GetFirstName()
{
return FirstName.getText();

}

public String GetLastName()
{
return LastName.getText();
}


public WebDriver SetFirstname(String FirstName)
{
this.FirstName.sendKeys(FirstName);
return driver;
}

public WebDriver SetLastname(String LastName)
{
this.LastName.sendKeys(LastName);
return driver;
}

}

测试类:

public class TestClassTest  

{

public static WebDriver driver =null;
private String Url ="";
private String DriverOption="";
private String DriverPath="";

private String Firstname= "";
private String LastName= "";



@BeforeSuite(alwaysRun = true)
public void setupBeforeSuite(ITestContext context) throws IOException
{

Url = context.getCurrentXmlTest().getParameter("URL");

DriverOption = context.getCurrentXmlTest().getParameter("DriverOptions");

DriverPath = context.getCurrentXmlTest().getParameter("DriverPath");


Firstname= ReadValue_XL(driver,0,0);

LastName= ReadValue_XL(driver,0,1);

}

@Parameters("Browser")
@BeforeTest
public void SetUp(String Browser)
{
if(Browser.equalsIgnoreCase("chrome"))
{
System.setProperty(DriverOption,DriverPath);

driver = new ChromeDriver();
}

if(Browser.equalsIgnoreCase("firefox"))
{
System.setProperty(DriverOption, DriverPath);

driver = new FirefoxDriver();
}

driver.get(Url);

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

driver.manage().window().maximize();



}


@Test (priority = 1)
public void TestOne()
{
BaseClass Base= new BaseClass(driver);

Base.SetFirstname(Firstname);
Base.SetLastname(LastName);

Assert.assertEquals(Base.GetFirstName(), "Dog Food");


}



@AfterTest
public void Driver_close()
{
driver.close();
driver.quit();
}


public String ReadValue_XL(WebDriver driver,int Row,int Column) throws IOException
{
XSSFWorkbook WrkBk = new XSSFWorkbook(".\\Input\\Input.xlsx");

XSSFSheet sourceSheet = WrkBk.getSheet("Sheet1");

XSSFRow sourceRow = sourceSheet.getRow(Row);

XSSFCell cell1=sourceRow.getCell(Column);

cell1.toString();

WrkBk.close();

return cell1.toString();

}
}

错误:

java.lang.NullPointerException
at baseProject.BaseClass.<init>(BaseClass.java:19)
at TestProject.TestClassTest.TestOne(TestClassTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

我能够理解驱动程序未在基类中正确解析或初始化。请建议我必须在哪里解决此问题。

当我在 testclass 中扩展基类时,没有 TestNG 类在运行。

最佳答案

初始化程序在构造函数之前被调用,因此,NullPointerException,请尝试以下代码:

public class BaseClass{

public static WebDriver driver;

public BaseClass(WebDriver _driver){
driver = _driver;
FirstName = driver.findElement(By.name("firstname"));
LastName = driver.findElement(By.name("LastName"));
}

public WebElement FirstName;
public WebElement LastName;
//Rest of the code

关于java - Selenium : Java Null pointer exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46815442/

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