簡體   English   中英

Javascript 閉包無法識別全局變量

[英]Javascript closure doesn't recognize global variable

嘗試使用 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 使用可以轉換import語句的 Babel。 我建議你在這種情況下使用require 以下行應該使您的測試工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM