javascript - 如何使量角器 JavaScript 等待读取文件完成?
问题描述
我将测试的应用程序 URL 放在一个文本文件中,因此我希望 Protactor 执行等待而不使用“Sleep()”方法,直到它完成读取,以便 Protactor 可以使用文件中的文本启动浏览器到网站。但到目前为止,Protractor 执行速度非常快,因此无法从文件中获取文本,因此无法在 Chrome 上打开网站
function retrieveAppURL(){
const fs = require('fs')
var data = null;
fs.readFile("appURL.txt", (err, text) => {
if (err){
console.log("error " + err);
}
else
{
console.log("text file data = " + text.toString());
data = text.toString();
}
});
return data;
}
function launchAppURL(){
var data = retrieveAppURL();
browser.get(data );
browser.waitForAngularEnabled(false);
} catch (err) {
console.log("exception " + err.message);
}
}
解决方案
您可以使用 browser.wait() 来等待文件像这样读取。
await browser.wait(async function () {
return fs.readFile("appURL.txt", (err, text) => {
if (err){
console.log("error " + err);
}
else
{
console.log("text file data = " + text.toString());
data = text.toString();
}
});
}, 30*1000, "File has not been read within 30 seconds")
推荐阅读
- python - 使用python ev3进行多线程,连接方法不起作用
- docker - Powershell:使用docker时从错误流重定向输出的问题
- python - (Discord.py) 使用 on_guild_join 事件为 dm 消息添加延迟
- c - 如何查找和替换 C 字符串中的多个或所有出现
- r - 在R中仅计算自然数的差异
- core-data - 将更新保存到 Core Data 对象
- javascript - 将固定查询字符串连接或附加到 javascript 中的 url
- .htaccess - Htaccess 只允许移动设备访问目录
- python - 如何使用地图功能比较和填写数据框?
- javascript - WkWebView 应用程序不要求位置和图库权限