Currently, I am utilizing Javascript and webdriverio (v2.1.2) for the purpose of data extraction from an internal site that is SSO enabled. If authentication has already been done on another application, there is no need to log in again for this specific application, which is a common scenario in enterprise intranet applications. Here is my plan:
- Create a client with the necessary capabilities
- Provide the required URL
- Just for fun: Print out the title of the page
Determine if a certain element exists on the page. If it does, then it is a login page. If not, then it is not a login page.
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(); });
};
Is this approach the most optimal one? I attempted to separate the code for verifying the login page into a distinct function, but it did not yield the desired results as everything in webdriver functions asynchronously through callbacks and I am uncertain if I am implementing it correctly. How can I efficiently return a value from a callback function that will ultimately be the final output of that specific function?
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;
};