Skip to content

fix: css to xpath backward compatibility #4141

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

Merged
merged 4 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/data/output
lib/css2xpath/*
17 changes: 13 additions & 4 deletions lib/locator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cssToXPath = require('csstoxpath');
let cssToXPath;
const { sprintf } = require('sprintf-js');

const { xpathLocator } = require('./utils');
Expand Down Expand Up @@ -162,8 +162,17 @@ class Locator {
* @returns {string}
*/
toXPath(pseudoSelector = '') {
const locator = `${this.value}${pseudoSelector}`;
const limitation = [':nth-of-type', ':first-of-type', ':last-of-type', ':nth-last-child', ':nth-last-of-type', ':checked', ':disabled', ':enabled', ':required', ':lang'];

if (limitation.some(item => locator.includes(item))) {
cssToXPath = require('css-to-xpath');
} else {
cssToXPath = require('csstoxpath');
}

if (this.isXPath()) return this.value;
if (this.isCSS()) return cssToXPath(`${this.value}${pseudoSelector}`);
if (this.isCSS()) return cssToXPath(locator);

throw new Error('Can\'t be converted to XPath');
}
Expand Down Expand Up @@ -250,7 +259,7 @@ class Locator {
*/
withText(text) {
text = xpathLocator.literal(text);
const xpath = this.toXPath(`:text-contains-case(${text})`);
const xpath = sprintf('%s[%s]', this.toXPath(), `contains(., ${text})`);
return new Locator({ xpath });
}

Expand All @@ -261,7 +270,7 @@ class Locator {
*/
withTextEquals(text) {
text = xpathLocator.literal(text);
const xpath = this.toXPath(`:text-case(${text})`);
const xpath = sprintf('%s[%s]', this.toXPath(), `.= ${text}`);
return new Locator({ xpath });
}

Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@
"axios": "1.6.3",
"chai": "4.3.8",
"chai-deep-match": "1.2.1",
"chai-exclude": "^2.1.0",
"chai-json-schema": "^1.5.1",
"chai-json-schema-ajv": "^5.2.4",
"chai-match-pattern": "^1.3.0",
"chai-string": "^1.5.0",
"chai-exclude": "2.1.0",
"chai-json-schema": "1.5.1",
"chai-json-schema-ajv": "5.2.4",
"chai-match-pattern": "1.3.0",
"chai-string": "1.5.0",
"chalk": "4.1.2",
"commander": "11.1.0",
"cross-spawn": "7.0.3",
"css-to-xpath": "0.1.0",
"csstoxpath": "1.6.0",
"devtools": "8.27.2",
"envinfo": "7.11.0",
Expand All @@ -93,7 +94,7 @@
"fn-args": "4.0.0",
"fs-extra": "11.2.0",
"glob": "6.0.1",
"html-minifier-terser": "^7.2.0",
"html-minifier-terser": "7.2.0",
"inquirer": "6.5.2",
"joi": "17.11.0",
"js-beautify": "1.14.11",
Expand All @@ -119,7 +120,7 @@
"@faker-js/faker": "7.6.0",
"@pollyjs/adapter-puppeteer": "6.0.6",
"@pollyjs/core": "5.1.0",
"@types/chai": "^4.3.7",
"@types/chai": "4.3.7",
"@types/inquirer": "9.0.3",
"@types/node": "20.10.7",
"@wdio/sauce-service": "8.27.0",
Expand Down
15 changes: 14 additions & 1 deletion test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const xml = `<body>
</div>
</fieldset>
<label class="n-1">Hello<a href="#">Please click</a></label>
<label class="n1">Hello no hyphen<a href="#">Please click</a></label>
</div>
<input type="hidden" name="return_url" value="" id="return_url" />

Expand Down Expand Up @@ -197,7 +198,7 @@ describe('Locator', () => {
.inside(Locator.build('label').withText('Hello'));

const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes).to.have.length(2, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Please click', l.toXPath());
});

Expand Down Expand Up @@ -301,4 +302,16 @@ describe('Locator', () => {
expect(nodes).to.have.length(1, l.toXPath());
expect(nodes[0].firstChild.data).to.eql('Authoring', l.toXPath());
});

it('should find element with last of type with text', () => {
const l = Locator.build('.p-confirm-popup:last-of-type button').withText('delete');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(0, l.toXPath());
});

it('should find element with last of type without text', () => {
const l = Locator.build('.p-confirm-popup:last-of-type button');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(0, l.toXPath());
});
});