MarkusBernhardt / robotframework-selenium2library-java

Java port of the Selenium 2 (WebDriver) Python library for Robot Framework
Apache License 2.0
46 stars 51 forks source link

Keydown, keyup for keys like ctrl, alt, etc... #81

Closed ghost closed 8 years ago

ghost commented 8 years ago

I haven't found a way to invoke clicks with modifier keys pressed with the library. With Selenium actions it is possible to fire a click while ctrl button is down for example to trigger specific events on a site. (It's not about opening links in new tab)

I wonder if it is possible to add support for pressing (and holding) special keys, do the click, then release them.

I am willing to contribute if you are okay with the idea

MarkusBernhardt commented 8 years ago

Hi, I'm surely ok with that idea, but to be honest I don't have very much time at the moment. Perhaps you should also ask the RobotFramework user list before. Perhaps this can already be done somehow.

Cheers, Markus

ghost commented 8 years ago

Thanks for the quick response, at first we are going to implement in in our keyword library depending on Selenium2Library, and if it looks good I'll get back to you with it, probably with a pull request or something. Checked out a few alternative ways, but could't make it, so I guess it is time to try with accessing the webdriver.

ghost commented 8 years ago

A colleague of mine created a keyword for it, here it comes.

    /**
     * Click on element identified by locator with modifier key.
     *
     * @param locator xpath expression
     * @param modifierKey key constant defined in org.openqa.selenium.Keys
     */
    @RobotKeyword("Click on element identified by locator with modifier key.")
    @ArgumentNames({"locator=", "modifierKey="})
    public void Click_Element_With_Modifier(String locator, String modifierKey) throws ScriptException {
        WebDriver webDriver = Selenium2Library.getLibraryInstance().getBrowserManagement().getCurrentWebDriver();
        Keys key = Keys.valueOf(modifierKey);
        WebElement element = webDriver.findElement(By.xpath(locator));
        new Actions(webDriver)
            .keyDown(key)
            .click(element)
            .keyUp(key)
            .build()
            .perform();
    }

Since it is Java, and would affect only the users using the Robot Selenium combo with Java through your library, maybe it would be a good idea to let the Selenium2Library devs know about this, and maybe they also can add their implementation.

Should I do a pull request with it, or you will add it, or I should just close this issue since there is a "workaround" provided. I am not sure how general the need for this way of clicking around.

ghost commented 8 years ago

Well, not much activity here, with the workaround we are happy, so I am closing this.