Checking the program status of the page

I am writing a Selenium test for a web application, and it seems that there are several instances of an internal server error in the Internal Server Error application, the application displays a user error page, and the error identifier is displayed to the user to continue working with the technical team, in case the user encounters with her.

This makes it difficult to debug test failures during the execution of Selenium. I was thinking of using some kind of mechanism to poll the page with each step performed in the test to determine if there was any instance of an internal server error. And so when I came across a Junit rule and thought about writing a custom annotation for it, some thing like -

public class SelTestCase { protected WebDriver driver; @Before public void startDriver() { driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.google.com/"); } @After public void closeDriver() { driver.quit(); } } public class GoogleSearchTest extends SelTestCase { @Rule PageChecker pageChecker = new PageChecker(); @Test @CheckPage public void testGoogleSearch() { GoogleHomePage googleHomePage = PageFactory.initElements(driver, GoogleHomePage.class); googleHomePage.searchGoogle("Selenium HQ"); assert driver.getPageSource().contains("seleniumhq") : "Selenium headquarter search failed"; } } 

SelTestCase class creates an instance of WebDriver to run the test, but the PageChecker class -

 public class PageChecker extends SelTestCase { @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface CheckPage { // page check should take place here (Though not sure if it is right place) // like if(driver.getPageSource.contains("Internal Server Error") {throw Exception ("Err")} } } 

Here's what I'm stuck with, how can I continue with CheckPage annotations?

+4
source share
1 answer

IMHO there are two solutions to your problems. If this function is needed only by a small part of your tests, I would not use the rule. Instead, add one line of errorChecker.checkPage(driver) to each test and implement a test of this method.

If you need it for almost all of your tests:

  • Convert SelTestCase to rule by extending ExternalResource .

     public class WebDriverRule extends ExternalResource { public WebDriver driver; @Override protected void before() { driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.google.com/"); } @Override protected void after() { driver.quit(); } } 
  • Add the page verification code to the rule by extending Verifier .

     public class PageChecker extends Verifier { private WebDriverRule webDriverRule; private enabled = true; public PageChecker(WebDriverRule webDriverRule) { this.webDriverRule = webDriverRule; } public void disable() { this.enabled = false; } @Override public void verify() { if(enabled && notValid()) throw new AssertionError("foo"); } private boolean notValid() { WebDriver driver = webDriverRule.driver; //do something with driver } } 
  • Use org.junit.rules.RuleChain to control the execution order of the two rules.

     public class GoogleSearchTest { private WebDriverRule webDriverRule = new WebDriverRule(); private PageChecker pageChecker = new PageChecker(webDriverRule); @Rule public RuleChain driverAroundPageChecker = RuleChain.outerRule(webDriverRule).around(pageChecker); @Test public void testGoogleSearch() { GoogleHomePage googleHomePage = PageFactory.initElements(driver, GoogleHomePage.class); googleHomePage.searchGoogle("Selenium HQ"); assert driver.getPageSource().contains("seleniumhq") : "Selenium headquarter search failed"; } @Test public void testWithouPageCheck() { pageChecker.disable(); //here is your real test } } 
+1
source

Source: https://habr.com/ru/post/1403623/


All Articles