Skip to content

Implemented right click for WebDriver, Nightmare #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/webapi/rightClick.mustache
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Performs right click on an element matched by CSS or XPath.
Performs right click on a clickable element matched by semantic locator, CSS or XPath.

@param locator element located by CSS|XPath|strict locator.
```js
// right click element with id el
I.rightClick('#el');
// right click link or button with text "Click me"
I.rightClick('Click me');
// right click button with text "Click me" inside .context
I.rightClick('Click me', '.context');
```

@param locator clickable element located by CSS|XPath|strict locator.
@param context (optional) element located by CSS|XPath|strict locator.
12 changes: 12 additions & 0 deletions lib/helper/Nightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,18 @@ class Nightmare extends Helper {
.wait(this.options.waitForAction);
}


/**
* {{> ../webapi/rightClick }}
*/
async rightClick(locator, context = null) {
const el = await findClickable.call(this, locator, context);
assertElementExists(el, locator, 'Clickable');
return this.browser.evaluate(el => window.codeceptjs.rightClickEl(el), el)
.wait(this.options.waitForAction);
}


/**
* {{> ../webapi/moveCursorTo }}
*/
Expand Down
11 changes: 7 additions & 4 deletions lib/helper/Protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,13 @@ class Protractor extends Helper {
if (locator === undefined) {
return this.browser.actions().click(Button.RIGHT).perform();
}

const els = await this._locate(locator, true);
assertElementExists(els);
const el = els[0];
let matcher = this.browser;
if (context) {
const els = await this._locate(context, true);
assertElementExists(els, context);
matcher = els[0];
}
const el = await findClickable.call(this, matcher, locator);

await this.browser.actions().mouseMove(el).perform();
return this.browser.actions().click(Button.RIGHT).perform();
Expand Down
35 changes: 22 additions & 13 deletions lib/helper/WebDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,19 +643,28 @@ class WebDriver extends Helper {
*
* * *Appium*: supported, but in apps works as usual click
*/
async rightClick(locator) {
// just press button if no selector is given
if (locator === undefined) {
return this.browser.buttonDown('right');
async rightClick(locator, context) {
const locateFn = prepareLocateFn.call(this, context);

const res = await findClickable.call(this, locator, locateFn);
if (context) {
assertElementExists(res, locator, 'Clickable element', `was not found inside element ${new Locator(context)}`);
} else {
assertElementExists(res, locator, 'Clickable element');
}

const res = await this._locate(locator, true);
assertElementExists(res, locator, 'Clickable element');
const elem = usingFirstElement(res);
const elementId = getElementId(elem);
if (this.browser.isMobile) return this.browser.touchClick(elementId);
await this.browser.moveTo(elementId);
this.browser.buttonDown('right');
const el = usingFirstElement(res);

await el.moveTo();

if (this.browser.isW3C) {
// W3C version
return this.browser.performActions([
{ type: 'pointerDown', button: 2 },
]);
}
// JSON Wire version
await this.browser.buttonDown(2);
}

/**
Expand Down Expand Up @@ -2262,9 +2271,9 @@ function prepareLocateFn(context) {
return (l) => {
l = new Locator(l, 'css');
if (el) return this.browser.findElementsFromElement(el, l.type, l.value);
return this._locate(context, true).then((res) => {
return this._locate(context, true).then(async (res) => {
assertElementExists(res, context, 'Context element');
return this.browser.findElementsFromElement(el = getElementId(res[0]), l.type, l.value);
return res[0].$$(l.simplify());
});
};
}
Expand Down
12 changes: 12 additions & 0 deletions lib/helper/clientscripts/nightmare.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ if (!window.codeceptjs) {
this.fetchElement(el).dispatchEvent(event);
};


codeceptjs.rightClickEl = function (el) {
const event = new MouseEvent('contextmenu', {
bubbles: true,
cancelable: true,
view: window,
buttons: 2,
});

this.fetchElement(el).dispatchEvent(event);
};

codeceptjs.checkEl = function (el) {
const element = this.fetchElement(el);
const event = document.createEvent('HTMLEvents');
Expand Down
24 changes: 24 additions & 0 deletions test/data/app/view/form/rightclick.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<script>
function printContext() {
document.getElementById('output').innerHTML = 'right clicked';
}
</script>
</head>
<body>

<p class="context">
<a oncontextmenu="javascript:printContext();return false;">
Lorem Ipsum
</a>
</p>

<div id="output">
</div>




</body>
</html>
24 changes: 24 additions & 0 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,30 @@ module.exports.tests = function () {
});
});

describe('#rightClick', () => {
it('it should rightClick', function* () {
yield I.amOnPage('/form/rightclick');
yield I.dontSee('right clicked');
yield I.rightClick('Lorem Ipsum');
return I.see('right clicked');
});

it('it should rightClick by locator', function* () {
yield I.amOnPage('/form/rightclick');
yield I.dontSee('right clicked');
yield I.rightClick('.context a');
return I.see('right clicked');
});

it('it should rightClick by locator and context', function* () {
yield I.amOnPage('/form/rightclick');
yield I.dontSee('right clicked');
yield I.rightClick('Lorem Ipsum', '.context');
return I.see('right clicked');
});
});


describe('#checkOption', () => {
it('should check option by css', function* () {
yield I.amOnPage('/form/checkbox');
Expand Down