I'm trying to match some text using a regex pattern, but in the assert I'm getting an error saying it's expecting a pattern but it received a string. The locator looks like this: this.swapAmountFrom = page.locator("data-test-id=swap-amount-from").first();
and the assert line looks like this await expect.soft(drawer.swapAmountFrom).toHaveText(/-1.280\d+ BTC/);
. The error message is:
Error: Timed out 30000ms waiting for expect(received).toHaveText(expected)
Expected pattern: /-1.280\d+ BTC/
Received string: "-1.28089 BTC"
Call log:
- expect.soft.toHaveText with timeout 30000ms
- waiting for locator('data-test-id=swap-amount-from').first()
- locator resolved to <div font-size="6" color="palette.text.shade100" data…>-1.28089 BTC</div>
- unexpected value "-1.28089 BTC"
- locator resolved to <div font-size="6" color="palette.text.shade100" data…>-1.28089 BTC</div>
- unexpected value "-1.28089 BTC"
- locator resolved to <div font-size="6" color="palette.text.shade100" data…>-1.28089 BTC</div>
- unexpected value "-1.28089 BTC"
etc...
I'm guessing it's something dumb on my side but the assert looks like it does in the docs. Any ideas what's wrong?
This thread is trying to answer question "Why is the assert statement expecting a pattern but receiving a string when trying to match text using a regex pattern?"
might be me i never have liked the regex meta match characters would rather simply use the following as it is absolutely clear what all be matching.. -1.280 followed by any number of digets followed by any character followed by BTC, you decide if you want case insensitive...:
/-1.280[0-9]*.*BTC/
Thing the might really mess with you having a good day is the space html may use... a space, a hard space, a tab or something else before the "BTC"...
Related Ask AI answer for "What is the reason behind the assert statement expecting a pattern instead of a string when trying to match text using a regex pattern?".
When you're using regular expressions (regex) in assert statements, you need to provide a regex pattern, not a string. Why? Because regex is all about pattern matching within strings. It's a powerful tool that lets you define complex patterns to find specific character sequences or validate a string's format.
Here's the thing: if you provide a string instead of a regex pattern, the assert statement will treat it as a literal value. It will look for an exact match in the text, not interpreting any special characters or patterns within the string as regex elements.
So, how do you use regex patterns in assert statements correctly? You need to provide them as either RegExp objects or strings containing valid regex syntax. This way, the assert statement can properly interpret and apply the matching rules defined by the regex pattern.
import { test } from '@playwright/test';
test('regex pattern in assert', async ({ page }) => {
await page.goto('https://ray.run/');
const text = await page.textContent('.content');
assert.match(text, /Playwright/);
});
In this @playwright/test example, the assert statement is expecting a regex pattern (/Playwright/
). It will match this pattern with the actual text from the page.
Remember, when using assert statements with regex for matching text, always provide a valid regex pattern. It ensures accurate and flexible matching based on your criteria.
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].