繁体   English   中英

测试失败后关闭浏览器

[英]Closing Browser After Failed Test

我正在使用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;

}
}

您没有提到要使用什么框架来执行测试,但是处理此问题的一种方法是使用等效的“测试后”注释来完成此任务。 JUnit将该注释称为@After而TestNG将该注释称为@AfterMethod 无论测试的通过/失败状态如何,使用after test注释注释的方法将在使用@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");
    }
}

如果您使用的是Ruby和测试单元,则可能会出现以下内容-

如果您要共享wedriver浏览器会话以进行多个测试,并且只需要在最后关闭浏览器

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

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

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

这可能不是合适的解决方案,但是我将其作为解决该问题的方法。

而不是Try{}catch ..., 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();

}

driver.close()应该为您解决问题。 或设置一个布尔标志,当测试失败时,布尔标志设置为true。

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

如果上述解决方法遇到问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM