Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor: Save url as string for later redirect

How can I save the current URL as a String? I want to navigate somewhere else and later navigate back to the saved URL. I dont want to use 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 fullfilled and urlString is undefined

Is there a possiblity to do this without breaking the test-flow?

like image 754
Alex Avatar asked Feb 03 '26 23:02

Alex


1 Answers

Try this:

  1. Put you test inside a describe.
  2. Add a before each to save the url.
  3. 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
  });
})

... or you can put all of your test inside the then of the getCurrentUrl()

like image 181
Andres D Avatar answered Feb 06 '26 14:02

Andres D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!