gpt4 book ai didi

selenium - 测试失败后关闭浏览器

转载 作者:行者123 更新时间:2023-11-28 19:45:58 25 4
gpt4 key购买 nike

我正在使用 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 文件运行测试,并且无论测试是否通过都希望关闭浏览器。

下面是我的浏览器类:

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");
}
}

关于selenium - 测试失败后关闭浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13995609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com