1

I am trying to use Headless testing using HTMLUnitDriver library.

My code with keyboard keys events working fine with real browser but same code is not working when using HTMLUnitDriver headless testing.

I tried below option-

//Using Action

Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).build().perform();

// using Keyboard

Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();
keyboard.pressKey(Keys.ENTER);

// using sendkeys

driver.findElement(By.xpath(search)).sendKeys(Keys.ENTER);

With all the above code I not getting any error but also not performing any key event.

1

2 Answers 2

0

You can do it through Robot class in Java.awt API.

below is the code snippet you can use for opening a new tab in chrome. (Cntrl+T)

Robot robot = new Robot();
        try {
            robot = new Robot();
        } 
            catch (AWTException e) {
            e.printStackTrace();
        }
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_T);  
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_T);
1
  • Not working when chrome mode is --headless..
    – hrdkisback
    Commented Oct 27, 2018 at 11:07
0

You can use Robot object to work with Keyboard keys,as i have used the same in Browser Authentication to pass Username and password ,PFB

Robot rb = new Robot(); 
StringSelection user = new StringSelection("RajKuc");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(user, null);

//User which replace with entered user name //Copy Username data using
Ctrl+v as below `rb.keyPress(KeyEvent.VK_CONTROL);`

rb.keyPress(KeyEvent.VK_V);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.