selenium webdriver在sendKeys先前填充字段后清除它们 [英] selenium webdriver is clearing out fields after sendKeys had previously populated them

查看:856
本文介绍了selenium webdriver在sendKeys先前填充字段后清除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试的网页正在使用淘汰赛.在我们网站上当前未使用淘汰赛的其他页面上,我没有同样的问题.在打开页面的情况下,我输入了多个必填字段,然后单击保存"按钮.在最后一个文本字段中输入值与单击保存按钮之间的某个时间点,以前具有值的字段将被清除,因此脚本无法继续.这是我正在运行的代码的示例:

The webpage that I'm testing is using knockout. On other pages on our site that are not currently using knockout I'm not having the same problem. The scenario I have is where the page opens, I enter in various required fields and click the save button. At some point between when it enters a value in the last text field and when it clicks the save button the fields that previously had values become cleared out, and thus the script can't continue. Here is an example of the code that I'm running:

driver.findElement(By.id("sku")).clear();
driver.findElement(By.id("sku")).sendKeys(itemNo);
driver.findElement(By.id("desktopThankYouPage")).clear();
driver.findElement(By.id("desktopThankYouPage")).sendKeys(downloadUrl);
driver.findElement(By.id("mobileThankYouPage")).clear();
driver.findElement(By.id("mobileThankYouPage")).sendKeys(mobileDownloadUrl);
driver.findElement(By.id("initialPrice")).clear();
driver.findElement(By.id("initialPrice")).sendKeys(initialPrice);
driver.findElement(By.id("submitSiteChanges")).click();

就像我说的那样,在最后一个字段中输入文本的时间与单击它的时间之间,保存以前包含文本的字段将被清除,因此我的测试失败.问题是它并不总是发生.有时测试运行正常,而其他时候则没有.

Like I said, between the time it enters text in the last field and the time it clicks save the fields that previously had text in them get cleared out, and thus my test fails. The problem is it doesn't always happen. Sometimes the test runs fine, other times it doesn't.

我已经尝试过将Thread.sleep(x);在各处查看是否在某些时候暂停可以解决问题.我也尝试过使用javascript在后台等待可能正在运行的任何ajax.还具有driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS)的隐式等待.似乎没有任何改变.

I've tried putting Thread.sleep(x); all over the place to see if pausing at certain points would fix the problem. I also have tried using javascript to wait in the background for any ajax that might be running. Also have the implicit wait of driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS). None of it seemingly has made any difference.

我正在运行2.13版的硒服务器,我所有的测试都在Firefox 8上运行.

I'm running version 2.13 of selenium server and all my tests run on Firefox 8.

在此方面的任何帮助将不胜感激!

Any help on this would be greatly appreciated!

推荐答案

Firefox的错误,可防止在浏览器窗口失焦时执行某些事件.当您运行自动化测试时,这可能是一个问题-即使窗口没有聚焦,它也可能正在键入.

Firefox has a bug which prevents some events from being executed while the browser window is out of focus. This could be an issue when you're running your automation tests - which might be typing even if the window is out of focus.

关键是敲除模型更新是通过change事件触发的(默认情况下).如果未执行,则其基础模型不会是最新的.

The point is that knockout model updates are triggered (by default) with the change event. If it's not being executed, it's underlying model won't be up-to-date.

要解决此问题,我手动"触发了更改事件,将javascript注入了我的测试中.:

To fix this issue I triggered the change event "manually", injecting javascript into my tests.:

//suppose "element" is an input field
element.sendKeys("value");
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeScript("$(arguments[0]).change();", element);

您可能已经注意到,我正在使用jQuery触发change事件.如果您未在应用程序上使用jQuery,则可以在此处如何使用普通javascript触发它.

As you might have noticed, I'm using jQuery to trigger the change event. If you're not using jQuery on your app, you can check here how to trigger it using vanilla javascript.

希望对某人有帮助.

这篇关于selenium webdriver在sendKeys先前填充字段后清除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆