-
Notifications
You must be signed in to change notification settings - Fork 675
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
Node snapshot properties shorthands for Selector (closes #771) #899
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yarn.lock should be in the repo:
https://yarnpkg.com/en/docs/migrating-from-npm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good approach if you use it for end user application, which can't be a dependency itself or shipped with dependencies included. See ongoing discussion in yarnpkg/yarn#1067. The issue with checking in a lock file is that we'll not have the same configuration that our end users have. E.g. we use lock file and always have a fixed set of dependencies. Everything works fine for us. Meanwhile, our end user may use
npm
to install testcafe and they'll end up with another set of dependencies, which may broke functionality for them. And we'll be not even aware about it.