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;
尝试 运行 使用 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;