我有以下硒结构,我在浏览器启动后面临错误&最大化。当@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类。
答案 0 :(得分:2)
在构造函数之前调用初始化程序,因此,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
答案 1 :(得分:1)
@DarshanMehta很好地解释了这个问题及其解决方案。我刚刚测试了它,它的工作原理。我还想补充一下:
您在Stackoverflow中已经讨论过您遇到的问题。我认为这个链接可以让你更好地理解一个类实例化时的执行顺序: Are fields initialized before constructor code is run in Java?
因此,您可以在第一个答案中读取初始化块在构造函数的super()部分之后完成,但在构造函数的其余部分之前完成。
如果例如你的驱动程序是在BaseClass的超类中初始化的话,你就不会遇到这个错误。 因此,如果您真的想在初始化块中初始化FirstName
和LastName
,则可以创建一个BaseTest将继承的超类。它应该解决你的问题。
你可以尝试下面的代码,说明一下:创建一个超类:
public class MotherClass {
public MotherClass(WebDriver _driver){
driver = _driver;
}
WebDriver driver;
}
并在BaseClass的构造函数中添加super,它继承自MotherClass:
public class Testasupp extends MotherClass{
public BaseClass(WebDriver _driver)
{
super(_driver);
FirstName = driver.findElement(By.name("firstname"));
LastName = driver.findElement(By.name("LastName"));
}
总结:除非你有充分的理由选择这个解决方案,否则我真的认为@DashanMehta提供了更好的解决方案。