简体   繁体   中英

How to use TestNG RetryAnalyzer to invoke new instance driver

How can I retry the test in TestNG using Selenium WebDriver if it failed?

I have used retryAnalyser . It works but not the way I want. I want to instantiate a new instance of the web driver: "Start the test case from the beginning of the test" .

Note that the instantiation of the web driver is in the @BeforeClass . This @BeforeClass is not in the same class as the @Test class. It is located in another class. The class that has the @Test , inherits the class that has @BeforeClass and all others annotations.

To be precise here a sample model:

public class A {
  @BeforeClass(alwaysRun = true)
  public void setUp() {
    driver = new InternetExplorerDriver();
    driver.get(C.getURL());
    driver.switchTo().frame("top_frame");
    driver.manage().window().maximize();
  }    

  @AfterClass(alwaysRun = true)
  public void tearDown(ITestContext textContext) throws IOException {
    report.flush();
    driver.quit();          
  }    
}

// this is the test class

@Test(retryAnalyzer = StartWebDriver.class)
public class TestCase extends StartWebDriver{
  @Test(testName = "Login to Application ")
  public void loginTestCase() {
    login.setUserName(C.getUSER_NAME());
    login.setPassword(C.getPASSWORD());
    login.clickLogin();
    deskTopMenu.newClaimClick();
    Assert.assertTrue(findPolicyPage.searchButton().isDisplayed()); 
  }
}

Note that I already write a retry analyzer class and override its method. I just didn't put in the sample code. Now, I have tried a lot of thing without success.

For example, I have moved the annotation to the class level instead of the test level. I also have created this in the in class A like:

@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) {
  for (ITestNGMethod method : context.getAllTestMethods()) {
    method.setRetryAnalyzer(new StartWebDriver());
  }
}

But all of these doesn't instantiate the driver. It's just starting over from where test step left. I need to start over before passing to next test. Note that each class has one test.

You can change the driver instantiation logic like:

public class A {
  @BeforeMethod(alwaysRun = true)
  public void setUp() {
    if (driver = null) {
      driver = new InternetExplorerDriver();
      driver.get(C.getURL());
      driver.switchTo().frame("top_frame");
      driver.manage().window().maximize();
    }
  }

  @AfterMethod(alwaysRun = true)
  public void afterMethod(ITestResult result) {
    if (result.getStatus() = FAILURE) {
       driver.quit();
       driver = null;
    } 
  }    

  @AfterClass(alwaysRun = true)
  public void tearDown(ITestContext textContext) throws IOException {
    report.flush();
    if (driver != null) {
      driver.quit();       
    }   
  }    
}

Steps are:

  1. Create a driver if it is not yet done
  2. Run tests
  3. If a test fails, remove the driver

Note: If the @BeforeMethod is not called in case of retry analyzer, you can keep your previous @BeforeClass method and create the new driver in the @AfterMethod method. Just make a driver create method to avoid code duplication in that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM