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

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

问题描述

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

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 版的 selenium 服务器,我的所有测试都在 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 来触发更改事件.如果您没有在您的应用程序上使用 jQuery,您可以查看此处 如何使用 vanilla 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.

希望对某人有所帮助.

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

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