Question

I am new to Selenium, While practicing I come up with one issue, I am doing testing for my own application which has been deployed in tomcat server. So after opening my application I am testing validations in one method and page change in one method. Now My point is I am doing both testing for my both methods at same page.

Why do I need to write same code both methods,

 driver.get("http://localhost:8070/");
 driver.findElement(By.xpath("//div[@id='actions']/div[2]/a/span")).click();
 driver.findElement(By.linkText("/ReportGenerator")).click();

How can I directly perform operations, If I remove above two lines in my second method It is getting failed. How to solve this?

 @Test
        public void analysisValidation()
        {
            driver.get("http://localhost:8070/");
            driver.findElement(By.xpath("//div[@id='actions']/div[2]/a/span")).click();
            driver.findElement(By.linkText("/ReportGenerator")).click();
            driver.findElement(By.id("Analysis")).click();
            WebElement webElement = driver.findElement(By.id("modelForm.errors"));
            String alertMsg = webElement.getText();
            System.out.println(alertMsg);
            Assert.assertEquals("Please select a Survey Id to perform Aggregate Analysis", alertMsg); 

        }

        @Test
        public void testAnalysisPage()
        {
                driver.get("http://localhost:8070/");
                driver.findElement(By.xpath("//div[@id='actions']/div[2]/a/span")).click();
                driver.findElement(By.linkText("/ReportGenerator")).click();
                new Select(driver.findElement(By.id("surveyId"))).selectByVisibleText("Apollo");
                driver.findElement(By.id("Analysis")).click();
                System.out.println(driver.getTitle());
                String pageTitle = driver.getTitle();
                Assert.assertEquals("My JSP 'analysis.jsp' starting page", pageTitle);
        }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top