简体   繁体   English

以编程方式检查页面状态

[英]Checking page status programmatically

I have been writing Selenium test for web application and there seem to be multiple instances of Internal Server Error in application in case of Internal Server Error, application displays custom error page and and error id is displayed to user to pursue matter with technical team, in case user encounter it. 我一直在为Web应用程序编写Selenium测试,并且在内部服务器错误的情况下,应用程序中似乎存在多个内部服务器错误实例,应用程序显示自定义错误页面,并且向用户显示错误ID以跟踪技术团队的问题,案例用户遇到它。

This makes it a little laborious to debug the test failures during Selenium execution. 这使得在Selenium执行期间调试测试失败变得有点费力。 I was thinking to use some mechanism to keep polling a page with each step executed in test to find if there was any instance of Internal Server error, And this is when I came across Junit Rule and thought of writing a custom annotation for it, some thing like - 我正在考虑使用一些机制来保持轮询页面,在测试中执行每个步骤,以查找是否存在任何内部服务器错误的实例,这是当我遇到Junit规则并想到为它编写自定义注释时,喜欢的东西 -

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 instance of WebDriver to execute test, And here is the PageChecker class - SelTestCase类创建WebDriver实例来执行测试,这里是PageChecker类 -

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 I am stuck with, how do I proceed with CheckPage annonations? 这就是我所困扰的,我如何进行CheckPage发声?

IMHO there are two solutions to your problems. 恕我直言,你的问题有两个解决方案。 If the feature is only needed by a small part of your tests, then I would not use a rule. 如果只有一小部分测试需要该功能,那么我就不会使用规则。 Instead add a single line errorChecker.checkPage(driver) to each tests and implement the check in this method. 而是向每个测试添加单行errorChecker.checkPage(driver)并在此方法中实现检查。

If you need it for almost all your tests: 如果几乎所有测试都需要它:

  1. Convert SelTestCase to a rule by extending ExternalResource . 通过扩展ExternalResource将SelTestCase转换为规则。

     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(); } } 
  2. Add the page check code to the rule by extending Verifier . 通过扩展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 } } 
  3. Use org.junit.rules.RuleChain to control the execution order of the two rules. 使用org.junit.rules.RuleChain来控制两个规则的执行顺序。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM