【问题标题】:How do I test that the page has reloaded when I click a link to the current page?当我单击指向当前页面的链接时,如何测试页面是否已重新加载?
【发布时间】:2014-01-21 02:45:24
【问题描述】:

我正在尝试通过测试重新创建一个奇怪的错误。我知道是什么导致了这个错误以及如何修复它,但我不知道如何正确测试它。

场景:

  • 我的浏览器在http://example.com/foo
  • 我单击该页面上带有 href="/foo" 的链接(同一页面)
  • 浏览器应该重新加载相同的页面

我试过了:

class SamePageHyperlinkSpec extends StatelessUserWebGebSpec {

    def 'users is on Foo page and clicks foo link'() {

        when:
            to FooPage

        then:
            // navigation.foo is a property in a super class that references
            // a particular navigation item
            navigation.foo.click()

        and:
            at FooPage
    }
}

...但这给出了误报,因为尽管浏览器没有重新加载页面,但它已经在该页面上。所以,我想我会看看 PageChangeListener 应该允许我通过/失败页面更改:

class SamePageHyperlinkSpec extends StatelessUserWebGebSpec {

    def 'users is on Foo page and clicks foo link'() {

        given:
            def listener = new MyPageChangeListener()
            browser.registerPageChangeListener(listener)

        when:
            to FooPage
        and:
            listener.changeCount = 0

        then:
            // navigation.foo is a property in a super class that references
            // a particular navigation item
            navigation.foo.click()

        and:
            at FooPage

        and:
            listener.changeCount == 1
    }
}

class MyPageChangeListener implements PageChangeListener {

    int changeCount = 0

    void pageWillChange(Browser browser, Page oldPage, Page newPage) {
        println "browser '$browser' changing page from '$oldPage' to '$newPage'"
        changeCount++
    }
}

...当没有页面重新加载时失败(应该如此),但是当页面重新加载时它也会失败,因为它最终到达的页面是相同的,所以没有页面 change 发生了。

我还尝试复制 Page 类(它是“at”闭包),但是无论是否重新加载页面,它总是通过。

那么,当我单击指向当前页面的链接时,如何测试页面是否已重新加载?

【问题讨论】:

  • 重新加载时页面有什么变化吗?例如字段或元素可见等?
  • @PaulHarris Nope, nada
  • 我正在尝试除此之外的其他东西,但它会起作用....基本上你可以使用可以将你的驱动程序转换为 JavascriptExecutor 然后执行一些 Javascript 来向页面添加一个元素。然后您可以重新加载,您将知道它已完成,因为您刚刚添加的元素将消失。必须有更好的方法,但我现在的想法出奇地白痴。

标签: selenium functional-testing spock geb


【解决方案1】:

如果您在页面的文本字段中写下一些值,然后创建一个等待程序,等待该文本字段不包含在页面中,然后单击链接,会怎样? 所以程序是这样的:

1.- 在文本字段中写入一些新值或使用 js 添加新的 html 标签或其他内容。

2.- 制定一个例程,等待您添加到页面的元素/值不包含在页面中。

3.- 点击链接。

4.- 调用等待代码。

让我知道这是否适合你。

【讨论】:

    【解决方案2】:

    在@PaulHarris 的带领下,JS 方法似乎可以解决问题:

    class SamePageHyperlinkSpec extends StatelessUserWebGebSpec {
    
        def 'users is on Foo page and clicks foo link'() {
    
            when:
                to FooPage
    
            and:
                browser.js.exec '$(document.body).attr("data-not-reloaded",true);'
    
            then:
                // navigation.foo is a property in a super class that references
                // a particular navigation item
                navigation.foo.click()
    
            and:
                at FooPage
    
            and:
                browser.js.exec 'return $(document.body).attr("data-not-reloaded") === undefined';
        }
    }
    

    【讨论】:

      【解决方案3】:

      Here 是在 C#.Net 中实现的更好且经过测试的 WaitForPageLoad 版本

      public void WaitForPageLoad(int maxWaitTimeInSeconds) {
      string state = string.Empty;
      try {
          WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(maxWaitTimeInSeconds));
      
          //Checks every 500 ms whether predicate returns true if returns exit otherwise keep trying till it returns ture
          wait.Until(d = > {
      
              try {
                  state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
              } catch (InvalidOperationException) {
                  //Ignore
              } catch (NoSuchWindowException) {
                  //when popup is closed, switch to last windows
                  _driver.SwitchTo().Window(_driver.WindowHandles.Last());
              }
              //In IE7 there are chances we may get state as loaded instead of complete
              return (state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase));
      
          });
      } catch (TimeoutException) {
          //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
          if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
              throw;
      } catch (NullReferenceException) {
          //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
          if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
              throw;
      } catch (WebDriverException) {
          if (_driver.WindowHandles.Count == 1) {
              _driver.SwitchTo().Window(_driver.WindowHandles[0]);
          }
          state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
          if (!(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase)))
              throw;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        • 2014-02-11
        • 2017-11-01
        • 1970-01-01
        相关资源
        最近更新 更多