简体   繁体   中英

Capture user input and actions with Selenium WebDriver using Java

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?

ie when the user enters a URL, clicks a link, fills in a text box, clicks a button etc etc.

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.

I attempted to offer a viable solution in Record Actions using Selenium

Hope this helps.

You can't 'record' a set of actions with Selenium WebDriver, you will need to write those steps manually.

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.

Navigate to a 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:

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:

http://blog.josephwilk.net/cucumber/page-object-pattern.html

Hope that helps.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM