Perform shortcut in selenide

63 views Asked by At

I'm trying to perform shortcut cmd (ctrl) + shift + u in selenide to open WCAG extension. However chromium ignores it totally. If I leave window open, I have to fiddle with it (eg. minimize + maximize) to be able to pass that shortcut even manually. Eventually it works, but just manually. I've also tried other shortcuts like copy & paste from text field to another one...

Anyone with similar problem? how did you avoid it?

Link to github repository: https://github.com/1azyman/selenide-wcag-sample

Sample code:

package com.example;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverRunner;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import java.io.File;

import static com.codeborne.selenide.Selenide.open;

public class Main {

    public static void main(String[] args) throws Exception {
        Configuration.baseUrl = "https://google.com";

        // wave extension
        // https://chrome.google.com/webstore/detail/wave-evaluation-tool/jbbplnpkjmmeebjpijfedlgcdilocofh
        Configuration.browserCapabilities = new ChromeOptions()
                .addExtensions(new File("src/main/resources/wave.crx"));

        open("/");

        WebDriver driver = WebDriverRunner.getWebDriver();

        Keys cmdCtrl = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

        // shortcut to open wave (cmd+shift+u)
//        String wave = Keys.chord(cmdCtrl, Keys.SHIFT, "u");
//        new Actions(driver)
//                .sendKeys(wave)
//                .perform();

        new Actions(driver)
                .keyDown(cmdCtrl)
                .keyDown(Keys.SHIFT)
                .sendKeys("u")
                .keyUp(Keys.SHIFT)
                .keyUp(cmdCtrl)
                .perform();

        Thread.sleep(20_000L);
    }
}
1

There are 1 answers

1
Yaroslavm On

Selenide actions doesn't equal native user keypresses. So, not all actions (especially related to extension shortcuts) would work as expected.

However, as a workaround you can use Robot class with Selenide. Be aware that it's execution will intercept your current focus, as far as it executes shortcut in OS System context.

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

// your imports and code that opening website
int cmdCtrl = Platform.getCurrent().is(Platform.MAC) ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;

Robot robot = new Robot();
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); // this performs focus to opened browser window
robot.keyPress(cmdCtrl);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_U);

robot.keyRelease(cmdCtrl);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_META);