具有Selenium的多浏览器环境中的GUI

时间:2015-08-24 13:13:49

标签: 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中运行,我只是试图让它在两个浏览器中一个接一个地运行

1 个答案:

答案 0 :(得分:1)

我的猜测是你

  

获取NullPointerException

因会话问题(特别是如果您处理driver.getWindowHandle();代码)。使用driver.close();关闭单个浏览器窗口,使用driver.quit();结束整个会话。 driver.get("...");的第二个位置可能是相同的 - 驱动程序无法切换到下一个浏览器(会话)。

相关问题