forked from DevExpress/testcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node snapshot properties shorthands for Selector (closes DevExpress#771…
…) (DevExpress#899) * Node snapshot properties shorthands for Selector (closes DevExpress#771) * Fix review issues * Remove no-var eslint rule * Fix tests. Add yarn.lock to .gitignore
- Loading branch information
Showing
20 changed files
with
453 additions
and
196 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ site | |
.sass-cache | ||
.publish | ||
___test-screenshots___ | ||
yarn.lock |
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
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
64 changes: 64 additions & 0 deletions
64
src/client-functions/selector-builder/create-snapshot-property-shorthands.js
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,64 @@ | ||
import { ELEMENT_SNAPSHOT_PROPERTIES, NODE_SNAPSHOT_PROPERTIES } from './node-snapshot-properties'; | ||
import { CantObtainInfoForElementSpecifiedBySelectorError } from '../../errors/test-run'; | ||
import { getCallsite, getCallsiteForGetter } from '../../errors/callsite'; | ||
|
||
const SNAPSHOT_PROPERTIES = NODE_SNAPSHOT_PROPERTIES.concat(ELEMENT_SNAPSHOT_PROPERTIES); | ||
|
||
async function getSnapshot (selector, callsite) { | ||
var node = null; | ||
|
||
try { | ||
node = await selector(); | ||
} | ||
|
||
catch (err) { | ||
err.callsite = callsite; | ||
throw err; | ||
} | ||
|
||
if (!node) | ||
throw new CantObtainInfoForElementSpecifiedBySelectorError(callsite); | ||
|
||
return node; | ||
} | ||
|
||
export default function createSnapshotPropertyShorthands (obj, selector) { | ||
SNAPSHOT_PROPERTIES.forEach(prop => { | ||
Object.defineProperty(obj, prop, { | ||
get: async () => { | ||
var callsite = getCallsiteForGetter(); | ||
var snapshot = await getSnapshot(selector, callsite); | ||
|
||
return snapshot[prop]; | ||
} | ||
}); | ||
}); | ||
|
||
obj.getStyleProperty = async prop => { | ||
var callsite = getCallsite('getStyleProperty'); | ||
var snapshot = await getSnapshot(selector, callsite); | ||
|
||
return snapshot.style[prop]; | ||
}; | ||
|
||
obj.getAttribute = async attrName => { | ||
var callsite = getCallsite('getAttribute'); | ||
var snapshot = await getSnapshot(selector, callsite); | ||
|
||
return snapshot.attributes[attrName]; | ||
}; | ||
|
||
obj.getBoundingClientRectProperty = async prop => { | ||
var callsite = getCallsite('getBoundingClientRectProperty'); | ||
var snapshot = await getSnapshot(selector, callsite); | ||
|
||
return snapshot.boundingClientRect[prop]; | ||
}; | ||
|
||
obj.hasClass = async name => { | ||
var callsite = getCallsite('hasClass'); | ||
var snapshot = await getSnapshot(selector, callsite); | ||
|
||
return snapshot.classNames ? snapshot.classNames.indexOf(name) > -1 : false; | ||
}; | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/client-functions/selector-builder/node-snapshot-properties.js
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,41 @@ | ||
// ------------------------------------------------------------- | ||
// WARNING: this file is used by both the client and the server. | ||
// Do not use any browser or node-specific API! | ||
// ------------------------------------------------------------- | ||
|
||
export const NODE_SNAPSHOT_PROPERTIES = [ | ||
'nodeType', | ||
'textContent', | ||
'childNodeCount', | ||
'hasChildNodes', | ||
'childElementCount', | ||
'hasChildElements' | ||
]; | ||
|
||
export const ELEMENT_SNAPSHOT_PROPERTIES = [ | ||
'tagName', | ||
'visible', | ||
'focused', | ||
'attributes', | ||
'boundingClientRect', | ||
'classNames', | ||
'style', | ||
'innerText', | ||
'namespaceURI', | ||
'id', | ||
'value', | ||
'checked', | ||
'selected', | ||
'scrollWidth', | ||
'scrollHeight', | ||
'scrollLeft', | ||
'scrollTop', | ||
'offsetWidth', | ||
'offsetHeight', | ||
'offsetLeft', | ||
'offsetTop', | ||
'clientWidth', | ||
'clientHeight', | ||
'clientLeft', | ||
'clientTop' | ||
]; |
103 changes: 103 additions & 0 deletions
103
src/client/driver/command-executors/client-functions/selector-executor/filter.js
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,103 @@ | ||
import { InvalidSelectorResultError } from '../../../../../errors/test-run'; | ||
import { domUtils } from '../../../deps/testcafe-core'; | ||
import { getInnerText, getTextContent } from './sandboxed-node-properties'; | ||
|
||
// NOTE: save original ctors and methods because they may be overwritten by page code | ||
var isArray = Array.isArray; | ||
var Node = window.Node; | ||
var HTMLCollection = window.HTMLCollection; | ||
var NodeList = window.NodeList; | ||
|
||
function isArrayOfNodes (obj) { | ||
if (!isArray(obj)) | ||
return false; | ||
|
||
for (var i = 0; i < obj.length; i++) { | ||
if (!(obj[i] instanceof Node)) | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function hasText (node, textRe) { | ||
// Element | ||
if (node.nodeType === 1) { | ||
var text = getInnerText(node); | ||
|
||
// NOTE: In Firefox, <option> elements don't have `innerText`. | ||
// So, we fallback to `textContent` in that case (see GH-861). | ||
if (domUtils.isOptionElement(node)) { | ||
var textContent = getTextContent(node); | ||
|
||
if (!text && textContent) | ||
text = textContent; | ||
} | ||
|
||
return textRe.test(text); | ||
} | ||
|
||
// Document | ||
if (node.nodeType === 9) { | ||
// NOTE: latest version of Edge doesn't have `innerText` for `document`, | ||
// `html` and `body`. So we check their children instead. | ||
var head = node.querySelector('head'); | ||
var body = node.querySelector('body'); | ||
|
||
return hasChildrenWithText(head, textRe) || hasChildrenWithText(body, textRe); | ||
} | ||
|
||
// DocumentFragment | ||
if (node.nodeType === 11) | ||
return hasChildrenWithText(node, textRe); | ||
|
||
return textRe.test(getTextContent(node)); | ||
} | ||
|
||
function hasChildrenWithText (node, textRe) { | ||
var cnCount = node.childNodes.length; | ||
|
||
for (var i = 0; i < cnCount; i++) { | ||
if (hasText(node.childNodes[i], textRe)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function filterNodeCollectionByText (collection, textRe) { | ||
var count = collection.length; | ||
var filtered = []; | ||
|
||
for (var i = 0; i < count; i++) { | ||
if (hasText(collection[i], textRe)) | ||
filtered.push(collection[i]); | ||
} | ||
|
||
return filtered; | ||
} | ||
|
||
|
||
// Selector filter | ||
Object.defineProperty(window, '%testCafeSelectorFilter%', { | ||
value: (node, options) => { | ||
if (node === null || node === void 0) | ||
return node; | ||
|
||
if (node instanceof Node) { | ||
if (options.text) | ||
return hasText(node, options.text) ? node : null; | ||
|
||
return node; | ||
} | ||
|
||
if (node instanceof HTMLCollection || node instanceof NodeList || isArrayOfNodes(node)) { | ||
if (options.text) | ||
node = filterNodeCollectionByText(node, options.text); | ||
|
||
return node[options.index]; | ||
} | ||
|
||
throw new InvalidSelectorResultError(); | ||
} | ||
}); |
Oops, something went wrong.