Webdriver等待页面完全加载

huangapple go评论88阅读模式
英文:

Webdriver wait for complete page to load

问题

以下是代码部分的中文翻译:

browser.waitUntil(
  () => browser.execute(() => document.readyState === 'complete'),
  {
    timeout: 60 * 1000, // 60秒
    timeoutMsg: '加载失败时的消息'
  }
);

这段代码用于等待页面完全加载。您可以将它放在脚本的以下位置:

"Where exactly I have to put this readyState code in script?
Currently I am adding this LOC after navigating any new page and before code where loading is happening."

英文:

Is there any way to make webdriverio wait for a complete page to load?
I dont want to use browser.setTimeout({ 'pageLoad': 10000 }), because I dont know how much time it will take to load complete page
I tried below code but it didn't worked,

browser.waitUntil(
() => browser.execute(() => document.readyState === 'complete'),
{
timeout: 60 * 1000, // 60 seconds
timeoutMsg: 'Message on failure'
}
);

Where exactly I have to put this readyState code in script?
Currently I am adding this LOC after navigating any new page and before code where loading is happening.

答案1

得分: 1

了解您的担忧,关于超时问题。假设您正在遵循页面对象模式(https://webdriver.io/docs/pageobjects/),您可以将其添加为页面类的基本方法。例如:

class WhateverPage {

  open(path): void {
    browser.url(path);
    this.waitForPageLoad(); // 在此处添加页面加载条件
  }

  waitForPageLoad(): void {
    browser.waitUntil(
      () => browser.execute(
        () => document.readyState === 'complete'),
        {
          timeout: 60 * 1000, // 60秒
          timeoutMsg: '失败时的消息'
        }
      )
    )
  }
}

现在,在您的测试中,当您作为开始步骤执行 Page.open() 时,它应该在执行任何其他操作之前等待页面加载。在执行页面转换时,您还可以单独使用 waitForPageLoad 方法。希望这有所帮助!

英文:

I understand your concerns, regarding timeouts. Assuming you are following the page object pattern (https://webdriver.io/docs/pageobjects/), you can add that as a base method for your Page class. For example:

  class WhateverPage {

    open(path): void {
        browser.url(path);
        this.waitForPageLoad(); // add your is loaded conditions here
    }

    waitForPageLoad(): void {
      browser.waitUntil(
        () => browser.execute(
          () => document.readyState === 'complete'),
          {
            timeout: 60 * 1000, // 60 seconds
            timeoutMsg: 'Message on failure'
          }
        )
      )
    }
  }

Now in your test when you do as a beggining step Page.open() it should wait for the page to be loaded before it does anything else. You can also use the waitForPageLoad method here by itself when you do page transitions. Let me know if this helps!

huangapple
  • 本文由 发表于 2023年3月9日 21:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定