2017-05-14 159 views
1

我试图运行我的测试使用Selenium并刚刚遇到问题。我有我为Chrome浏览器编写的测试。现在我一直试图在Firefox浏览器中运行相同的测试,但失败了。Firefox不会等待页面加载Webdriverio

我已经开始调查问题,发现Firefox不会等到页面完全加载。 Chrome完美运作。

我在Docker容器中运行Selenium

这里是我的脚本

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') { 

        } 
       }); 
     }); 
    } 

它不会尝试甚至从选择.selectByVisibleText("#store","Tech")选择存储类型,只是抛出一个异常。

“(输入[名=“搜索”] \“,

我尝试添加的元件不能在页面上使用给定的搜索 参数\)”位于” timeouts但它不工作,并给我一个错误。

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

引发以下错误。

“未知等待类型:页面加载\ nBuild信息:版本: '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 : 未知

我一直在试图解决这个问题了两天,但至今没有运气

可能有人帮助,也许你有秒。 ome的想法是什么会导致问题?

谢谢。

UPDATE

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

如果我把一些pause陈述它的工作原理,但是这是真的不好,不是我想从这个框架的期望。 Chrome完美运作。它会一直等到加载栏完全加载,然后才执行操作。

问题是在geckodriver我猜测,我测试了它在Python中的相同流,Java和行为是完全一样的。

+0

[@bxfvgekd(HTTPS:/ /stackoverflow.com/users/4671628/bxfvgekd)你有点杀死与'更新'的问题。这不是一个** geckodriver问题**,更不用说'.pause()'不是最佳实践。请考虑重新阅读我的答案并相应地更新/接受答案。干杯! – iamdanchiv

+0

请考虑根据我的回答/以上评论结束问题。再一次,这不是一个GheckoDriver问题,它是**最佳实践**来显式等待** WebElement。干杯! – iamdanchiv

回答

0

我已经遇到了很多类似于Python和C#中的Selenium的问题,不幸的是在Chrome和Firefox的webdrivers中都存在这样的问题。问题似乎是,代码在它自己之前,并尝试引用元素,甚至在它们存在/在页面上可见之前。我在Python中找到的解决方案至少是使用这样的Wait函数:http://selenium-python.readthedocs.io/waits.html

如果节点中没有等价物,那么您可能必须编写自定义方法来检查源代码,随着时间的推移x元素中的元素。

0

您可以在JavaScript中使用代码,它将等待网站的状态。 在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")); 
    } 
2

我遇到的确切行为你上面详细,全面绿色/通过测试用例浏览器,但在Firefox,一个不同的故事。

首先,从不使用timeoutspause在您的测试案例,除非您正在调试。在这种情况下,将.debug()先前链接到您的失败步骤/命令将实际上会更好。

我把我所有的WDIO命令全部包装在waitUntill()之后,之后我在Firefox中也看到了绿色。看到你的代码波纹管:

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'); 
     }); 
}); 
} 

这不是很漂亮,但它为我做了工作。希望对你有好处。

增强:如果您计划在实际构建&维护使用WDIO强大的自动化线束,那么你应该考虑创建custom commands这个包等待&让你的测试用例更具可读性。参见下文对.click()一个例子:

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

所有剩下要做的,就是通过require()在你的测试用例文件导入模块:var commands = require('./<pathToCommandsFile>/commands.js');