admin管理员组文章数量:1333210
Scenario:
I run TestCafé wrapped in code, using the API I have a test I want to parameterize, testing with different dynamic values.
Problem
Testcafé has no support for sending parameters to a test. Is there a way to inject values?
Scenario:
I run TestCafé wrapped in code, using the API I have a test I want to parameterize, testing with different dynamic values.
Problem
Testcafé has no support for sending parameters to a test. Is there a way to inject values?
Share Improve this question edited Dec 18, 2019 at 12:30 Alex Skorkin 4,2743 gold badges26 silver badges48 bronze badges asked Sep 24, 2019 at 8:07 SvenSven 95215 silver badges31 bronze badges2 Answers
Reset to default 6You can use process.env to pass parameters to TestCafe tests from your runner script.
//test.js
const createTestCafe = require('testcafe');
(async => {
process.env.foo = 'bar';
const testcafe = await createTestCafe();
await testcafe
.createRunner()
.src('test.js')
.browsers('chrome')
.run();
await testcafe.close();
})()
//test.js
fixture `Examples`;
test('process.env', async t => {
console.log(process.env.foo);
});
Yes, there is a way! The clientScripts
feature can be used to send in parameters. The documentation is very well written how to inject scripts. It took me some time to figure out how to use it from within the test so hopefully this will bring you on the right track.
- Create a data object with your params
- Add the JSON to a small JS code block representing a function
- Inject the code block to your runner/fixture/test with the
.clientScripts
setter eval
the code in your test et voilá! You have the parameters
// Create the data object
let data = {aString: 'Yo!', aNumber: 345}
// Add it to a String value representing a JS function
const scriptContent = `
function getParameters() {
return ${JSON.stringify(data)};
}`
// Invoke the test runner with the code block as content
testcafe('localhost').then(cafe => {
cafe.createRunner()
.src('mytest.js')
.clientScripts({ content: scriptContent })
.run()
//...and so on...
})
Now, when the test is executed the getParameters function exists inside the page head. This code can be evaluated or invoked with a ClientFunction
or a t.eval
:
let x = await t.eval( () => getParameters() );
console.log(x);
await t.expect(x.aString).eql('Yo!', 'Not Okey, mKay?')
An answer with a fully working example can be found here.
本文标签: javascriptHow can I inject parameters into a TestCaf233 testStack Overflow
版权声明:本文标题:javascript - How can I inject parameters into a TestCafé test? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742226700a2436451.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论