【问题标题】:Javascript closure doesn't recognize global variableJavascript 闭包无法识别全局变量
【发布时间】:2020-04-05 14:36:45
【问题描述】:

尝试使用 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)
      })
    }
  );
};

【问题讨论】:

    标签: javascript node.js testing automated-tests testcafe


    【解决方案1】:

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

    const Selector = require('testcafe').Selector;

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      相关资源
      最近更新 更多