JavaScript闭包无法识别全局变量

问题描述 投票:0回答:1

[试图使用testcafe在UI上运行一些端到端测试。以下脚本应该从用户那里获取输入,并在运行时对其进行评估,以帮助开发新的测试脚本。但是,它无法识别Selector

例如

>> console.log("hi")
hi
>> t.click(Selector('button').withText('Google Search'))
 ✖ test 1

   1) ReferenceError: Selector is not defined

      Browser: Chrome 80.0.3987.162 / macOS 10.15.4

         12 |let x = "";
         13 |
         14 |test('test 1', async (t) => {
         15 |    while (1) {
         16 |      x = await getInput();
       > 17 |      eval(x)
         18 |   }
         19 |})
         20 |
         21 |async function getInput() {
         22 |  return new Promise(

知道为什么吗?谢谢!!

这里是代码

import {Selector} from 'testcafe';
import readline from "readline";

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

fixture`test page`
  .page(`https://www.google.com`)

let x = "";

test('test 1', async (t) => {
    while (1) {
      x = await getInput();
      eval(x)
    }
})

async function getInput() {
  return new Promise(
    (resolve, reject) => {
      rl.question(">> ", function (txt) {
        resolve(txt)
      })
    }
  );
};

[试图使用testcafe在UI上运行一些端到端测试。以下脚本应该从用户那里获取输入,并在运行时对其进行评估,以帮助开发新的测试脚本。但是,它是...

javascript node.js testing automated-tests testcafe
1个回答
2
投票

TestCafe使用Babel可以转换import语句。我建议您在这种情况下使用require。以下行应使您的测试工作:

© www.soinside.com 2019 - 2024. All rights reserved.