I have written the following test using the testNG annotations:
public class Create {
WebDriver driver;
@BeforeClass
public void testsetup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
@BeforeMethod
public void Login() {
driver.get("www.xyz.com");//just an example
}
@Test(priority=3)
public void AccountCreate() {
System.out.println("Test3");
}
}
@Test(priority=1)
public void CompanyCreate() {
System.out.println("Test1");
}
@Test(priority=2)
public void VerifyResult() {
System.out.println("Test2");
}
}
@AfterMethod
public void Logout() {
System.out.println("print after method");
}
@AfterClass
public void CloseBrowser() {
driver.close();
}
}
The o/p is like this:
print after method
test1
print after method
test2
print after method
test3
Observations; @BeforeClass
executes first, then @Beforemethod
executes, then @Aftermethod
and then @Test(priority=1)
, @Aftermethod
then @Beforemethod
and then @Test(priority=2)
and so on.
But after all the @Test
run, then only the @Aftermethod
executes. Anyone, please help me on this. I really couldn't find what exactly is the problem.
There is no issue in your code block. I took your code, simplified it a bit replacing the Browsing Context line with
Sysout()
statements and here is the execution result:Code Block:
Console Output:
Analysis
This observation looks just perfect and as per your expectation.
Your usecase
Earlier it was observed at times
Sysout()
from@Test
reaches to the console later then@Aftermethod
. A simple solution would be to update your test environment with all the latest binaries:<version>6.10</version>
Reference
You can find a detailed relevant discussion in: