Skip to content

Commit e7e4e26

Browse files
romovschristian-bromann
authored andcommitted
webdriverio: throw proper errors from selectByAttribute/VisibleText
1 parent 31782bc commit e7e4e26

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

packages/webdriverio/src/commands/element/selectByAttribute.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export default async function selectByAttribute (attribute, value) {
5050
const normalized = `[normalize-space(@${attribute.trim()}) = "${value.trim()}"]`
5151
const optionElement = await this.findElementFromElement(this.elementId, 'xpath', `./option${normalized}|./optgroup/option${normalized}`)
5252

53+
if (optionElement && optionElement.error === 'no such element') {
54+
throw new Error(`Option with attribute "${attribute}=${value}" not found.`)
55+
}
56+
5357
/**
5458
* select option
5559
*/

packages/webdriverio/src/commands/element/selectByVisibleText.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ export default async function selectByVisibleText (text) {
6060

6161
const optionElement = await this.findElementFromElement(this.elementId, 'xpath', selections.join('|'))
6262

63+
if (optionElement && optionElement.error === 'no such element') {
64+
throw new Error(`Option with text "${text}" not found.`)
65+
}
66+
6367
/**
6468
* select option
6569
*/

packages/webdriverio/tests/commands/element/selectByAttribute.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,22 @@ describe('selectByAttribute test', () => {
4646
[ELEMENT_KEY]: 'some-sub-elem-321'
4747
})
4848
})
49+
50+
it('should throw if option is not found', async () => {
51+
expect.hasAssertions()
52+
53+
const mockElem = {
54+
selector: 'foobar2',
55+
elementId: 'some-elem-123',
56+
'element-6066-11e4-a52e-4f735466cecf': 'some-elem-123',
57+
findElementFromElement: jest.fn().mockReturnValue(Promise.resolve({ error: 'no such element' }))
58+
}
59+
mockElem.selectByAttribute = elem.selectByAttribute.bind(mockElem)
60+
61+
try {
62+
await mockElem.selectByAttribute('value', 'non-existing-value')
63+
} catch (e) {
64+
expect(e.toString()).toBe('Error: Option with attribute "value=non-existing-value" not found.')
65+
}
66+
})
4967
})

packages/webdriverio/tests/commands/element/selectByVisibleText.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,22 @@ describe('selectByVisibleText test', () => {
106106
[ELEMENT_KEY]: 'some-sub-elem-321'
107107
})
108108
})
109+
110+
it('should throw if option is not found', async () => {
111+
expect.hasAssertions()
112+
113+
const mockElem = {
114+
selector: 'foobar2',
115+
elementId: 'some-elem-123',
116+
'element-6066-11e4-a52e-4f735466cecf': 'some-elem-123',
117+
findElementFromElement: jest.fn().mockReturnValue(Promise.resolve({ error: 'no such element' }))
118+
}
119+
mockElem.selectByVisibleText = elem.selectByVisibleText.bind(mockElem)
120+
121+
try {
122+
await mockElem.selectByVisibleText('non-existing-option')
123+
} catch (e) {
124+
expect(e.toString()).toBe('Error: Option with text "non-existing-option" not found.')
125+
}
126+
})
109127
})

0 commit comments

Comments
 (0)