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