vendredi 18 janvier 2019

How to Close Browser after a Test Fails during Parallel Testing

Trying to close the browser when a test fails in Selenium. Tried to include an AfterMethod but not browser is still not closing on fail. Any help would be greatly appreciated!

Here is my java class:

public class googleTestClass extends Methods{

public void executeGoogle() throws InterruptedException {
    this.goToURL("https://www.google.com");
    this.enterValue("name","q","google test 1");
}

@Test
public void test1() throws InterruptedException {

    googleTestClass object1;
    object1 = new googleTestClass();
    object1.launchBrowser();
    object1.executeGoogle();
}

@Test
public void test2() throws InterruptedException {

    googleTestClass object2;
    object2 = new googleTestClass();
    object2.launchBrowser();
    object2.executeGoogle();
}

@AfterMethod
public void tearDown() throws InterruptedException, IOException {
    driver.quit();
}
} 

Here is the mentioned Methods class: // import statements

public class Methods {

public WebDriver driver;

public void launchBrowser() {

System.setProperty("webdriver.chrome.driver","C:\chromedriver_win32\chromedriver.exe"); driver = new ChromeDriver(); }

public void goToURL(String url) {
    driver.get(url);
}

    public void enterValue(String htmltype, String identifier, String value) throws InterruptedException {
    if (htmltype == "id") {
        WebElement element = driver.findElement(By.id(identifier));
        element.clear();
        element.sendKeys(value);
        element.submit();
    }
    if (htmltype =="name") {
        WebElement element = driver.findElement(By.name(identifier));
        element.clear();
        element.sendKeys(value);
        element.submit();
    }

    Thread.sleep(3000);
}

Here is the used testNG file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">

  <test thread-count="5" name="Test" parallel="methods">
    <classes>
         <class name="webDrivertests.googleTestClass">
            <methods>
                <include name ="test1"/>
                <include name ="test2"/>
            </methods>
        </class>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Is the AfterMethod not being reached because it is written incorrectly or does the problem lie in "driver.quit"? Any help would be great. Thank you!

Aucun commentaire:

Enregistrer un commentaire