测试失败后关闭浏览器

时间:2012-12-21 18:52:59

标签: testing selenium webdriver testng qa

我正在使用WebDriver运行测试,当测试失败时,浏览器不会关闭。在Windows机器上,这是一个很大的问题,因为我有几个IEDriver实例仍然在后台运行。

我尝试了一个似乎不起作用的try / catch语句。如果测试失败,浏览器仍然保持打开状态。任何帮助将不胜感激。

try catch语句如下所示:

try
{
   Assert.something(something something dark side);
   driver.quit();
}
catch(Exception e)
{
   System.out.println(e)
   driver.quit();
}

我的完整代码如下:

public class ClickAddMedication 
{
Browser browser = new Browser();

public void addMedication(String driverName)
{
    //Open Browser and navigate to page
    WebDriver driver = browser.getDriver(driverName);
    driver.manage().window().maximize();
    driver.get("http://someIP:8080/hmp_patient/index.html");

    //Click Add Medication button
    WebElement addBtn = driver.findElement(By.id("add-btn"));
    addBtn.click();

    //Verify Add Medication page has loaded successfully
    WebElement rxBtn = driver.findElement(By.className("icon-rx"));
    WebElement otcBtn = driver.findElement(By.className("icon-otc"));
    WebElement herbBtn = driver.findElement(By.className("icon-herb"));

    Assert.assertEquals(true, rxBtn.isDisplayed());
    Assert.assertEquals(true, otcBtn.isDisplayed());
    Assert.assertEquals(true, herbBtn.isDisplayed());

    driver.quit();

}

@Test(groups = {"functionalTests.FF"})
public void test_AddMedication_FF()
{
    addMedication("firefox"); 
}
@Test(groups = {"functionalTests.iOS"})
public void test_AddMedication_iOS()
{
    addMedication("iOS");
}
}

我使用testng.xml文件运行测试,并且无论测试是否通过,都希望浏览器关闭。

以下是我的Browser课程:

public class Browser 
{
public WebDriver getDriver(String driverName)
{
    WebDriver driver = null;
    if(driverName == "firefox")
    {
        driver = new FirefoxDriver();
    }
    else if(driverName == "chrome")
    {
        File chromeFile = new File ("C:/webdrivers/chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", chromeFile.getAbsolutePath());
        driver = new ChromeDriver();
    }
    else if(driverName == "ie")
    {
        File ieFile = new File("C:/webdrivers/IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", ieFile.getAbsolutePath());
        driver = new InternetExplorerDriver();
    }
    else if(driverName == "iOS")
    {
        try 
        {
            driver = new RemoteWebDriver(new URL("http://localhost:3001/wd/hub"), DesiredCapabilities.ipad());
        } catch (MalformedURLException e) 
        {

            e.printStackTrace();
        }
    }


    return driver;

}
}

4 个答案:

答案 0 :(得分:6)

您还没有提到用于执行测试的框架,但处理此问题的一种方法是使用等效的“after test”注释来完成此任务。当TestNG将其称为@After时,JUnit会调用此批注@AfterMethod。无论测试的通过/失败状态如何,使用后测试注释注释的方法将在使用@Test注释的每个方法之后运行。如果您希望在测试方法中使用相同的驱动程序实例,则大多数测试运行程序都有@AfterClass注释或类似注释,它将在类中所有@Test方法的末尾运行。

例如,您需要执行以下操作(请注意将driver变量提升为类中的成员变量):

public class ClickAddMedication 
{
    // N.B. For this approach to work, you *must* have the "driver"
    // variable here. Having it as a member variable of the class is
    // what allows the addMedication() method to access it for manipulating
    // the browser, and the tearDown() method to access it for closing
    // the *same* *browser* *instance*.
    WebDriver driver;
    Browser browser = new Browser();

    public void addMedication(String driverName)
    {
        //Open Browser and navigate to page
        driver = browser.getDriver(driverName);
        driver.manage().window().maximize();
        driver.get("http://someIP:8080/hmp_patient/index.html");

        //Click Add Medication button
        WebElement addBtn = driver.findElement(By.id("add-btn"));
        addBtn.click();

        //Verify Add Medication page has loaded successfully
        WebElement rxBtn = driver.findElement(By.className("icon-rx"));
        WebElement otcBtn = driver.findElement(By.className("icon-otc"));
        WebElement herbBtn = driver.findElement(By.className("icon-herb"));

        Assert.assertEquals(true, rxBtn.isDisplayed());
        Assert.assertEquals(true, otcBtn.isDisplayed());
        Assert.assertEquals(true, herbBtn.isDisplayed());
    }

    @AfterMethod
    public void tearDown()
    {
        driver.quit();
    }

    @Test(groups = {"functionalTests.FF"})
    public void test_AddMedication_FF()
    {
        addMedication("firefox"); 
    }

    @Test(groups = {"functionalTests.iOS"})
    public void test_AddMedication_iOS()
    {
        addMedication("iOS");
    }
}

答案 1 :(得分:0)

如果您使用的是Ruby和测试单元,您可以使用类似的东西 -

如果您正在共享多个测试的wedriver浏览器会话,并且只需要在结束时关闭浏览器

def self.shutdown
    @driver.quit
    assert_equal [], @verification_errors
end

或者如果您想在每次单独测试后关闭浏览器

def teardown
    @driver.quit
    assert_equal [], @verification_errors
end

答案 2 :(得分:0)

这可能不是一个合适的解决方案,但我这样做是解决问题的方法。

而不是Try{}catch ...使用throwsthrows java.lang.AssertionError并退出catch块中的浏览器,例如:

public void testMethod() throws java.lang.AssertionError{

Assert.assertTrue(condition,Message);
}

catch(java.lang.AssertionError e){

e.printstactrace();

browser.quit();

Assert.fail();

}

答案 3 :(得分:0)

driver.close()应该为你做的伎俩。或设置布尔标志,当测试失败时,布尔标志设置为true。

然后使用driver.close()或System.exit(1)应该工作!!

如果您在使用上述解决方法时遇到问题,请与我们联系。

相关问题