Java – check page status programmatically

I have been writing selenium tests for web applications, and in the case of internal server errors, there seem to be multiple internal server error instances in the application. The application displays a custom error page and displays the error ID to the user to track the problems of the technical team. The case user encounters it

This makes it a bit laborious to debug test failures during selenium execution I'm thinking about using some mechanisms to keep polling the page and performing each step in the test to find out if there are any instances of internal server errors. This is what I like when I encounter JUnit rule and think of writing custom comments for it –

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";
    }
}

The seltestcase class creates a webdriver instance to execute the test. Here is 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")}
    }

}

This is what bothers me. How do I make a sound in checkpage?

Solution

With all due respect, there are two solutions to your problem If only a small number of tests need this feature, I won't use rules Instead, add a single line errorchecker. Net to each test Checkpage (driver) and implement the check in this method

If it is required for almost all 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 page check 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 controls the execution order of 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
  }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>