Skip to content

Fix/locator method withChild #1513

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 8 commits into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add method for finds ancestor of located element
  • Loading branch information
Vorobeyko committed Feb 20, 2019
commit 3162bc54d3852b8a9e3bfee43584e9fc2ff8dc1c
9 changes: 9 additions & 0 deletions docs/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
10 changes: 10 additions & 0 deletions test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '#' })
Expand Down