When user enters "problem_user" and "secret_sauce" # stepDefinitions.LoginStepDef.user_enters_and(java.lang.String,java.lang.String)
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null
at stepDefinitions.LoginStepDef.user_enters_and(LoginStepDef.java:26)
/*
您在测试类中声明一个
driver
属性,然后在 driver
方法中创建另一个 launch_the_browser_and_navigate_to_swag_url
。
第一个 driver
对象可在类中的任何位置访问,但默认初始化为 null,因为您没有提供值。在方法中声明的另一个驱动程序对象仅在此特定方法中可见。
因此,当您调用测试用例时,它会尝试调用统一的 findElement
上的 driver
方法。
您需要做的是,不要在 launch_the_browser_and_navigate_to_swag_url
方法中创建方法范围的对象,而是使用关键字 this
将类范围的驱动程序设置为相同的值,即
public void launch_the_browser_and_navigate_to_swag_url(){
this.driver = new WebDriver(options);
//rest of your method
}
此外,您使用了代码的屏幕截图,这使得阅读和复制/粘贴内容变得困难。以后发帖时请尽量遵循准则。