Java 硒可以处理自动完成吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/663034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can selenium handle autocomplete?
提问by Afamee
I have a test case that requires typing in a partial value into an ajax based textfield and verifying the list has the expected content. If it does, select the content. Any idea how to make this work?
我有一个测试用例,需要在基于 ajax 的文本字段中输入部分值并验证列表是否具有预期的内容。如果是,请选择内容。知道如何进行这项工作吗?
采纳答案by Patrick Lightbody
The typecommand may not be enough to trigger the autocomplete. Dave Webb's suggestions are otherwise spot on. My only addition would be that you might need the typeKeyscommand, which causes slightly different JavaScript events to be fired, which may be more likely to trigger the autocomplete widget.
该类型命令可能不足以触发自动完成功能。戴夫·韦伯 (Dave Webb) 的建议是正确的。我唯一的补充是您可能需要typeKeys命令,这会导致触发稍微不同的 JavaScript 事件,这可能更有可能触发自动完成小部件。
回答by krosenvold
Your question is slightly ambigious.
你的问题有点模棱两可。
Most browsers keep a value cache that is based on the name of the field: This is the value that is being suggested as autocompletion by your browser even though you may never have visited the site before. This feature is non-standard across all browsers, and there's going to be no standard way for selenium to detect/analyze this. You can still do it, but you'll have to make javascript functions that determine the values yourself. Then you can use "eval" in selenium to execute these functions. I have not seen any js libraries that can tell you these values in a cross-browser compatible way.
大多数浏览器都会保留一个基于字段名称的值缓存:即使您之前可能从未访问过该站点,浏览器也会建议您自动完成该值。此功能在所有浏览器中都是非标准的,并且 selenium 将没有标准的方法来检测/分析它。您仍然可以这样做,但您必须制作自己确定值的 javascript 函数。然后你可以在 selenium 中使用“eval”来执行这些函数。我还没有看到任何 js 库可以以跨浏览器兼容的方式告诉您这些值。
The other alternative is that you use ajax to do a server-side submit of the partially entered value. In this case it's just a matter of typing the values into the textbox and asserting that the expected values turn up. Normally the autocomplete suggestions show up in some layer on the client side.
另一种选择是使用 ajax 对部分输入的值进行服务器端提交。在这种情况下,只需将值键入文本框中并断言预期值出现即可。通常,自动完成建议显示在客户端的某个层中。
回答by Dave Webb
I'd do this as follows:
我会这样做:
type
to enter the value in the text field.waitForTextPresent
orverifyTextPresent
to check the autocomplete contentclick
ormouseDown
to click on the item in the autocomplete list
type
在文本字段中输入值。waitForTextPresent
或verifyTextPresent
检查自动完成内容click
或mouseDown
单击自动完成列表中的项目
The trick is going to be making the final click
be just in the right place. You should be able to use an XPath expression that searches for the text you're expecting to find it.
诀窍是让决赛click
恰到好处。您应该能够使用 XPath 表达式来搜索您希望找到的文本。
回答by Patrick Lightbody
Please use typeKeys instead of type. Also use mouseDown instead of click. It works fine.
请使用 typeKeys 而不是 type。也可以使用 mouseDown 而不是 click。它工作正常。
回答by johnnymire
I found I needed to do a focuson the field before doing typeKeysto get it to work.
我发现我需要在做typeKeys之前专注于该领域才能使其工作。
回答by Jason Buberel
I recently wrote a HOWTO on this very topic - using Selenium to test an AJAX-driven JQuery autocomplete menu:
我最近写了一篇关于这个主题的 HOWTO -使用 Selenium 测试 AJAX 驱动的 JQuery 自动完成菜单:
回答by Santi
Patrick's answer is definitely important, I also found that focus and mouseDown is needed in the last versions of Jquery UI. I recorded a video of a test so that you can see it running in Sauce Labs: https://saucelabs.com/jobs/ad8c561be39bb7a42c9bb3a063214c95
帕特里克的回答绝对重要,我还发现在最新版本的 Jquery UI 中需要 focus 和 mouseDown。我录制了一个测试视频,以便您可以在 Sauce Labs 中看到它的运行情况:https: //saucelabs.com/jobs/ad8c561be39bb7a42c9bb3a063214c95
回答by Thiyagarajan Veluchamy
I used following sequence in IDE,
我在IDE中使用了以下序列,
- typeKeys
- waitForTextPresent
- mouseOver
- clickAt
- 类型键
- 等待文本呈现
- 鼠标移到
- 点击
and worked well
并且运作良好
回答by Prashanth Sams
For WebDriver, try this
对于WebDriver,试试这个
The below code is for searching a text automatically from the auto suggest; mainly for a list item.
以下代码用于从自动建议中自动搜索文本;主要用于列表项。
driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword");
Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator"));
listItems.get(0).click();
driver.findElement(By.id("your searchButton")).click();
回答by Kees van Dieren
We had some problems with typeKeys. sendKeys seems to become the final solution, but it is still experimental. From the reference:
我们在使用 typeKeys 时遇到了一些问题。sendKeys 似乎成为最终的解决方案,但它仍然是实验性的。从参考:
This command is experimental. It may replace the typeKeys command in the future.
For those who are interested in the details, unlike the typeKeys command, which tries to fire the keyDown, the keyUp and the keyPress events, this command is backed by the atoms from Selenium 2 and provides a much more robust implementation that will be maintained in the future.
此命令是实验性的。将来它可能会取代 typeKeys 命令。
对于那些对细节感兴趣的人,与 typeKeys 命令不同,它试图触发 keyDown、keyUp 和 keyPress 事件,这个命令由 Selenium 2 的原子支持,并提供了一个更健壮的实现,将在未来。