我正在使用Javascript,webdriverio(v2.1.2)从内部站点执行一些数据提取。内部站点启用了SSO,因此,如果我已经在另一个应用程序上进行了身份验证,则无需为此应用程序登录(在企业Intranet应用程序中很常见)。我计划实现以下目标,
检查页面上是否存在元素。如果是,那么这是一个登录页面。如果没有,那么它不是登录页面
login = function (username, password) {
if (!browserClientUtil) {
throw "Unable to load browserClientUtil.js";
}
browserClientUtil
.createClient()
.url(_Url)
.title(function (err, res) {
console.log('Title is: ' + res.value);
}) .isExisting('input#login_button.login_button', function (err, isExisting) {
browserClientUtil.getCurrentClient()
.setValue('input#USER.input', username)
.setValue('input#PASSWORD.input', password)
//.saveScreenshot('ultimatixLoginDetails.png')
.click('input#login_button.login_button')
.pause(100);
handlePostLogin();
});
};
这是最好的方法吗?我试图在单独的函数中分离用于验证登录页面的代码,但由于Webdriver中的所有事情都是作为回调的一部分发生的,因此它无法正常工作,并且不确定是否以正确的方式进行操作。我如何从回调返回,该回调又将是该函数返回的最终值?
login = function (username, password) {
if (!browserClientUtil) {
throw "Unable to load browserClientUtil.js";
}
browserClientUtil
.createClient()
.url(_Url)
.title(function (err, res) {
console.log('Title is: ' + res.value);
});
if(isThisLoginPage()){
browserClientUtil.getCurrentClient()
.setValue('input#USER.input', username)
.setValue('input#PASSWORD.input', password)
//.saveScreenshot('ultimatixLoginDetails.png')
.click('input#login_button.login_button')
.pause(100);
handlePostLogin();
}
};
isThisLoginPage = function() {
var client = browserClientUtil.getCurrentClient();
if(!client) {
throw "Unable to get reference for current client, hence cannot validate if this is login page.";
}
client.isExisting('input#login_button.login_button', function (err, isExisting) {
if(isExisting) {
return true;
}
});
return false;
};
您可以通过创建自己的包装其他命令的命令来创建自己的工作流程。例如,您可以使用自己的命令登录:
browserClientUtil.addCommand("login", function(url, user, pw, cb) {
this.url(url)
.setValue('#username', user)
.setValue('#password', pw)
.submitForm('#loginForm')
.call(cb);
});
这使您可以将“复杂的”异步webdriver操作隐藏在一个简单的函数后面。创建功能强大的工具链很容易。最后,您的测试脚本如下所示:
browserClientUtil
.login("http://example.com/login", "john.doe", "testpass")
.getTitle(function(err, title) {
console.log(title);
})
// ...
干杯
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句