How can I directly perform operations, If I remove above two lines in my second method It is getting failed. How to solve this
The tests fail because each @Test test is executed independently. The code you remove is needed to initialize the driver and load the page.
You can fix this as follows:
- Create a function,
setUp()
with the@beforemethod
annotation. Populate it with the driver initialization and loading-page calls. - Create a function,
teardown()
with the@AfterMethod
annotation. Populate it with the driver cleanup calls.
For example, here is some pseudocode (modify this as per taste)
@BeforeMethod
public void setUp() throws Exception {
driver.get("http://localhost:8070/");
driver.findElement(By.xpath("//div[@id='actions']/div[2]/a/span")).click();
driver.findElement(By.linkText("/ReportGenerator")).click();
}
@AfterMethod
public void teardown() throws Exception {
driver.quit()
}
The advantage of the @BeforeMethod and @AfterMethod annotations is that the code will be run before / after each @Test method executes. You can therefore avoid having to duplicate your code.