From 162c59cae4a91bbf926ca7fd8968bd1970d13d4e Mon Sep 17 00:00:00 2001 From: Savka Oleg Date: Mon, 16 Nov 2020 10:54:16 +0200 Subject: [PATCH] OI-1067 focus trick --- src/ox_modules/module-web/commands/click.js | 24 +++++++++++++++++++ .../module-web/commands/clickHidden.js | 20 +++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/ox_modules/module-web/commands/click.js b/src/ox_modules/module-web/commands/click.js index 6472cb7d..5cda9637 100644 --- a/src/ox_modules/module-web/commands/click.js +++ b/src/ox_modules/module-web/commands/click.js @@ -23,10 +23,15 @@ module.exports = async function(locator, timeout) { this.helpers.assertArgumentTimeout(timeout, 'timeout'); this.retryCount = 3; this.firstError = null; + + this.focusRetryCount = 3; + this.focusFirstError = null; + this.clickJS = async (el) => { try { /*global document*/ const retVal = await this.driver.execute(function(domEl) { + domEl.focus(); // createEvent won't be available in IE < 9 compatibility mode if (!document.createEvent) { if (document.createEventObject) { @@ -65,11 +70,30 @@ module.exports = async function(locator, timeout) { } }; + this.focus = async (el) => { + try { + await this.driver.execute(function(domEl) { + domEl.focus(); + }, el); + } catch (e) { + if (this.focusRetryCount) { + if (!this.focusFirstError) { + this.focusFirstError = e; + } + --this.focusRetryCount; + await this.focus(el); + } else { + throw this.focusFirstError; + } + } + }; + var el = await this.helpers.getElement(locator, false, timeout); var clickable = await el.isClickable(); if (clickable) { try { + await this.focus(el); await el.click(); } catch (e) { // chromedriver doesn't seem to support clicking on elements in Shadow DOM diff --git a/src/ox_modules/module-web/commands/clickHidden.js b/src/ox_modules/module-web/commands/clickHidden.js index 0d862920..4d69aa0a 100644 --- a/src/ox_modules/module-web/commands/clickHidden.js +++ b/src/ox_modules/module-web/commands/clickHidden.js @@ -17,19 +17,25 @@ * @example [javascript] Usage example * web.clickHidden("id=HiddenLink"); */ -module.exports = async function(locator, clickParent) { +module.exports = async function(locator, clickParent = false) { this.helpers.assertArgumentBoolOptional(clickParent, 'clickParent'); this.retryCount = 3; this.firstError = null; - this.clickJS = async (domEl, clickParent) => { + this.clickJS = async (domEl, clickParent = false) => { try { /*global document*/ - const retVal = await this.driver.execute(function(domEl) { + const retVal = await this.driver.execute(function(domEl, clickParent) { // createEvent won't be available in IE < 9 compatibility mode if (!document.createEvent) { if (document.createEventObject) { var ev = document.createEventObject(); - domEl.fireEvent('onclick', ev); + if (clickParent) { + domEl.parentElement.focus(); + domEl.parentElement.fireEvent('onclick', ev); + } else { + domEl.focus(); + domEl.fireEvent('onclick', ev); + } } else { return 'clickHidden is not supported on IE with compatibility mode "IE' + document.documentMode + ' ' + document.compatMode + '"'; @@ -38,11 +44,13 @@ module.exports = async function(locator, clickParent) { var clckEv = document.createEvent('MouseEvent'); clckEv.initEvent('click', true, true); if (clickParent) { + domEl.parentElement.focus(); domEl.parentElement.dispatchEvent(clckEv); } else { + domEl.focus(); domEl.dispatchEvent(clckEv); } - }, el); + }, el, clickParent); /* { @@ -63,7 +71,7 @@ module.exports = async function(locator, clickParent) { this.firstError = e; } --this.retryCount; - await this.clickJS(el, domEl); + await this.clickJS(el, clickParent); } else { throw this.firstError; }