Skip to content

Commit

Permalink
fix: cannot locate complicated locator (#4101)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Jan 2, 2024
1 parent 1a9f3be commit 0d2ef39
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/locator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cssToXPath = require('convert-cssxpath');
const cssToXPath = require('csstoxpath');
const { sprintf } = require('sprintf-js');

const { xpathLocator } = require('./utils');
Expand Down Expand Up @@ -162,7 +162,7 @@ class Locator {
*/
toXPath() {
if (this.isXPath()) return this.value;
if (this.isCSS()) return cssToXPath.convert(this.value);
if (this.isCSS()) return cssToXPath(this.value);

throw new Error('Can\'t be converted to XPath');
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
"chai-string": "^1.5.0",
"chalk": "4.1.2",
"commander": "11.1.0",
"convert-cssxpath": "1.0.2",
"cross-spawn": "7.0.3",
"csstoxpath": "1.6.0",
"envinfo": "7.11.0",
"escape-string-regexp": "4.0.0",
"figures": "3.2.0",
Expand Down Expand Up @@ -121,6 +121,7 @@
"@wdio/sauce-service": "8.27.0",
"@wdio/selenium-standalone-service": "8.3.2",
"@wdio/utils": "8.27.0",
"@xmldom/xmldom": "0.8.10",
"apollo-server-express": "2.25.3",
"chai-as-promised": "7.1.1",
"chai-subset": "1.6.0",
Expand Down Expand Up @@ -157,7 +158,6 @@
"wdio-docker-service": "1.5.0",
"webdriverio": "8.3.8",
"xml2js": "0.6.2",
"@xmldom/xmldom": "0.8.10",
"xpath": "0.0.34"
},
"engines": {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ const xml = `<body>
<label class="n-1">Hello<a href="#">Please click</a></label>
</div>
<input type="hidden" name="return_url" value="" id="return_url" />
<div class="ps-submenu-root">
<ul class="ps-submenu-root">
<li class="ps-menu-button">Adhemar da Silva</li>
<li class="ps-menu-button">HelloWorld</li>
<li class="ps-menu-button">Hallo</li>
</ul>
<ul class="ps-submenu-root">
<li class="ps-menu-button">Adhemar da Silva</li>
<li class="ps-menu-button">HelloWorld</li>
<li class="ps-menu-button">Authoring</li>
</ul>
</div>
</body>`;

describe('Locator', () => {
Expand Down Expand Up @@ -272,4 +285,14 @@ describe('Locator', () => {
expect(nodes[0].firstChild.data).to.eql('Sign In', l.toXPath());
Locator.filters = [];
});

it('should be able to locate complicated locator', () => {
const l = Locator.build('.ps-menu-button')
.withText('Authoring')
.inside('.ps-submenu-root:nth-child(2)');

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

0 comments on commit 0d2ef39

Please sign in to comment.