Protractor: save url as string for later redirection

How do I store the current url as a string? I want to navigate to a different location and then return to the saved url. I don't want to use it browser.navigate().back()

for this.

When I, for example, do

var urlString;

browser.getCurrentUrl().then(function(url) {
    urlString = url;
});

....redirecting...navigating...


browser.waitForAngular();
browser.get(urlString); // get back to beginning URL

      

it fails because the promise has not been filled and urlString

-undefined

Is it possible to do this without breaking the test flow?

+3


source to share


1 answer


Try the following:

  • Test inside the description.
  • Add before each to save the URL.
  • Run the test, the value should be resolved.



describe('my test', function() {
  var urlString;

  beforeEach(function() {
    browser.getCurrentUrl().then(function(url) {
      urlString = url;
    });
  });

  it('should go and come back', function() {
    // Here the value has already been resolved.
    ....redirecting...navigating...

    browser.waitForAngular();
    browser.get(urlString); // get back to beginning URL
  });
})
      

Run codeHide result


... or you can put your whole test inside then

getCurrentUrl()

+1


source







All Articles