使用 Java 使用 Selenium WebDriver 捕获用户输入和操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13253322/
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
Capture user input and actions with Selenium WebDriver using Java
提问by Dave
Is it possible to capture user input/actions with Selenium WebDriver, in the same way that you can use the Selenium IDE for recording / creating tests?
是否可以使用 Selenium WebDriver 捕获用户输入/操作,就像使用 Selenium IDE 记录/创建测试一样?
i.e. when the user enters a URL, clicks a link, fills in a text box, clicks a button etc etc.
即当用户输入 URL、点击链接、填写文本框、点击按钮等时。
I'd like to be able to capture these actions using the WebDriver rather than just using the Selenium IDE, as I want to integrate with other classes available in my Java application.
我希望能够使用 WebDriver 而不是仅使用 Selenium IDE 来捕获这些操作,因为我想与我的 Java 应用程序中可用的其他类集成。
回答by sep
I attempted to offer a viable solution in Record Actions using Selenium
我尝试使用 Selenium在Record Actions 中提供可行的解决方案
Hope this helps.
希望这可以帮助。
回答by Nickel
You can't 'record' a set of actions with Selenium WebDriver, you will need to write those steps manually.
您无法使用 Selenium WebDriver“记录”一组操作,您需要手动编写这些步骤。
Strictly speaking you can capture user input by using the WebDriver API in your chosen language (C#, Java, PHP, Ruby. Python, Perl or JavaScript
) and it vaguely resembles using the DOM. If it suits your requirements you could use configuration files to supply some of your user input.
严格来说,您可以使用所选语言 ( C#, Java, PHP, Ruby. Python, Perl or JavaScript
) 中的 WebDriver API 来捕获用户输入,它与使用 DOM 有点相似。如果它符合您的要求,您可以使用配置文件来提供一些用户输入。
Navigate to a URL:
导航到一个 URL:
WebDriver driver = new FirefoxDriver();
driver.get('url')
Click a link/button:
单击链接/按钮:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
element.click();
Enter Text in a field:
在字段中输入文本:
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
element.sendKeys('userinput');
For more information on the API Selenium HQ is pretty definitive:
有关 API Selenium HQ 的更多信息非常明确:
http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
http://seleniumhq.org/docs/03_webdriver.html#introducing-the-selenium-webdriver-api-by-example
If you're going from Selenium IDE
to writing tests it'd be really useful to check out the page object pattern as I've found it makes your tests more maintainable in the long-run. This link is a good starting point because it gives an overview, and a visual representation of what you get by following the pattern:
如果您要开始Selenium IDE
编写测试,检查页面对象模式会非常有用,因为我发现它使您的测试在长期内更易于维护。此链接是一个很好的起点,因为它提供了概述,并直观地表示了遵循该模式所获得的内容:
http://blog.josephwilk.net/cucumber/page-object-pattern.html
http://blog.josephwilk.net/cucumber/page-object-pattern.html
Hope that helps.
希望有帮助。
回答by Akcipitrokulo
As far as I'm aware, there isn't an easy way to do it - but recording on IDE and exporting as a java file has worked well for me (File -> Export test case as...). I usually do it to c# but have used it with java.
据我所知,没有一种简单的方法可以做到这一点 - 但是在 IDE 上录制并导出为 java 文件对我来说效果很好(文件 - > 将测试用例导出为...)。我通常在 c# 中使用它,但在 java 中使用过它。