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 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
9 changes: 9 additions & 0 deletions docs/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <select> which is the descendant it
locate('form').withDescendant('select');
```

#### withText

Finds element containing a text
Expand Down
7 changes: 6 additions & 1 deletion lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ class Locator {
}

withChild(locator) {
const xpath = sprintf('%s[//%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()));
return new Locator({ xpath });
}

Expand Down
15 changes: 15 additions & 0 deletions test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ const xml = `<body>
<label for="remember" class="optional">Remember Me</label>
</div>
</div>
<div class="form-field">
<input name="name0" label="Выберите услугу" type="text" value=""/>
</div>
<div class="form-field">
<input name="name1" label="Выберите услугу" type="text" value=""/>
</div>
</fieldset>
<label>Hello<a href="#">Please click</a></label>
</div>
Expand Down Expand Up @@ -106,6 +112,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')
.withDescendant(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')
Expand Down