1

Can someone let me know why it stops execute after the first @test(priority=1)? It ran the @beforeclass, then @test(priority 1) then @aftermethod. So it skipped @test(prority=2) and @test(priority=3)

public class SDSearchTest {

    private WebDriver driver;
    private String baseUrl= "website";
    ExtentReports report;
    ExtentTest test;

    @BeforeClass
    public void beforeClass() {
        driver = new FirefoxDriver();
        driver.get(baseUrl);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
    }
    @Test(priority=1)
    public void sdLogin() {
        //enter username and password 


    }

    @Test(priority=2)
    public void testDocument() throws Exception {
        //find the element 1, enter data and drag and drop 

    }
    @Test(priority=3)
    public void pheWastesting() {

       //find the element 2, enter data and drag and drop 


    }
    @AfterMethod
    public void tearDown(ITestResult testResult) throws IOException {

        driver.quit();

    }
}
3
  • Can you Share the error shown on the stacktrace Commented Jun 30, 2018 at 2:07
  • Were there any exceptions thrown? You shouldn't use driver.quit(); in @AfterMethod, if you're planning to use the driver in other test methods as well. Commented Jul 1, 2018 at 6:08
  • Are you using testng.xml to run the test cases? if yes. paste the text of the testng.xml file in the description.
    – Alok
    Commented Jul 1, 2018 at 7:34

3 Answers 3

1

You are mixing TestNG annotations with different behavior to set up and tear down the WebDriver:

  • @BeforeClass: Runs once before the first test method of the class.
  • @AfterClass: Runs once after the last test method of the class.
  • @BeforeMethod: Runs before each test method.
  • @AfterMethod: Runs after each test method.

Change @AfterMethod to @AfterClass—just like you did with @BeforeClass for your setup method—on your teardown method to only quit the WebDriver when all test methods have been executed.

1

This is expected behavior, because there is a driver.quit() in the @AfterMethod.

The purpose of @AfterMethod is it runs after each @Test method in a class.

So the execution flow will be @BeforeClass then the @Test with highest priority followed by @AfterMethod. Here the execution will stop.

Hope this explains.

0

You shouldn't use driver.quit(); in your @AfterMethod. Instead you can use it in @AfterClass. So your execution will be as follows:

@BeforeClass
@Test(priority=1)
@Test(priority=2)
@Test(priority=3)
@AfterClass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.