java - 使用 Selenium 的多浏览器环境中的 GUI

标签 java selenium selenium-webdriver nullpointerexception

我正在尝试编写多浏览器 GUI 测试代码。但我在

中遇到 NullPointerException
driver.quit();

driver.get("http://localhost:8080/Params/ClientCutOff/index.jsp?lang=en");

这是我的代码

public class CrossBrowserTest {
public WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private final StringBuffer verificationErrors = new StringBuffer();

@BeforeTest
public void setUp() throws MalformedURLException {
    int i;
    for (i = 0; i < 2; i++) {
        int browser = i;

        if (browser == 0) {
            System.out.println("Running Firefox");
            driver = new FirefoxDriver();

        }

        else if (browser == 1) {
            System.out.println("Running Internet Explorer");
            DesiredCapabilities ieCapabilities = DesiredCapabilities
                    .internetExplorer();
            ieCapabilities
                    .setCapability(
                            InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
                            true);
            File file = new File(
                    "C:/tools/Selenium/IEDriverServer_x64_2.47.0/IEDriverServer.exe");
            System.setProperty("webdriver.ie.driver",
                    file.getAbsolutePath());
            driver = new InternetExplorerDriver(ieCapabilities);
            baseUrl = "http://isisetet:8081/";
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        }
    }
}

@Test
public void testAccess() throws Exception {
    // Test If the screen is acessible
    System.out.println("Test if the screen is accessible");
    driver.get("http://localhost:8080/Params/ClientCutOff/index.jsp?lang=en");
    assertTrue(driver.getTitle().matches("^[\\s\\S]*ClientCutOff$"));
}

@After
public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

private boolean isAlertPresent() {
    try {
        driver.switchTo().alert();
        return true;
    } catch (NoAlertPresentException e) {
        return false;
    }
}

private String closeAlertAndGetItsText() {
    try {
        Alert alert = driver.switchTo().alert();
        String alertText = alert.getText();
        if (acceptNextAlert) {
            alert.accept();
        } else {
            alert.dismiss();
        }
        return alertText;
    } finally {
        acceptNextAlert = true;
    }
}

该测试可以单独在 mozilla 或 IE 中运行,我只是想让它在两个浏览器中相继运行

最佳答案

我猜你是

getting a NullPointerException

因为 session 问题(尤其是在处理 driver.getWindowHandle(); 代码时)。使用 driver.close(); 关闭单个浏览器窗口,使用 driver.quit(); 结束整个 session 。第二个地方是 driver.get("..."); 可能是由相同原因引起的 - 驱动程序无法切换到下一个浏览器( session )。

关于java - 使用 Selenium 的多浏览器环境中的 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32183254/

相关文章:

java - Java 是否支持创建属性少于父类型的类型?

java - 在 JAX-WS Web 服务中实现 WS-Policy

java - Android 运行时错误,ActivityNotFoundException : No Activity

java - 编码风格: checking user input is messy. 我做错了吗?

java - 如何处理 "org.openqa.selenium.WebDriverException: ChromeDriver only supports characters in the BMP"异常?

javascript - 在javascript中通过麦克风发送声音

python - 构建函数以清除字段和 send_keys

java - Selenium webdriver Java 代码使用 web 驱动程序双击网格中的记录

java - selenium中如何使用方法调用内部的各种方法并返回true

Selenium + Jenkins + Chromedriver = WebDriverException : Message: unknown error: Chrome failed to start: exited abnormally