javascript - How to validate if its a login page using webdriverio - Stack Overflow

I am using Javascript, webdriverio (v2.1.2) to perform some data extraction from an internal site. The

I am using Javascript, webdriverio (v2.1.2) to perform some data extraction from an internal site. The internal site is SSO enabled, so if I have been authenticated on another application, I need not login for this application (mon in enterprise intranet applications). I plan to achieve the below,

  • Create a client with required capabilities
  • Pass the required URL
  • For fun : Print the title of the page
  • Check if an element exist on the page. If yes, then it's a login page. If not, then it's not 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 the best way to do? I tried to separate the code for verifying login page in a separate function, it didn't work as everything in webdriver happens as part of callback and I am not sure if I am doing it in a right way. How do I return from a callback, that will in-turn be the final value returned by that 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;
};

I am using Javascript, webdriverio (v2.1.2) to perform some data extraction from an internal site. The internal site is SSO enabled, so if I have been authenticated on another application, I need not login for this application (mon in enterprise intranet applications). I plan to achieve the below,

  • Create a client with required capabilities
  • Pass the required URL
  • For fun : Print the title of the page
  • Check if an element exist on the page. If yes, then it's a login page. If not, then it's not 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 the best way to do? I tried to separate the code for verifying login page in a separate function, it didn't work as everything in webdriver happens as part of callback and I am not sure if I am doing it in a right way. How do I return from a callback, that will in-turn be the final value returned by that 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;
};
Share Improve this question asked Aug 27, 2014 at 10:07 rohitmohtarohitmohta 1,07114 silver badges23 bronze badges 1
  • I know that, if an inner function call is asynchronous, then all the functions 'wrapping' this call must also be asynchronous in order to 'return' a response. So, my final question would be - was my first method the best way or may be I should look into deferred and promises to make the code modular and readable? link – rohitmohta Commented Aug 27, 2014 at 10:13
Add a ment  | 

1 Answer 1

Reset to default 6

You can create your own workflow by creating own mands that wrap other ones. For example you can make an own mand to login:

browserClientUtil.addCommand("login", function(url, user, pw, cb) {
    this.url(url)
        .setValue('#username', user)
        .setValue('#password', pw)
        .submitForm('#loginForm')
        .call(cb);
});

This allows you to hide "plex" asynchronous webdriver actions behind a simple function. It is easy to create an powerful toolchain. At the end your test script looks like:

browserClientUtil
    .login("http://example./login", "john.doe", "testpass")
    .getTitle(function(err, title) {
        console.log(title);
    })
    // ...

Cheers

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646440a4638000.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信