[英]Firefox doesn't wait for a page load Webdriverio
我正在嘗試使用Selenium
運行測試,只是遇到了問題。 我已經為Chrome
瀏覽器編寫了測試。 現在,我一直試圖在Firefox
瀏覽器中運行相同的測試,但是它們失敗了。
我已經開始調查問題,發現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")
選擇商店類型,而只會引發異常。
“使用給定的搜索參數(\\“ input [name ='search'] \\”)無法在頁面上找到元素。”,
我嘗試添加timeouts
但效果不佳,並給了我一個錯誤。
browserClient.init()
.url("http://somewhere.com")
.timeouts('pageLoad', 100000)
.selectByVisibleText("#store","Tech")
引發以下錯誤。
“未知等待類型: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:未知
我已經嘗試解決這一問題已有兩天了,但是到目前為止還沒有運氣。
有人可以幫忙,也許您有什么想法可以引起問題?
謝謝。
UPDATE
.url("http://somewhere.com")
.pause(2000)
.selectByVisibleText("#store","Tech")
如果我添加一些pause
語句,它會起作用,但這確實很糟糕,而不是我對這個框架的期望。 Chrome完美運作。 它等待直到加載條完全加載,然后才執行操作。
我猜問題出在geckodriver中,我已經在Python,Java中測試了相同的流程,並且行為完全相同。
我在上面詳細描述了您的確切行為,在Chrome中為全綠色/通過測試用例,但在Firefox中,情況卻完全不同。
首先,除非進行調試,否則請勿使用timeouts
或在測試用例中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');
});
});
}
它不是很漂亮,但是為我做了工作。 希望對您也是如此。
增強功能:如果計划使用WDIO實際構建和維護強大的自動化工具,則應考慮創建打包等待的定制命令 ,並使測試用例更具可讀性。 請參見下面的.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');
您可以使用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"));
}
我在Python和C#中遇到了很多類似的Selenium問題,不幸的是在Chrome和Firefox Webdrivers中。 問題似乎是代碼先於自身,並試圖在元素甚至不存在/在頁面上可見之前就對其進行引用。 我在Python中找到的解決方案至少是使用這樣的Wait函數: http : //selenium-python.readthedocs.io/waits.html
如果節點中沒有等效項,則可能必須編寫一種自定義方法,以經常檢查源中是否存在x個間隔內的元素(隨着時間的推移)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.