Skip to content

fix: locator builder returns error when class name contains hyphen #4024

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
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
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('css-to-xpath');
const cssToXPath = require('convert-cssxpath');
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(this.value);
if (this.isCSS()) return cssToXPath.convert(this.value);

throw new Error('Can\'t be converted to XPath');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"chai-string": "^1.5.0",
"chalk": "4.1.2",
"commander": "11.0.0",
"convert-cssxpath": "1.0.2",
"cross-spawn": "7.0.3",
"css-to-xpath": "0.1.0",
"envinfo": "7.11.0",
"escape-string-regexp": "4.0.0",
"figures": "3.2.0",
Expand Down
8 changes: 7 additions & 1 deletion test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const xml = `<body>
<input name="name1" label="Выберите услугу" type="text" value=""/>
</div>
</fieldset>
<label>Hello<a href="#">Please click</a></label>
<label class="n-1">Hello<a href="#">Please click</a></label>
</div>
<input type="hidden" name="return_url" value="" id="return_url" />
</body>`;
Expand Down Expand Up @@ -205,6 +205,12 @@ describe('Locator', () => {
}).to.throw('round brackets');
});

it('should find element with class name contains hyphen', () => {
const l = Locator.build('').find('.n-1').first();
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1, l.toXPath());
});

it('should throw an error when locator with specific position is nested', () => {
expect(() => {
Locator.build('tr').withChild(Locator.build('td').first());
Expand Down