forked from angular/protractor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(elementexplorer): highlight elements
- Highlight elements by changing css - Use the rootEl default 'body'. On second attempt, use a default rootEl ''. After the attempt is made, reset the rootEl. closes angular#3465
- Loading branch information
Showing
3 changed files
with
178 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {promise, WebElement} from 'selenium-webdriver'; | ||
|
||
import {ProtractorBrowser} from '../../browser'; | ||
import {Locator} from '../../locators'; | ||
let absoluteXPath = require('./explorerXpath').absoluteXPath; | ||
|
||
export class ExplorerScripts { | ||
static list(locator: Locator) { | ||
(<any>global).tempRootEl = (<any>global).browser.rootEl; | ||
return (<any>global) | ||
.browser.findElements(locator) | ||
.then((arr: WebElement[]) => { | ||
let found: string[] = []; | ||
for (let i = 0; i < arr.length; ++i) { | ||
arr[i].getText().then((text: string) => { found.push(text); }); | ||
} | ||
return found; | ||
}) | ||
.catch((err: Error) => { throw err; }); | ||
} | ||
|
||
static highlight(locator: Locator) { | ||
let xPaths: string[] = []; | ||
let cssBorders: string[] = []; | ||
|
||
return (<any>global) | ||
.browser.findElements(locator) | ||
.then((arr: WebElement[]) => { | ||
|
||
for (let i = 0; i < arr.length; ++i) { | ||
ExplorerScripts.getAbsoluteXPath(arr[i]).then( | ||
(result: string) => { xPaths.push(result); }); | ||
(arr[i] as any).getCssValue('border').then((val: string) => { | ||
cssBorders.push(val); | ||
}); | ||
} | ||
if (arr.length == 0) { | ||
return 'No elements found.'; | ||
} | ||
|
||
}) | ||
.then(() => { | ||
|
||
let timesFlashed = 10; | ||
let waitTime = 500; | ||
for (let t = 0; t < timesFlashed; t++) { | ||
let borderValue = '5px dotted rgb(255, 55, 55)'; | ||
if (t % 2 == 1) { | ||
borderValue = '5px dotted rgb(255, 180, 55)'; | ||
} | ||
for (let i = 0; i < xPaths.length; i++) { | ||
(<any>global) | ||
.browser.executeScript( | ||
'var x = document.evaluate(\'' + xPaths[i] + | ||
'\', document, null, XPathResult.ANY_TYPE, null);' + | ||
'var xx = x.iterateNext(); xx.style.border = \'' + | ||
borderValue + '\';'); | ||
} | ||
|
||
global.browser.driver.sleep(waitTime); | ||
} | ||
|
||
}) | ||
.then(() => { | ||
|
||
for (let j = 0; j < cssBorders.length; j++) { | ||
(<any>global) | ||
.browser.executeScript( | ||
'var x = document.evaluate(\'' + xPaths[j] + | ||
'\', document, null, XPathResult.ANY_TYPE, null);' + | ||
'var xx = x.iterateNext(); xx.style.border = \'' + | ||
cssBorders[j] + '\';'); | ||
} | ||
return 'Highlighting Completed.'; | ||
|
||
}); | ||
}; | ||
|
||
|
||
static getAbsoluteXPath(element: WebElement): promise.Promise<any> { | ||
return (<any>global) | ||
.browser.executeScript( | ||
'var absoluteXPath = ' + | ||
Function.prototype.toString.call(absoluteXPath) + | ||
';\nreturn absoluteXPath(arguments[0]);', | ||
element); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
// jshint browser: true | ||
// jshint shadow: true | ||
exports.absoluteXPath = function(element) { | ||
var comp, comps = []; | ||
var xpath = ''; | ||
var getPos = function(element) { | ||
var position = 1, curNode; | ||
if (element.nodeType == Node.ATTRIBUTE_NODE) { | ||
return null; | ||
} | ||
for (curNode = element.previousSibling; curNode; curNode = curNode.previousSibling) { | ||
if (curNode.nodeName == element.nodeName) { | ||
++position; | ||
} | ||
} | ||
return position; | ||
}; | ||
|
||
if (element instanceof Document) { | ||
return '/'; | ||
} | ||
|
||
for (; element && !(element instanceof Document); | ||
element = element.nodeType == Node.ATTRIBUTE_NODE ? | ||
element.ownerElement : element.parentNode) { | ||
comp = comps[comps.length] = {}; | ||
switch (element.nodeType) { | ||
case Node.TEXT_NODE: | ||
comp.name = 'text()'; | ||
break; | ||
case Node.ATTRIBUTE_NODE: | ||
comp.name = '@' + element.nodeName; | ||
break; | ||
case Node.PROCESSING_INSTRUCTION_NODE: | ||
comp.name = 'processing-instruction()'; | ||
break; | ||
case Node.COMMENT_NODE: | ||
comp.name = 'comment()'; | ||
break; | ||
case Node.ELEMENT_NODE: | ||
comp.name = element.nodeName; | ||
break; | ||
} | ||
comp.position = getPos(element); | ||
} | ||
|
||
for (var i = comps.length - 1; i >= 0; i--) { | ||
comp = comps[i]; | ||
xpath += '/' + comp.name.toLowerCase(); | ||
if (comp.position !== null) { | ||
xpath += '[' + comp.position + ']'; | ||
} | ||
} | ||
return xpath; | ||
}; |