1

I have a protractor test that locates a URL on a page. The URL is then clicked and expected to contain specific text. The URL needs to be manipulated before the browser opens it, how can I change the URL before hand?

The URL to be clicked looks like app.company.com but needs to be changed to app-dev.company.com or app-staging.company.com to function correctly. If app.company.com is opened, the URL auto re-directs to an error page.

Edit - below is the test in question. When openURL is clicked, its automatically redirected to an error page because the URL is incorrect:

customizeWebsite.openURL.click();
browser.sleep(10000);
browser.getAllWindowHandles().then(function(handles){
browser.switchTo().window(handles[1]).then(function(){
expect(browser.getCurrentUrl()).toContain('gid=');
1
  • Are you starting with a production server URL then expecting to go to a test server URL later? Or are you starting with a test server, but the links in the test server point to prod for some reason? If it's the latter, why are the links absolute instead of relative?
    – ernie
    Jul 13, 2017 at 22:20

1 Answer 1

1

Determining which environment is currently under test

  1. Using baseUrl configuration parameter. In Protractor, you can either pass the --baseUrl command-line parameter or set the baseUrl setting in your configuration. This is a common way to parameterize your tests - execute the same tests against different environments.

    This though requires not hardcoding absolute URLs in your tests. E.g. instead of using browser.get("https://dev.app.com") use browser.get(browser.params.baseUrl) and having baseUrl coming from an environment-specific configuration or from a command-line.

  2. Using browser.getCurrentUrl() and extract the environment information from a current URL.

Changing the link hostname

We can actually use the .hostname property of an a element:

var yourLinkElement = $("#yourLinkId");

browser.getCurrentUrl().then(function (currentUrl) {
    var desiredHostName = (new URL(currentUrl)).hostname;

    browser.executeScript("arguments[0].hostname = arguments[1];", 
                          yourLinkElement.getWebElement(),
                          desiredHostName)

     // logging the link href after the update
     yourLinkElement.getAttribute("href").then(console.log);
});
yourLinkElement.click();

(not tested)

This would replace the domain name of a link to a domain name of the current url.

6
  • What additional information can I provide? I am not using baseUrl at this point, I need to get the tests working and plan on re-factoring dupe code at a later point. The page object file locates the link to be clicked by: this.openURL = element(by.css('a[href*="specific text"]')); The URL that is clicked always points to production but it needs to point to a test environment otherwise, it'll error out. So before the test clicks the URL, it needs to change where its looking. The URL's are always going to be different (app.company.com/#/1234) with the numerical value changing.
    – kylokyler
    Jul 13, 2017 at 14:36
  • @Kelsey that is actually helpful, thanks. What if you would make the test environment-agnostic, is that possible? For instance, you may apply a partial match without checking the app or app-staging part - is it an option?
    – alecxe
    Jul 13, 2017 at 14:38
  • Unfortunately its not. The Url needs to be changed to look at the test environment because the automated tests is creating data that only exist on test environments (doesn't propagate to production). When the production URL is opened, the test fails because the data doesn't match.
    – kylokyler
    Jul 13, 2017 at 14:45
  • @Kelsey gotcha. Could you please tell me how do you execute your tests - do you have different configs for different environments or not? Basically, I want to know at what step do you know if you are on dev, stage or prod. Thanks.
    – alecxe
    Jul 13, 2017 at 14:52
  • Right now, the tests are hard coded to look at dev only with one config file. I inherited these tests and they need to be updated before we run them on staging or production. Once the tests get to a stable place, I planned on running a find/replace anytime they need to be ran on different environments until I can update everything to use global variables.
    – kylokyler
    Jul 13, 2017 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.