Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record Actions using Selenium

I have a semi-vague question to ask about Selenium. I've discovered a few different ways to perform actions using the FirefoxDriver. What I need to do is repeat actions that a user performs on a web page (clicking a link, checking a checkbox, etc.). Is there any method or combination of methods that allows me to "record" the user's actions? Here is what I have so far to perform actions (you'll notice I've tried using the WebDriverBackedSelenium and Actions classes to perform actions)

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class MyReplayer {
    public static void main(String[] args) throws Exception {
        // The Firefox driver supports javascript 
        FirefoxDriver driver = new FirefoxDriver();

        driver.get("http://www.cs.umd.edu");

        List<WebElement> elements = driver.findElements(By.tagName("a"));
        //WebDriverBackedSelenium driverBacked = new WebDriverBackedSelenium(driver,        "http://www.cs.umd.edu");
        Actions builder = new Actions(driver);    
        Action clickLink = builder.click(elements.get(100)).build();
        clickLink.perform();
        //driverBacked.click("document.getElementsByTagName('a')[100]");
     }
}
like image 823
user1294114 Avatar asked Mar 26 '12 22:03

user1294114


2 Answers

I came across Huxley. It allows recording and playback of user actions. I found this question in search of how they did it, but had to resort to source code.

Lines 98-154 of huxley/run.py define the record function. It uses webdirvier to execute some js on the page which adds some event listeners. It also adds a function to return the events.

(function() {
var events = [];
window.addEventListener('click', function (e) { events.push([Date.now(), 'click',  [e.clientX, e.clientY]]); }, true);
window.addEventListener('keyup', function (e) { events.push([Date.now(), 'keyup', String.fromCharCode(e.keyCode)]); }, true);
window._getHuxleyEvents = function() { return events; };
})();

To read the events the js function is called

events = d.execute_script('return window._getHuxleyEvents();')

Then the events are stored in a way that seems application specific.

Sorry, I do not have Java code. I hope this helps.

like image 116
sep Avatar answered Nov 16 '22 02:11

sep


You can use the Selenium IDE Addon for Firefox and export the generated test for Webdriver. It doesn't specifically say FirefoxDriver, but the methods of the interface look similar to what you posted. I hope this helps.

like image 24
ma cılay Avatar answered Nov 16 '22 03:11

ma cılay