我正在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;
}
}
你把你的測試的編輯,你把JimEvans答案,所以編輯被拒絕。請在此處將其編輯爲您的問題。 –
補給它,我以爲我有。 – DarthOpto