Skip to content

Commit 45f5a13

Browse files
authored
feat: locate element with withClassAttr (#4321)
1 parent f1a0e38 commit 45f5a13

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

docs/locators.md

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ Find an element with provided attributes
145145
locate('input').withAttr({ placeholder: 'Type in name' });
146146
```
147147

148+
#### withClassAttr
149+
150+
Find an element with class attribute
151+
152+
```js
153+
// find div with class contains 'form'
154+
locate('div').withClassAttr('text');
155+
```
156+
148157
#### withChild
149158

150159
Finds an element which contains a child element provided:

lib/locator.js

+9
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ class Locator {
299299
return new Locator({ xpath });
300300
}
301301

302+
/**
303+
* @param {String} text
304+
* @returns {Locator}
305+
*/
306+
withClassAttr(text) {
307+
const xpath = sprintf('%s[%s]', this.toXPath(), `contains(@class, '${text}')`);
308+
return new Locator({ xpath });
309+
}
310+
302311
/**
303312
* @param {string} output
304313
* @returns {Locator}

test/helper/webapi.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1390,11 +1390,11 @@ module.exports.tests = function () {
13901390

13911391
try {
13921392
await I.amOnPage('https://github.com/codeceptjs/CodeceptJS/');
1393-
await I.seeAttributesOnElements({ css: 'a[href="/team"]' }, {
1394-
href: '/team',
1393+
await I.seeAttributesOnElements({ css: 'a[href="/codeceptjs/CodeceptJS"]' }, {
1394+
href: '/codeceptjs/CodeceptJS',
13951395
});
13961396
} catch (e) {
1397-
e.message.should.include('all elements (a[href="/team"]) to have attributes {"href":"/team"}');
1397+
e.message.should.include('all elements (a[href="/codeceptjs/CodeceptJS"]) to have attributes {"href"="/codeceptjs/CodeceptJS"}');
13981398
}
13991399
});
14001400

@@ -1425,11 +1425,11 @@ module.exports.tests = function () {
14251425

14261426
try {
14271427
await I.amOnPage('https://github.com/codeceptjs/CodeceptJS/');
1428-
await I.seeAttributesOnElements({ css: 'a[href="/team"]' }, {
1428+
await I.seeAttributesOnElements({ css: 'a[href="/codeceptjs/CodeceptJS"]' }, {
14291429
disable: true,
14301430
});
14311431
} catch (e) {
1432-
e.message.should.include('expected all elements ({css: a[href="/team"]}) to have attributes {"disable":true} "0" to equal "1"');
1432+
e.message.should.include('expected all elements ({css: a[href="/codeceptjs/CodeceptJS"]}) to have attributes {"disable":true} "0" to equal "3"');
14331433
}
14341434
});
14351435

test/unit/locator_test.js

+6
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ describe('Locator', () => {
306306
expect(nodes).to.have.length(1);
307307
});
308308

309+
it('should build locator to match element by class', () => {
310+
const l = Locator.build('div').withClassAttr('form-');
311+
const nodes = xpath.select(l.toXPath(), doc);
312+
expect(nodes).to.have.length(9);
313+
});
314+
309315
it('should build locator to match element containing a text', () => {
310316
const l = Locator.build('span').withText('Hey');
311317
const nodes = xpath.select(l.toXPath(), doc);

0 commit comments

Comments
 (0)