right click in protractor

2020-04-16 18:30发布

问题:

I'm trying to right click an element using protractor, the element is a cell in an ag-grid.

I'm trying to use some of the earlier suggestions that I could find, the only one that didn't throw me an error was the following:

browser.actions().mouseMove(elementVar).perform();
browser.actions().click(protractor.Button.RIGHT).perform();

although it doesn't right-click at all.

Any suggestions?

回答1:

From the webdriverJs api, you can right click an element thusly:

browser.actions()
    .click($('.myElm'), protractor.Button.RIGHT)
    .perform();


回答2:

Found an answer for that one. I was really close, but for the ones to look for this question the solution is this:

the elementVar you are passing to the mouseMove function, should be the desired element location. What does it mean? For example lets say we have an element called 'el' that we want to right click on it, our code needs to be like:

async function rightClick (el) {
    loc = el.getLocation();    //get the location of the element we want to click
    await browser.actions().mouseMove(loc).perform();   //takes the mouse to hover the element
    await browser.actions().click(protractor.Button.RIGHT).perform();    //performs the right click
};


回答3:

Building on @Brine's answer, I managed to work out a method without the mousemove stuff or the Jquery.

import { protractor } from 'protractor/built/ptor';

const ele = element(by.id('my_element');
browser.actions().click(ele, protractor.Button.RIGHT).perform();


回答4:

Quite sure is not a clean solution but all the script above are ok if you are using a headless chrome. If you are using a normal chrome ant the above solutions are not working, give a try at this workaround:

        await browser.actions().mouseMove(elementFinder).mouseDown().mouseMove(elementFinder).perform();