Below is simple snippet of code.
@FindBy(className = \"element-to-press\")
private WebElement elementToPress;
// some other code
Keys move = Keys.ARROW_DOW
As discussed in chat you want to automate 2048
As a workaround I've written Javascript based on this to send Arrow KeyEvents.And It worked on all browsers
All you want to do is send arrow keys no need to click element at all [and find element ,unless you create a algorithm to play based on values)
You got me into this.The following will play game with 200(50 x 4) different moves for 3 browsers
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
public class Main {
public static void main(String args[]) {
play(new FirefoxDriver());
play(new SafariDriver());
System.setProperty("webdriver.chrome.driver", "/Users/Apple/Documents/chromedriver");
play(new ChromeDriver());
}
public static void play(WebDriver driver) {
driver.get("http://gabrielecirulli.github.io/2048");
driver.findElement(By.className("restart-button")).click();
String script = "fireKey(arguments[0]);\n"
+ "function fireKey(arrow)\n"
+ "{\n"
+ " var key;\n"
+ " switch (arrow.toLowerCase())\n"
+ " {\n"
+ " case \"left\":\n"
+ " key = 37;\n"
+ " break;\n"
+ " case \"right\":\n"
+ " key = 39;\n"
+ " break;\n"
+ " case \"up\":\n"
+ " key = 38;\n"
+ " break;\n"
+ " case \"down\":\n"
+ " key = 40;\n"
+ " break;\n"
+ " }\n"
+ " if (document.createEventObject)\n"
+ " {\n"
+ " var eventObj = document.createEventObject();\n"
+ " eventObj.keyCode = key;\n"
+ " document.documentElement.fireEvent(\"onkeydown\", eventObj);\n"
+ " } else if (document.createEvent)\n"
+ " {\n"
+ " var eventObj = document.createEvent(\"Events\");\n"
+ " eventObj.initEvent(\"keydown\", true, true);\n"
+ " eventObj.which = key;\n"
+ " document.documentElement.dispatchEvent(eventObj);\n"
+ " }\n"
+ "} ";
JavascriptExecutor js = (JavascriptExecutor) driver;
//use left,right,up,down for arrow keys
for (int i = 0; i < 50; i++) {
js.executeScript(script, "left");
js.executeScript(script, "right");
js.executeScript(script, "up");
js.executeScript(script, "down");
}
System.out.println("Score :" + driver.findElement(By.className("score-container")).getText());
driver.quit();
}
}
Update 2
Refer the above answer for fullcode
Update 1
SafariDriver Extension doesn't work with Selenium 2.46.I've tried it and you have to downgrade the Selenium to 2.45 as SafariDriver Extension for 2.46 is not released yet.Refer this GitHub Issue
So as of now go with Selenium 2.45
You have to install SafariDriver Extension Manually
As per SafariWiki
Starting with Selenium 2.45.0, you must manually install the SafariDriver browser extension. Simply open the latest copy of SafariDriver.safariextz in Safari and click the "install" button. Once installed, writing a test for Safari is just as straightforward as using the FirefoxDriver