Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

Commit

Permalink
fix: it now also supports XPATH for WebElements
Browse files Browse the repository at this point in the history
  • Loading branch information
wswebcreation committed Mar 1, 2020
1 parent 53917a0 commit c306763
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions lib/clientSideScripts/hideRemoveElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,56 @@ export default function hideRemoveElements(
});

function hideRemoveEl(el: HTMLElement | WebElement, prop: string, hideRemove: boolean, singleElement: boolean = false) {
const value = prop === 'visibility' ? 'hidden' : 'none';
try {
// Here we get the HTMLElement, if this fails we have a WebElement
// @ts-ignore
if (el.style) {
// Here we get the HTMLElement
// @ts-ignore
el.style[prop] = hideRemove ? value : '';
} catch (e) {
/// Here we have the WebElement
if (singleElement) {
setPropertyToElement(el, prop, hideRemove);
} else {
// Here we have the WebElement, with the web element we can have 2 types of selectors
// css and xpath, transform them into HTML
// This is an anti pattern, but I don't know how to do this better with XPATH selection
try {
if (singleElement) {
// @ts-ignore
return setPropertyToElement(document.querySelector(el.selector), prop, hideRemove);
}

// @ts-ignore
return document.querySelectorAll(el.selector).forEach(singleEl =>
setPropertyToElement(singleEl, prop, hideRemove)
);
} catch (e) {
// 99.99% sure that we have XPATH here
// @ts-ignore
return document.querySelector(el.selector).style[prop] = hideRemove ? value : '';
return getElementsByXpath(el.selector).forEach(singleEl =>
setPropertyToElement(singleEl, prop, hideRemove)
);
}
}
}

// @ts-ignore
return document.querySelectorAll(el.selector).forEach(singleEl => singleEl.style[prop] = hideRemove ? value : '');
function setPropertyToElement(el: HTMLElement, prop: string, hideRemove: boolean) {
const value = prop === 'visibility' ? 'hidden' : 'none';
// @ts-ignore
el.style[prop] = hideRemove ? value : '';
}

// Stupid TypeScript =)
function getElementsByXpath(xpathToExecute: string): any[] {
const result = [];
const nodesSnapshot = document.evaluate(
xpathToExecute,
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null,
);

for (let i = 0; i < nodesSnapshot.snapshotLength; i++) {
result.push(nodesSnapshot.snapshotItem(i));
}

return result;
}
}

0 comments on commit c306763

Please sign in to comment.