Luis Pablo Lopez Luis Pablo Lopez - 3 months ago 14
AngularJS Question

Protractor: How to expect browser url to equal url that have an id after registering new user?

I'm making some tests in Protractor and encountered a little problem:

I made a test to register a new user, when I finish registering it the url will be something like this:

#/user/592/profile


So, my question is, how can I manage to get the ID of the new user to get a true in my test? Something like this:

expect(browser.getCurrentUrl()).toEqual(browser.baseUrl+#/user/ID_OF_NEW_USER/profile);


How can I do this? If it's possible of course.

Answer

In your case, User_Id will be generated dynamically. So you need to write some customized code to pick the new user_id and use it in expect condition. You can follow below code snippet:

 Code: 

     browser.getCurrentUrl().then(function(url){
      var startIndex=(url.indexOf("user/"))+5;
      var endIndex=(url.indexOf(startIndex))+2;
      var user_id=url.substr(startIndex,endIndex);
      //it will return exactly user_id value
      expect(browser.getCurrentUrl())
            .toEqual(browser.baseUrl+"#/user/"+user_id+"/profile");

    });