Selecting translate option from right click menu i

2019-06-06 23:11发布

问题:

I am trying to translate the page to English by clicking on translate option from right click browser menu. So far, my code is doing the right click, but I am not able to select the translate option.

Code:

console.log('Then I should right click on screen')
browser.sleep(3000);
browser.actions().mouseMove(element(by.id('search-icon'))).perform();
browser.actions().click(protractor.Button.RIGHT).perform().then(function () {
    console.log('Is right click performed ?');

回答1:

Right Click and clicking on the translate on the browser generated menu is an event that protractor/selenium doesn't support. Instead if you know the position of the translate option on the menu generated by right click, then you can use arrow keys to navigate to it and then press enter or return key. Here's how -

Suppose if the translate option is 3rd in the menu, then send the ARROW_DOWN key thrice -

browser.actions().mouseMove(element(by.id('search-icon'))).perform();
browser.actions().click(protractor.Button.RIGHT).sendKeys(protractor.Key.ARROW_DOWN).sendKeys(protractor.Key.ARROW_DOWN).sendKeys(protractor.Key.ARROW_DOWN).sendKeys(protractor.Key.RETURN).perform().then(function () {
    browser.sleep(3000);
    console.log('Clicked on translate');
});

Hope it helps