Upgrading to Selenium 4. Can't do Control + click in nightwatch

51 Views Asked by At

I'm hoping someone here can help me with this issue that I'm having with automated testing.

I am testing with Nightwatch.

I am upgrading to Selenium 4. Everything is working except for one issue. One of the things I do in my scripts is I do control click and shift click. I need to have a have a new tab or new window for my testing.

Until now (in Selenium 3) I would code like this: client.keys(client.Keys.SHIFT) or client.keys(client.Keys.CONTROL) and then I would do the click command. This would open the link in a new tab or window. I also tried just doing Keys.CONTROL (omitting the word client) it didn't seem to make a difference.

Since I upgraded it doesn't open in a new tab. The link opens but ignores the fact that I did CONTROL click.

I tried many different combinations using either webdriver or selenium-webdriver as shown.

const driver1 = require("webdriver");
const driver2 = require('selenium-webdriver')

driver1.actions().keyDown(Keys.SHIFT).perform();
I get this error:   driver1.actions is not a function


driver1.actions.keyDown(Keys.SHIFT).perform();
I get this error:   Cannot read properties of undefined (reading 'keyDown')


driver2.Actions.keyDown(client.Keys.CONTROL).perform()
I get this error:   Cannot read properties of undefined (reading 'keyDown')
client.keys(client.Keys.CONTROL) or client.keys(Keys.CONTROL) 
then click: ignores the CONTROL and just clicks 

Any tips would be greatly appreciated!!

Thanks!!!

1

There are 1 best solutions below

1
AutomatedTester On

You need to do the following in Selenium

await driver.actions()
        .keyDown(Key.SHIFT)
        .sendKeys('a')
        .perform()

And in NightwatchJS you would do the follow

describe('example with user actions api', function () {
  
  before(browser => browser.navigateTo('https://nightwatchjs.org'));
  
  it('demo test', async function (browser) {
    // retrieve the element; the actions api requires Selenium WebElement objects,
    //  which can be retrieved using the global element() utility
    const btnElement = await element('a.btn-github').findElement();
    
    await browser.perform(function() {
      // initiate the actions chain
      const actions = this.actions({async: true});
      
      return actions
        .<insert commands>
    });
  });
});