You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A new function on the Page and Locator classes (and wherever else these things appear), similar to getByText or getByRole. getByValue would find input elements with the given value and return a Locator. I'm not sure if inputs are the only thing this would apply to - I guess whichever types of elements the LocatorAssertion toHaveValue looks at.
Example
My current use case is a form with multiple rows, where the user can set area, startTime and endTime using the inputs which trigger a dropdown. Each row is identical, save for the values currently selected.
If I want my test to do something like form.changeArea('area 1', 'area 2'), I need to locate the right <input> to click on it.
I could just get the nth(1) row and get the area input that way, but selecting by area name makes for a more readable test, and I imagine situations exist where you know the value but not its place in a list.
This is what the input element looks like in the DOM - I assume the reason something like locator('input[value="whatever"]') doesn't work is it doesn't actually have a value property, just a computed Value attribute.
I've made this function which seems to do the job for now:
// Example: this.getByValue('Bob Bobson')// Example: this.getByValue('Bob', { exact: false })// Example: this.getByValue('Bob Bobson', { parent: this.addSchedulePopover() })
@step()asyncgetByValue(value: string,{ parent =this.page, exact =false}: {parent?: Locator|Page;exact?: boolean;}={}){letmatchingInput;constfnExact=(element,name)=>element?.value===name,fnIncludes=(element,name)=>element?.value?.includes(name);// since we can't use inbuilt locator functions for this, expect.toPass gives us some auto-retrying capability// in case the page isn't done loading when this is calledawaitexpect(async()=>{matchingInput=null;for(constinputofawaitparent.locator('input').all()){constmatch=awaitinput.evaluate(exact ? fnExact : fnIncludes,value);if(match){matchingInput=input;break;}}if(!matchingInput)throwError(`Could not find input with value '${value}'`);},{message: `find input with value '${value}'`}).toPass({timeout: 20_000});returnmatchingInput;}
It seems to do the job but it's not ideal, because any locator function that uses it now has to return a Promise instead of a Locator, so you have to go up the chain and make everything async.
(I'm now realising I probably could've used inputValue() to find the element instead of evaluate(), but that still leaves me with an async function instead of a nice synchronous function that returns a Locator.)
Motivation
This isn't the first time I've run across a situation where getByValue would have helped, I've just always found a less-ideal workaround and never thought to log a request. I could just be crazy or a fool but it seems like 'find element by value' would be a common operation like 'find element by text' is; I was surprised I couldn't find an example of someone asking for it.
I think it would make Playwright better by providing a simple method to perform what I think is a common operation.
The text was updated successfully, but these errors were encountered:
Please tell us more about your use case. How do you select the input field where you enter "area 1" when all rows in the table are empty? Is each row comprised of one input field or multiple, maybe you can share a DOM snippet or trace file with a DOM snapshot?
There have been similar requests in the past #18970, #14679, #3228, but they didn't gather enough upvotes. Also, maybe in your case table selectors would solve the problem, if they were implemented?
🚀 Feature Request
A new function on the Page and Locator classes (and wherever else these things appear), similar to
getByText
orgetByRole
.getByValue
would find input elements with the given value and return a Locator. I'm not sure ifinput
s are the only thing this would apply to - I guess whichever types of elements the LocatorAssertiontoHaveValue
looks at.Example
My current use case is a form with multiple rows, where the user can set area, startTime and endTime using the inputs which trigger a dropdown. Each row is identical, save for the values currently selected.
If I want my test to do something like
form.changeArea('area 1', 'area 2')
, I need to locate the right <input> to click on it.I could just get the
nth(1)
row and get the area input that way, but selecting by area name makes for a more readable test, and I imagine situations exist where you know the value but not its place in a list.This is what the input element looks like in the DOM - I assume the reason something like
locator('input[value="whatever"]')
doesn't work is it doesn't actually have avalue
property, just a computed Value attribute.I've made this function which seems to do the job for now:
It seems to do the job but it's not ideal, because any locator function that uses it now has to return a Promise instead of a Locator, so you have to go up the chain and make everything async.
(I'm now realising I probably could've used
inputValue()
to find the element instead ofevaluate()
, but that still leaves me with an async function instead of a nice synchronous function that returns a Locator.)Motivation
This isn't the first time I've run across a situation where getByValue would have helped, I've just always found a less-ideal workaround and never thought to log a request. I could just be crazy or a fool but it seems like 'find element by value' would be a common operation like 'find element by text' is; I was surprised I couldn't find an example of someone asking for it.
I think it would make Playwright better by providing a simple method to perform what I think is a common operation.
The text was updated successfully, but these errors were encountered: