import { Selector } from 'testcafe';
let appList;
let username = '';
let password = '';
// Start - service call to get JSON Data in appList variable
var requestNode = require('request');
var options = {
'method': 'GET',
'url': 'https://********.com/_layouts/15/cors/testcafeapps.ashx',
'headers': {
'Authorization': '*****w=='
}
};
requestNode(options, function (error, response) {
if (error) throw new Error(error);
appList = JSON.parse(response.body);
});
// End -service call to get JSON Data in appList variable
fixtureValidate
.httpAuth({
username: username,
password: password
})
// iterate through JSON items and create tests for each JSON dataitems
for (const app in appList) {
test('SSO test for ' + appList[app], async t => {
console.log('Checking login for ' + appList[app])
await t
.navigateTo(appList[app])
});
}我已经写了上面的代码,我试图对服务调用返回的每个应用程序执行一些测试步骤。我得到一个错误,因为“源文件不包含有效的‘夹具’和‘测试’声明”
如果我使用硬编码的JSON并遍历JSON项,那么我的代码就不会使用服务调用,但我的实际要求是不要使用硬编码的JSON数据,而是从服务调用中获取数据。请让我知道我如何能做到这一点。
发布于 2022-10-31 07:52:00
当文件不包含任何测试时会出现此错误,这就是您的情况。request是一个异步函数,您不会等到它完成后再尝试运行对appList的测试,但是appList在那一刻是空的。要只在执行request之后运行测试,可以promisify请求并使用await等待结果。但是,我建议使用request来执行axios请求,而不是对API进行编程。
//runner.js
function DoRequest() {
return new Promise(function (resolve) {
setTimeout(() => {
resolve([
'test1',
'test2',
])
}, 2000)
});
}
(async () => {
global.appList = await DoRequest();
const createTestCafe = require('testcafe');
const testcafe = await createTestCafe();
await testcafe
.createRunner()
.src('test.js')
.browsers('chrome')
.run();
await testcafe.close();
})();//test.js
fixture('Getting Started')
.page('https://devexpress.github.io/testcafe/example');
for (const app in global.appList) {
test('SSO test for ' + app, async t => {
await t
.typeText('#developer-name', 'John Smith')
.click('#submit-button');
});
}https://stackoverflow.com/questions/74232936
复制相似问题