techjourneyman
techjourneyman

Reputation: 1813

Text check in every page (Selenium)

Using Java, I am trying to write a general check for a particular text on every page in a web application that existing tests visit. Instead of having to write it on each and every page individually, is it possible to do in one place at a high level (may be in the base class)?

public class BaseClassForUiTest {
                public BaseClassForUiTest() {
                       ...
                }

                public void test() throws Exception {
                       boolean isNewPage = checkIfNewPage();
                       if (isNewPage)
                             // perform a text check on the page
                }
}

Every test extends from BaseClassForUiTest and overrides the test() method.

Upvotes: 1

Views: 245

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23815

Instead of having to write it on each and every page individually, is it possible to do in one place at a high level (may be in the base class)?

Yes, it is possible by implementing WebDriverEventListener into BaseClassForUiTest and override event handler methods to handling the appropriate WebDriver events according to need in one place.

Here every method corresponds to an event. According to your requirement you need to handle afterNavigateTo() method. This one is called every time the navigate to a page is completed.

You have to do perform a text checker on the page code in this method so that your code is executed every time the page navigates to some other page.

public class BaseClassForUiTest implements WebDriverEventListener
{

      ---------
      ---------

      public void afterNavigateTo(String arg0, WebDriver arg1) {
    // perform desire text checker stuff on the page here
  }
}

Now Create Event Throwing WebDriver to perform your test :-

  • Create a regular WebDriver.

    FirefoxDriver driver = new FirefoxDriver();
    
  • Now create an EventThrowingWebDriver using our regular WebDriver created above.

    EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
    
  • Create an instance of your eventHandler class and register it for events using the register method of EventFiringWebDriver object created above as :-

    BaseClassForUiTest handler = new BaseClassForUiTest();
     eventDriver.register(handler);
    

Full code :-

import org.openqa.selenium.support.events.EventFiringWebDriver;

public class BaseClassForUiTest {

 public void test() throws Exception {              

      FirefoxDriver driver = new FirefoxDriver();
      EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);

      BaseClassForUiTest handler = new BaseClassForUiTest();
  eventDriver.register(handler);

      eventDriver.get("your url");

      //Now do your further stuff
}
}

Upvotes: 1

A. Chubarov
A. Chubarov

Reputation: 89

As for me, you'd better create a separated test class for this check using Parameterized or JUnitParams and give it a urls to run where as parameters, but it depends on what is your common approach to running tests (we run all testpack at the same time, so it's a solution for us in this situation). Also it is seems like well-logically-separated solution

If you are going to use this check as an assertion you can rewrite your current code for this case and call for it in @Before block (but it is still not a good solution, in my opinion)

Upvotes: 0

Related Questions