0

Below is simple snippet of code.

@FindBy(className = "element-to-press")
private WebElement elementToPress;

// some other code

Keys move = Keys.ARROW_DOWN;
gridContainer.sendKeys(move);

In Firefox everything works fine.

In Chrome I've got "org.openqa.selenium.WebDriverException: unknown error: cannot focus element" error

Safari does not react for sendKeys()

Also I've tried to use another approach:

Keys move = Keys.ARROW_DOWN;
Actions actions = new Actions(driver);
actions.moveToElement(element).click(); // to focus on element
actions.sendKeys(move).perform();

This code fixed Chrome "cannot focus element" error, but browser still does not react for Keys.ARROW_DOWN/Keys.ARROW_UP/Keys.ARROW_LEFT/Keys.ARROW_RIGHT events

Safari and Firefox do not react for sendKeys() as well.

I cannot understand what is going on and how I can make my code run for all this browsers.

Environment:

  • OS X 10.9.5

  • Latest version of Chrome/Safari/Firefox

  • Selenium version 2.44.0

UPD:

Update Selenium to version 2.46.

Works for Chrome and Firefox now.

But still have an issue with Safari:org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session.

UPD2:

I have Safari browser extension installed. enter image description here

art_tykh
  • 169
  • 1
  • 17
  • 1
    Use Selenium 2.46 and chromedriver 2.16 [from here](http://docs.seleniumhq.org/download/) – Madhan Jul 22 '15 at 08:06
  • Update Selenium to version 2.46. Works for Chrome and Firefox now. But still have an issue with Safari. – art_tykh Jul 22 '15 at 09:42
  • Have you installed the [Safari Driver Extension](http://selenium-release.storage.googleapis.com/2.45/SafariDriver.safariextz) Read abt that [here](https://github.com/SeleniumHQ/selenium/wiki/SafariDriver#getting-started) – Madhan Jul 22 '15 at 09:44

2 Answers2

2

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


Initial Answer

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

Community
  • 1
  • 1
Madhan
  • 5,750
  • 4
  • 28
  • 61
  • I have SafariDriver Extension installed. The problem is that driver could not start a new session: org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. – art_tykh Jul 22 '15 at 09:53
  • @art_tykh Updated the answer check it – Madhan Jul 22 '15 at 10:02
  • Downgrading to Selenium 2.45 solved the issue with browser launch. But Safari still does not react for sendKeys() events. Do you know what is the best way to send "Keys.ARROW_RIGHT" for SafariDriver ? – art_tykh Jul 22 '15 at 10:21
  • @art_tykh You want to sendKeys to element or to driver? – Madhan Jul 22 '15 at 10:36
  • I want to sendKeys to element. For **FirefoxDriver** I do it via: `element.sendKeys(key)`; For **ChromeDriver** via: `Actions actions = new Actions(driver()); actions.moveToElement(element).click(); actions.sendKeys(key).perform();` For **SafariDriver** - none of above is working – art_tykh Jul 22 '15 at 10:50
  • Instead of action For all do like this: `element.click();element.sendKeys(key);` – Madhan Jul 22 '15 at 10:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83960/discussion-between-art-tykh-and-madhan). – art_tykh Jul 22 '15 at 10:55
  • @art_tykh I've posted the code just use this and tell whether it is working or not – Madhan Jul 22 '15 at 13:08
1

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();
}
}
Community
  • 1
  • 1
Madhan
  • 5,750
  • 4
  • 28
  • 61