From 3162bc54d3852b8a9e3bfee43584e9fc2ff8dc1c Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Wed, 20 Feb 2019 22:22:51 +0300 Subject: [PATCH 1/7] add method for finds ancestor of located element --- docs/locators.md | 9 +++++++++ lib/locator.js | 5 +++++ test/unit/locator_test.js | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/docs/locators.md b/docs/locators.md index 9704f6b5b..50edc43ff 100644 --- a/docs/locators.md +++ b/docs/locators.md @@ -174,6 +174,15 @@ locate('#table td').at(2); locate('#table td').at(-2); ``` +#### parent + +Finds an element that is the ancestor of the located: + +```js +// finds `form#user_profile' as the parent element of 'div` +locate('select').parent('form#user_profile'); +``` + #### inside Finds an element which contains an provided ancestor: diff --git a/lib/locator.js b/lib/locator.js index afdf2fea1..bd2dedb00 100644 --- a/lib/locator.js +++ b/lib/locator.js @@ -160,6 +160,11 @@ class Locator { return this; } + parent(locator) { + const xpath = sprintf('%s//ancestor::%s', this.toXPath(), removePrefix((new Locator(locator, 'css')).toXPath())); + return new Locator({ xpath }); + } + inside(locator) { const xpath = sprintf('%s[ancestor::%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 b09fdeda8..3e48be330 100644 --- a/test/unit/locator_test.js +++ b/test/unit/locator_test.js @@ -96,6 +96,16 @@ describe('Locator', () => { expect(nodes[0].firstChild.data).to.eql('Show'); }); + it('should parent select a by label', () => { + const l = Locator.build('a') + .withAttr({ href: '#' }) + .parent(Locator.build('label').withText('Hello')); + + const nodes = xpath.select(l.toXPath(), doc); + expect(nodes).to.have.length(1, l.toXPath()); + expect(nodes[0].firstChild.data).to.eql('Hello', l.toXPath()); + }); + it('should select a by label', () => { const l = Locator.build('a') .withAttr({ href: '#' }) From 76ff592436b92252a7272884226550273895b726 Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Wed, 20 Feb 2019 22:28:58 +0300 Subject: [PATCH 2/7] fix test name --- test/unit/locator_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/locator_test.js b/test/unit/locator_test.js index 3e48be330..dcd29ef33 100644 --- a/test/unit/locator_test.js +++ b/test/unit/locator_test.js @@ -96,7 +96,7 @@ describe('Locator', () => { expect(nodes[0].firstChild.data).to.eql('Show'); }); - it('should parent select a by label', () => { + it('should select a label text', () => { const l = Locator.build('a') .withAttr({ href: '#' }) .parent(Locator.build('label').withText('Hello')); From a8aaaa50231a526dbda4c94a44fafbe9cb84708a Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Thu, 21 Feb 2019 09:22:10 +0300 Subject: [PATCH 3/7] fix withChild --- lib/locator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/locator.js b/lib/locator.js index bd2dedb00..b9c80fe6a 100644 --- a/lib/locator.js +++ b/lib/locator.js @@ -116,7 +116,7 @@ class Locator { } withChild(locator) { - const xpath = sprintf('%s[//%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 }); } From 1f145a7a1a0b8ce00262827b77d36664dcaf0eed Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Thu, 21 Feb 2019 10:41:23 +0300 Subject: [PATCH 4/7] fix withChild --- docs/locators.md | 9 --------- lib/locator.js | 5 ----- test/unit/locator_test.js | 10 ---------- 3 files changed, 24 deletions(-) diff --git a/docs/locators.md b/docs/locators.md index 50edc43ff..9704f6b5b 100644 --- a/docs/locators.md +++ b/docs/locators.md @@ -174,15 +174,6 @@ locate('#table td').at(2); locate('#table td').at(-2); ``` -#### parent - -Finds an element that is the ancestor of the located: - -```js -// finds `form#user_profile' as the parent element of 'div` -locate('select').parent('form#user_profile'); -``` - #### inside Finds an element which contains an provided ancestor: diff --git a/lib/locator.js b/lib/locator.js index b9c80fe6a..a88a7d880 100644 --- a/lib/locator.js +++ b/lib/locator.js @@ -160,11 +160,6 @@ class Locator { return this; } - parent(locator) { - const xpath = sprintf('%s//ancestor::%s', this.toXPath(), removePrefix((new Locator(locator, 'css')).toXPath())); - return new Locator({ xpath }); - } - inside(locator) { const xpath = sprintf('%s[ancestor::%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 dcd29ef33..b09fdeda8 100644 --- a/test/unit/locator_test.js +++ b/test/unit/locator_test.js @@ -96,16 +96,6 @@ describe('Locator', () => { expect(nodes[0].firstChild.data).to.eql('Show'); }); - it('should select a label text', () => { - const l = Locator.build('a') - .withAttr({ href: '#' }) - .parent(Locator.build('label').withText('Hello')); - - const nodes = xpath.select(l.toXPath(), doc); - expect(nodes).to.have.length(1, l.toXPath()); - expect(nodes[0].firstChild.data).to.eql('Hello', l.toXPath()); - }); - it('should select a by label', () => { const l = Locator.build('a') .withAttr({ href: '#' }) From a9faf2f0b47bf48e441096521655990f5012b6be Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Thu, 21 Feb 2019 11:16:37 +0300 Subject: [PATCH 5/7] fix --- test/unit/locator_test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/unit/locator_test.js b/test/unit/locator_test.js index b09fdeda8..a9dde39ef 100644 --- a/test/unit/locator_test.js +++ b/test/unit/locator_test.js @@ -44,10 +44,17 @@ const xml = ` +
+ +
+
+ +
+ `; describe('Locator', () => { @@ -106,6 +113,15 @@ describe('Locator', () => { expect(nodes[0].firstChild.data).to.eql('Please click', l.toXPath()); }); + + it('should select child element by name', () => { + const l = Locator.build('.form-field') + .withChild(Locator.build('//input[@name="name1"]')); + const nodes = xpath.select(l.toXPath(), doc); + + expect(nodes).to.have.length(1, l.toXPath()); + }); + it('should select element by siblings', () => { const l = Locator.build('//table') .withChild('td') From ac94636f2a787e5831b5edf69a084bb44534d3af Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Thu, 21 Feb 2019 11:37:58 +0300 Subject: [PATCH 6/7] fix --- test/unit/locator_test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/locator_test.js b/test/unit/locator_test.js index a9dde39ef..e54057c65 100644 --- a/test/unit/locator_test.js +++ b/test/unit/locator_test.js @@ -54,7 +54,6 @@ const xml = ` - `; describe('Locator', () => { From 0979a5ddcfc7fa4a21d49b11c7c47a50aae0987b Mon Sep 17 00:00:00 2001 From: Vorobeyko Date: Fri, 22 Feb 2019 09:11:18 +0300 Subject: [PATCH 7/7] fix by comment --- docs/locators.md | 9 +++++++++ lib/locator.js | 5 +++++ test/unit/locator_test.js | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/locators.md b/docs/locators.md index 9704f6b5b..c1e9c7b87 100644 --- a/docs/locators.md +++ b/docs/locators.md @@ -137,6 +137,15 @@ Finds an element which contains a child element provided: locate('form').withChild('select'); ``` +#### withDescendant + +Finds an element which contains a descendant element provided: + +```js +// finds form with