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

Add a locator builder method withTextEquals() #4100

Merged
merged 4 commits into from
Jan 3, 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
10 changes: 9 additions & 1 deletion docs/locators.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,20 @@ locate('form').withDescendant('select');

#### withText

Finds element containing a text
Find an element containing a text

```js
locate('span').withText('Warning');
```

#### withTextEquals

Find an element with exact text

```js
locate('button').withTextEquals('Add');
```

#### first

Get first element:
Expand Down
19 changes: 16 additions & 3 deletions lib/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ class Locator {
}

/**
* @param {string} [pseudoSelector] CSS to XPath extension pseudo: https://www.npmjs.com/package/csstoxpath?activeTab=explore#extension-pseudos
* @returns {string}
*/
toXPath() {
toXPath(pseudoSelector = '') {
if (this.isXPath()) return this.value;
if (this.isCSS()) return cssToXPath(this.value);
if (this.isCSS()) return cssToXPath(`${this.value}${pseudoSelector}`);

throw new Error('Can\'t be converted to XPath');
}
Expand Down Expand Up @@ -243,12 +244,24 @@ class Locator {
}

/**
* Find an element containing a text
* @param {string} text
* @returns {Locator}
*/
withText(text) {
text = xpathLocator.literal(text);
const xpath = sprintf('%s[%s]', this.toXPath(), `contains(., ${text})`);
const xpath = this.toXPath(`:text-contains-case(${text})`);
return new Locator({ xpath });
}

/**
* Find an element with exact text
* @param {string} text
* @returns {Locator}
*/
withTextEquals(text) {
text = xpathLocator.literal(text);
const xpath = this.toXPath(`:text-case(${text})`);
return new Locator({ xpath });
}

Expand Down
10 changes: 8 additions & 2 deletions test/unit/locator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Locator = require('../../lib/locator');

let doc;
const xml = `<body>
<span>Hey</span>
<span>Hey boy</span>
<p>
<span></span>
<div></div>
Expand Down Expand Up @@ -157,12 +157,18 @@ describe('Locator', () => {
expect(nodes).to.have.length(1);
});

it('should build locator to match element by text', () => {
it('should build locator to match element containing a text', () => {
const l = Locator.build('span').withText('Hey');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1);
});

it('should build locator to match element by exact text', () => {
const l = Locator.build('span').withTextEquals('Hey boy');
const nodes = xpath.select(l.toXPath(), doc);
expect(nodes).to.have.length(1);
});

it('should build locator to match element by position', () => {
const l = Locator.build('#fieldset-buttons')
.find('//tr')
Expand Down