diff --git a/lib/locator.js b/lib/locator.js index 8629feaa9..f38c42742 100644 --- a/lib/locator.js +++ b/lib/locator.js @@ -116,12 +116,12 @@ class Locator { } withChild(locator) { - const xpath = sprintf('%s//child::%s', this.toXPath(), removePrefix((new Locator(locator, 'css')).toXPath())); + const xpath = sprintf('%s[./child::%s]', this.toXPath(), removePrefix((new Locator(locator, 'css')).toXPath())); return new Locator({ xpath }); } withDescendant(locator) { - const xpath = sprintf('%s//descendant::%s', this.toXPath(), removePrefix((new Locator(locator, 'css')).toXPath())); + const xpath = sprintf('%s[./descendant::%s]', this.toXPath(), removePrefix((new Locator(locator, 'css')).toXPath())); return new Locator({ xpath }); } diff --git a/test/unit/locator_test.js b/test/unit/locator_test.js index 9b5d818f4..e4b774e51 100644 --- a/test/unit/locator_test.js +++ b/test/unit/locator_test.js @@ -123,13 +123,20 @@ describe('Locator', () => { it('should select element by siblings', () => { const l = Locator.build('//table') - .withChild('td') - .withText('Also Edit') - .first(); + .withChild(Locator.build('tr') + .withChild('td') + .withText('Also Edit')); const nodes = xpath.select(l.toXPath(), doc); expect(nodes).to.have.length(1, l.toXPath()); }); + it('should not select element by deep nested siblings', () => { + const l = Locator.build('//table') + .withChild('td'); + const nodes = xpath.select(l.toXPath(), doc); + expect(nodes).to.have.length(0, l.toXPath()); + }); + it('should select element by siblings', () => { const l = Locator.build('//table') .find('td')