Skip to content

feat(locator): add withAttrEndsWith, withAttrStartsWith, withAttrContains #4334

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
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
43 changes: 43 additions & 0 deletions lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,49 @@ class Locator {
return new Locator({ xpath });
}

/**
* Adds condition: attribute value starts with text
* (analog of XPATH: [starts-with(@attr,'startValue')] or CSS [attr^='startValue']
* Example: I.click(locate('a').withAttrStartsWith('href', 'https://')));
* Works with any attribute: class, href etc.
* @param {string} attrName
* @param {string} startsWith
* @returns {Locator}
*/
withAttrStartsWith(attrName, startsWith) {
const xpath = sprintf('%s[%s]', this.toXPath(), `starts-with(@${attrName}, "${startsWith}")`);
return new Locator({ xpath });
}

/**
* Adds condition: attribute value ends with text
* (analog of XPATH: [ends-with(@attr,'endValue')] or CSS [attr$='endValue']
* Example: I.click(locate('a').withAttrEndsWith('href', '.com')));
* Works with any attribute: class, href etc.
* @param {string} attrName
* @param {string} endsWith
* @returns {Locator}
*/
withAttrEndsWith(attrName, endsWith) {
const xpath = sprintf('%s[%s]', this.toXPath(), `substring(@${attrName}, string-length(@${attrName}) - string-length("${endsWith}") + 1) = "${endsWith}"`,
);
return new Locator({ xpath });
}

/**
* Adds condition: attribute value contains text
* (analog of XPATH: [contains(@attr,'partOfAttribute')] or CSS [attr*='partOfAttribute']
* Example: I.click(locate('a').withAttrContains('href', 'google')));
* Works with any attribute: class, href etc.
* @param {string} attrName
* @param {string} partOfAttrValue
* @returns {Locator}
*/
withAttrContains(attrName, partOfAttrValue) {
const xpath = sprintf('%s[%s]', this.toXPath(), `contains(@${attrName}, "${partOfAttrValue}")`);
return new Locator({ xpath });
}

/**
* @param {String} text
* @returns {Locator}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,22 @@ describe('Locator', () => {
const nodes = xpath.select(l.toXPath(), doc)
expect(nodes).to.have.length(0, l.toXPath())
})

it('should find element with attribute value starts with text', () => {
const l = Locator.build('a').withAttrStartsWith('class', 'ps-menu-button')
const nodes = xpath.select(l.toXPath(), doc)
expect(nodes).to.have.length(10, l.toXPath())
})

it('should find element with attribute value ends with text', () => {
const l = Locator.build('a').withAttrEndsWith('class', 'ps-menu-button')
const nodes = xpath.select(l.toXPath(), doc)
expect(nodes).to.have.length(9, l.toXPath())
})

it('should find element with attribute value contains text', () => {
const l = Locator.build('a').withAttrEndsWith('class', 'active')
const nodes = xpath.select(l.toXPath(), doc)
expect(nodes).to.have.length(1, l.toXPath())
})
})
Loading