Firefox不会等待页面加载Webdriverio
我正在尝试使用Selenium
运行我的testing,并刚刚遇到问题。 我有我为Chrome
浏览器编写的testing。 现在我一直试图在Firefox
浏览器中运行相同的testing,但是失败了。
我已经开始调查这个问题,并发现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') { } }); }); }
它甚至不会尝试从select .selectByVisibleText("#store","Tech")
select一个存储types,而只是抛出一个exception。
“一个元素不能使用给定的search参数(\”input [name ='search'] \“)。”,
我试图添加timeouts
但它不工作,给我一个错误。
browserClient.init() .url("http://somewhere.com") .timeouts('pageLoad', 100000) .selectByVisibleText("#store","Tech")
引发以下错误。
“未知等待types: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:unknown
我一直在试图解决这个问题两天,但没有运气到目前为止。
有人可以帮忙,也许你有一些想法可以导致这个问题?
谢谢。
UPDATE
.url("http://somewhere.com") .pause(2000) .selectByVisibleText("#store","Tech")
如果我把一些pause
语句的工作,但这是非常糟糕的,而不是我期望从这个框架。 Chrome的作品完美。 它会一直等到加载栏完全加载,然后才执行操作。
问题是在geckodriver我猜测,我已经testing了它在Python中的相同stream,Java和行为是完全一样的。
我正在体验上面详细描述的确切行为,在Chrome中是全绿/通过testing用例,但是在Firefox上,则是另一回事。
首先,除非您正在debugging,否则绝对不要使用timeouts
,或在testing用例中pause
。 在这种情况下,将.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'); }); }); }
这不美,但它为我做了工作。 希望对你也是如此。
增强function:如果您计划使用WDIO实际构build和维护强大的自动化function ,那么您应该考虑创build自定义命令来打包等待并使您的testing案例更具可读性。 请参阅以下示例.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") }); })();
所有剩下的事情就是通过你的testing用例文件中的require()
来导入你的模块: var commands = require('./<pathToCommandsFile>/commands.js');
在Python和C#中,我遇到了很多类似于Selenium的问题,包括Chrome和Firefox的驱动程序。 问题似乎是,代码超越自身,并试图在元素甚至存在/在页面上可见之前引用元素。 我在Python中find的解决scheme至less是使用这样的等待函数: http : //selenium-python.readthedocs.io/waits.html
如果节点中没有等价物,则可能需要编写一个自定义方法来检查源,以便经常检查x间隔中是否存在元素。
您可以使用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")); }