Skip to content
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

feat: locate element with withClassAttr #4321

Merged
merged 2 commits into from
Apr 28, 2024
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 @@ -145,6 +145,15 @@ Find an element with provided attributes
locate('input').withAttr({ placeholder: 'Type in name' });
```

#### withClassAttr

Find an element with class attribute

```js
// find div with class contains 'form'
locate('div').withClassAttr('text');
```

#### withChild

Finds an element which contains a child element provided:
Expand Down
9 changes: 9 additions & 0 deletions lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,15 @@ class Locator {
return new Locator({ xpath });
}

/**
* @param {String} text
* @returns {Locator}
*/
withClassAttr(text) {
const xpath = sprintf('%s[%s]', this.toXPath(), `contains(@class, '${text}')`);
return new Locator({ xpath });
}

/**
* @param {string} output
* @returns {Locator}
Expand Down
10 changes: 5 additions & 5 deletions test/helper/webapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,11 +1390,11 @@ module.exports.tests = function () {

try {
await I.amOnPage('https://github.com/codeceptjs/CodeceptJS/');
await I.seeAttributesOnElements({ css: 'a[href="/team"]' }, {
href: '/team',
await I.seeAttributesOnElements({ css: 'a[href="/codeceptjs/CodeceptJS"]' }, {
href: '/codeceptjs/CodeceptJS',
});
} catch (e) {
e.message.should.include('all elements (a[href="/team"]) to have attributes {"href":"/team"}');
e.message.should.include('all elements (a[href="/codeceptjs/CodeceptJS"]) to have attributes {"href"="/codeceptjs/CodeceptJS"}');
}
});

Expand Down Expand Up @@ -1425,11 +1425,11 @@ module.exports.tests = function () {

try {
await I.amOnPage('https://github.com/codeceptjs/CodeceptJS/');
await I.seeAttributesOnElements({ css: 'a[href="/team"]' }, {
await I.seeAttributesOnElements({ css: 'a[href="/codeceptjs/CodeceptJS"]' }, {
disable: true,
});
} catch (e) {
e.message.should.include('expected all elements ({css: a[href="/team"]}) to have attributes {"disable":true} "0" to equal "1"');
e.message.should.include('expected all elements ({css: a[href="/codeceptjs/CodeceptJS"]}) to have attributes {"disable":true} "0" to equal "3"');
}
});

Expand Down
6 changes: 6 additions & 0 deletions test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ describe('Locator', () => {
expect(nodes).to.have.length(1);
});

it('should build locator to match element by class', () => {
const l = Locator.build('div').withClassAttr('form-');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(9);
});

it('should build locator to match element containing a text', () => {
const l = Locator.build('span').withText('Hey');
const nodes = xpath.select(l.toXPath(), doc);
Expand Down
Loading