I have a few tests that only need to be run when in a mobile browser. Currently I have a client function to check the user agent.
const checkMobile = ClientFunction(() => /iPhone|Android/i.test(navigator.userAgent))
Which I then access inside my tests:
test("Check if mobile", async t => {
const isMobile = await checkMobile();
if (isMobile) {
await t
// do the tests
}
}
Is there a way for me to use this in a fixture? Like a fixture which will only run if checkMobile
is true
? So I don't need to repeat this check in every test manually. I'm sure there's a smarter way to go about it than what I have right now. I tried doing the check in the fixture's beforeEach
hook but it didn't work out for me to then try to share the outcome of that in a variable to pass down to the tests.
To share a variable from the fixture hook for testing, you can use fixture context
http://devexpress.github.io/testcafe/documentation/test-api/test-code-structure.html#sharing-variables-between-fixture-hooks-and-test-code
In addition, you can reorganize your test in the following manner:
import { ClientFunction } from 'testcafe';
fixture `test`.page('http://example.com').beforeEach(async t => {
const isMobile = await checkMobile();
t.ctx.isMobile = isMobile;
})
const checkMobile = ClientFunction(() => /iPhone|Android/i.test(navigator.userAgent))
function testAndCheck (testName, testFn) {
const foo = async t => {
if (t.ctx.isMobile)
await testFn(t)
else
throw new Error('not mobile');
}
test(testName, foo);
}
testAndCheck('test1', async t => {
await t.click('h1');
await t.click('div');
await t.click('h1');
})
Here I define the testAndCheck
function that extends the test
function with an additional check for mobile devices.
In addition, you can refer to this comment https://github.com/DevExpress/testcafe/issues/1626#issuecomment-417424558 to see which other ways exist to overcome the issue.