This thread is trying to answer question "How can one store the value found from the ContainText function in a variable for repetitive use in other parts of a program?"
Sure. You can run the toContainText assertion to locate and verify your element, then use const text = myLocatorThatContainsTheDesiredText.textContent()
then use the text
variable wherever you want.
https://playwright.dev/docs/api/class-locator#locator-text-content
await (expect (page.getByText('+===================================+ | New Account Info | +====================')).toContainText('NameServer1: ns1.10-2-68-250.cprapid.com')); await (expect (page.getByText('+===================================+ | New Account Info | +====================')).toContainText('NameServer2: ns2.10-2-68-250.cprapid.com'));
Related Ask AI answer for "How do I save the result of a text search in Playwright for future use in my program?".
You can store the result of a text search in a variable for future use in your program. Here's how to do it in TypeScript with @playwright/test.
First, declare a variable and assign it the text you're searching for:
const searchText = 'orange';
Next, use the getByText()
function to find an element with that text content. Store the result in another variable:
const element = await page.getByText(searchText);
Now, you can reuse element
as needed in your program. For example, if you want to click on the element multiple times, you can do so like this:
await element.click();
// ... other code ...
await element.click();
// ... more code ...
Remember, variables are scoped within their respective blocks or functions. If you need to access the stored value across different parts of your program, make sure to define your variables at an appropriate scope.
Also, if your program involves asynchronous operations or awaits for promises, handle them properly using async/await syntax or promise chaining.
Storing values from a text search in variables allows you to conveniently access and manipulate those values throughout your program whenever needed.
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].