If a site has two locales, GB and US, and there is a label text we need to assert, where GB says “My dashboard” and the US says “My Dashboard”, how do we validate these text values without duplicating the test?
CodePudding user response:
- As for "My dashboard" / "My Dashboard", the easiest way to do that is to use the
toLowerCase()
method (@pavelsman comment):
await t.expect(title.innerText.toLowerCase()).eql('my dashboard');
- If you have a complex case, you can use regex matching or the
.contains()
method:
await t.expect(title.innerText).match(yourRegex);
await t.expect(title.innerText).contains(sameStringPartForBothCases);
- If that doesn't suit you, then you might want to arrange your expected values as objects to assert the full title string:
import { userVariables } from 'testcafe';
fixture `Fixture`;
test('test', async t => {
const expectedTitle = {
'en-GB': 'My dashboard',
'en-US': 'My Dashboard'
};
await t.expect(title.innerText).eql(expectedTitle[userVariables.currentLocale]);
});
In this case, you need to run TestCafe with some of the .testcaferc.json
files.
The "en-GB" configuration contains:
{
"browsers": "chrome --lang=en-GB",
"userVariables": {
"currentLocale": "en-GB"
}
}
The "en-US" configuration contains:
{
"browsers": "chrome --lang=en-US",
"userVariables": {
"currentLocale": "en-US"
}
}
Reference: https://testcafe.io/documentation/402638/reference/configuration-file#uservariables