如何将参数注入TestCafé测试? [英] How can I inject parameters into a TestCafé test?

查看:82
本文介绍了如何将参数注入TestCafé测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

我使用API​​运行包装在代码中的TestCafé我有一个要参数化的测试,使用不同的动态值进行测试.

I run TestCafé wrapped in code, using the API I have a test I want to parameterize, testing with different dynamic values.

问题

Testcafé不支持将参数发送到测试.有没有办法注入值?

Testcafé has no support for sending parameters to a test. Is there a way to inject values?

推荐答案

是的,有办法! clientScripts 功能可用于发送参数.该文档写得很好,如何注入脚本.我花了一些时间从测试中弄清楚如何使用它,希望这能使您走上正确的轨道.

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.

  1. 使用参数创建数据对象
  2. 将JSON添加到表示功能的小JS代码块中
  3. 使用 .clientScripts 设置器
  4. 将代码块注入流道/夹具/测试
  5. eval 测试中的代码!您有参数
  1. Create a data object with your params
  2. Add the JSON to a small JS code block representing a function
  3. Inject the code block to your runner/fixture/test with the .clientScripts setter
  4. 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...
})

现在,执行测试时,getParameters函数将在页面头部内 中出现.可以使用 ClientFunction t.eval :

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?')

一个完整的工作示例的答案可以在这里找到.

这篇关于如何将参数注入TestCafé测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆