这是我的Hook课程。
public class Hooks {
public String browser;
public static WebDriver driver;
//IE, Chrome, Opera working ||| firefox ~working
@Before
public void beforeEach() throws IOException {
browser = System.getenv("BROWSER");
if (browser == null) {
browser = "ie";
}
System.out.println("Browser selected is " + browser);
if (browser.equalsIgnoreCase("chrome")) {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setJavascriptEnabled(true);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", "C:/Program Files/SeleniumDrivers/chromedriver.exe");
driver = new ChromeDriver(cap);
} else if (browser.equalsIgnoreCase("ie")) {
File file = new File("C:/Program Files/SeleniumDrivers/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("opera")) {
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
File file = new File("C:/Program Files/SeleniumDrivers/operadriver.exe");
System.setProperty("webdriver.opera.driver", file.getAbsolutePath());
driver = new OperaDriver(capabilities);
} else{
driver = new FirefoxDriver();
}
driver.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
}
@After
public void close() {
driver.quit();
}
}
我正在使用Selenium Junit和黄瓜。 我已经实现了IE,firefox,chrome,opera的代码。 我无法启动浏览器并获得java空指针异常。 并且控制台中没有其他错误信息。
以下是我登录步骤的java类。有什么线索吗?
public class LoginSteps {
public WebDriver driverLaunch;
public String landingURL = "https://www.periscopix.com";
public LoginSteps() {
driverLaunch = Hooks.driver;
}
@Given("^I am on company landing page$")
public void I_am_on_company_landing_page() throws Throwable {
driverLaunch.navigate().to("https://www.google.com");
driverLaunch.close();
}
@Given("^I wait for some time$")
public void I_wait_for_some_time() throws Throwable {
Thread.sleep(1000);
}
@When("^Then the page loads succesfully$")
public void Then_the_page_loads_succesfully() throws Throwable {
assertEquals(driverLaunch.getCurrentUrl(), landingURL);
}
}
提前致谢
答案 0 :(得分:0)
在^ LoginSteps ^中扩展^ Hooks ^并使用^ Hooks ^的静态驱动程序实例。删除^ driverLaunch ^变量。