简体   繁体   English

Firefox不等待页面加载Webdriverio

[英]Firefox doesn't wait for a page load Webdriverio

I am trying to run my test using Selenium and just encountered the problem. 我正在尝试使用Selenium运行测试,只是遇到了问题。 I have my test written for the Chrome browser. 我已经为Chrome浏览器编写了测试。 Now I have been trying to run the same tests in the Firefox browser, but they failed. 现在,我一直试图在Firefox浏览器中运行相同的测试,但是它们失败了。

I've started to investigate the problem and found out that Firefox doesn't wait until page is fully loaded. 我已经开始调查问题,发现Firefox不会等到页面完全加载后才开始。 Chrome works perfectly. Chrome完美运作。

I am running Selenium in docker containers. 我在Docker容器中运行Selenium

Here is my script 这是我的剧本

storeSearch(info) {
        let that = this;
        return new Promise(function (resolve, reject) {
            browserClient.init()
                .url("http://somewhere.com")
                .selectByVisibleText("#store","Tech")
                // Redirect to a new page
                .setValue("input[name='search']", info.searchCriteria)
                .selectByValue(".featured", 'MacBook')
                .click("button[name='info']")
                .element('.popup')
                .then(function (element) {
                    if (element.state === 'success') {

                    }
                });
        });
    }

It doesn't try even to select a store type from the select .selectByVisibleText("#store","Tech") and just throws an exception. 它甚至不会尝试从select .selectByVisibleText("#store","Tech")选择商店类型,而只会引发异常。

"An element could not be located on the page using the given search parameters (\\"input[name='search']\\").", “使用给定的搜索参数(\\“ input [name ='search'] \\”)无法在页面上找到元素。”,

I have tried to add timeouts but it doesn't work as well and gives me an error. 我尝试添加timeouts但效果不佳,并给了我一个错误。

  browserClient.init()
                    .url("http://somewhere.com")
                    .timeouts('pageLoad', 100000)
                    .selectByVisibleText("#store","Tech")

The following error is thrown. 引发以下错误。

"Unknown wait type: pageLoad\\nBuild info: version: '3.4.0', revision: 'unknown', time: 'unknown'\\nSystem info: host: 'ef7581676ebb', ip: '172.17.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.27-moby', java.version: '1.8.0_121'\\nDriver info: driver.version: unknown “未知等待类型:pageLoad \\ n构建信息:版本:'3.4.0',修订版:'未知',时间:'未知'\\ n系统信息:主机:'ef7581676ebb',ip:'172.17.0.3',os.name :'Linux',os.arch:'amd64',os.version:'4.9.27-moby',java.version:'1.8.0_121'\\ n驱动器信息:driver.version:未知

I have been trying to solve this problem for two days, but no luck so far. 我已经尝试解决这一问题已有两天了,但是到目前为止还没有运气。

Could someone help, maybe you have some ideas what can cause the problem ? 有人可以帮忙,也许您有什么想法可以引起问题?

Thanks. 谢谢。

UPDATE UPDATE

 .url("http://somewhere.com")
                .pause(2000)
                .selectByVisibleText("#store","Tech")

If I put some pause statements it works, but this is really bad and not what I expect from this framework. 如果我添加一些pause语句,它会起作用,但这确实很糟糕,而不是我对这个框架的期望。 Chrome works perfectly. Chrome完美运作。 It waits until loading bar is fully loaded and only then performs actions. 它等待直到加载条完全加载,然后才执行操作。

The problem is in geckodriver I guess, I have tested it the same flow in Python, Java and the behavior is exactly the same. 我猜问题出在geckodriver中,我已经在Python,Java中测试了相同的流程,并且行为完全相同。

I am experiencing the exact behavior you detailed above, all-green/passing test cases in Chrome, but on Firefox, a different story. 我在上面详细描述了您的确切行为,在Chrome中为全绿色/通过测试用例,但在Firefox中,情况却完全不同。

First off, never use timeouts , or pause in your test cases unless you are debugging. 首先,除非进行调试,否则请勿使用timeouts或在测试用例中pause In which case, chaining a .debug() previously to your failing step/command will actually do more good. 在这种情况下,将.debug()事先链接到失败的步骤/命令实际上会做得更好。

I wrapped all my WDIO commands in waitUntill() and afterwards, I saw green in Firefox as well. 我将所有WDIO命令都包装在waitUntill() ,然后在Firefox中也看到绿色。 See your code bellow: 看下面的代码:

storeSearch(info) {
let that = this;
return new Promise(function (resolve, reject) {
    browserClient.init()
        .url("http://somewhere.com")
        .waitUntil(function() {
            return browser
                .isExisting("#store");  
        }, yourTimeout, "Your custom error msg for this step")
        .selectByVisibleText("#store","Tech")
        // Redirect to a new page
        .waitUntil(function() {
            return browser
                .setValue("input[name='search']", info.searchCriteria); 
        }, yourTimeout, "Your custom error msg for this step")
        .waitUntil(function() {
            return browser
                .selectByValue(".featured", 'MacBook'); 
        }, yourTimeout, "Your custom error msg for this step")
        .waitUntil(function() {
            return browser
                .click("button[name='info']");  
        }, yourTimeout, "Your custom error msg for this step")
        .waitUntil(function() {
            return browser
                .isExisting(".popup");  
        }, yourTimeout, "Your custom error msg for this step")
        .element('.popup')
        .then(function (element) {
            assert.equal(element.state,'success');
        });
});
}

It's not pretty, but it did the job for me. 它不是很漂亮,但是为我做了工作。 Hopefully for you as well. 希望对您也是如此。

Enhancement: If you plan on actually building & maintaining a strong automation harness using WDIO , then you should consider creating custom commands that package the waiting & make your test cases more readable. 增强功能:如果计划使用WDIO实际构建和维护强大的自动化工具,则应考虑创建打包等待的定制命令 ,并使测试用例更具可读性。 See bellow an example for .click() : 请参见下面的.click()示例:

commands.js : commands.js

module.exports = (function() {
browser.addCommand('cwClick', function(element) {
    return browser
        .waitUntil(function() {
            return browser.isExisting(element);
        }, timeout, "Oups! An error occured.\nReason: element(" + element + ") does not exist")
        .waitUntil(function() {
            return browser.isVisible(element);
        }, timeout, "Oups! An error occured.\nReason: element(" + element + ") is not visible")
        .waitUntil(function() {
            return browser.click(element);
        }, timeout, "Oups! An error occured.\nReason: element(" + element + ") could not be clicked")
});
})();

All that's left to do, is import your module via require() in your test case file: var commands = require('./<pathToCommandsFile>/commands.js'); 剩下要做的就是通过测试用例文件中的require()导入模块: var commands = require('./<pathToCommandsFile>/commands.js');

You can use code in javascript which will be waiting for state of website. 您可以使用javascript中的代码来等待网站状态。 In C# looks like this: 在C#中如下所示:

public void WaitForPage(IWebDriver driver, int timeout = 30)
    {
        IWait<IWebDriver> wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
        wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
    }

I've run into quite a few issues like this with Selenium in Python and C#, unfortunately both in the Chrome and Firefox webdrivers. 我在Python和C#中遇到了很多类似的Selenium问题,不幸的是在Chrome和Firefox Webdrivers中。 The problem seems to be that the code gets ahead of itself and tries to reference elements before they even exist/are visible on the page. 问题似乎是代码先于自身,并试图在元素甚至不存在/在页面上可见之前就对其进行引用。 The solution I found in Python at least was to use the Wait functions like this: http://selenium-python.readthedocs.io/waits.html 我在Python中找到的解决方案至少是使用这样的Wait函数: http : //selenium-python.readthedocs.io/waits.html

If there isn't an equivalent in node, you might have to code a custom method to check the source every so often for the presence of the element in x intervals over time. 如果节点中没有等效项,则可能必须编写一种自定义方法,以经常检查源中是否存在x个间隔内的元素(随着时间的推移)。

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

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