9

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

4 に答える 4

0

RubyとTestUnitを使用している場合は、次のようになります-

複数のテストでwedriverブラウザセッションを共有していて、最後にのみブラウザを閉じる必要がある場合

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

または、個々のテストの後にブラウザを閉じたい場合

def teardown
    @driver.quit
    assert_equal [], @verification_errors
end
于 2012-12-21T19:04:13.703 に答える
0

driver.close() はあなたのためにトリックを行うはずです。または、ブール値フラグを設定し、テストが失敗すると、ブール値フラグが true に設定されます。

そして、 driver.close() または System.exit(1) を使用すると機能するはずです!!

上記の回避策で問題が発生した場合はお知らせください。

于 2014-01-31T19:33:39.003 に答える