Jonathan002
Jonathan002

Reputation: 10278

How can I know the base url used in a running test in protractor?

I'm trying to do navigation test in protractor and don't see any consitency with the baseUrl in the config and the url used in the test.

protractor.conf.js

exports.config = {
  baseUrl: 'http://localhost:4200/'
}

navbar.e2e-spec.ts

import { NavbarPage } from './navbar.po';
import * as protractor from './../protractor.conf.js';
describe('navbar', () => {
  let navbar: NavbarPage;
  const baseUrl = protractor.config.baseUrl; 

  beforeEach(() => {
    navbar = new NavbarPage();
    browser.get('/');
  });
  it(`should see showcase nav item, be able to (click) it,
  and expect to be navigated to showcase page`, () => {
    const anchorShowcase = navbar.anchorShowcase;
    expect(anchorShowcase.isDisplayed()).toBe(true);
    anchorShowcase.click();
    browser.waitForAngular();
    expect(browser.getCurrentUrl()).toBe(baseUrl + '/showcase');
  });
});

Although when I run the e2e test it uses a different port:

** NG Live Development Server is listening on localhost:49154, open your browser on http://localhost:49154/ **

Why is the test url set to port 49154. This apparently seems to be the default if you start a new angular-cli project: https://github.com/angular/angular-cli

How can I get control over the baseUrl / Or is http://localhost:49154/ safe to use for all my angular cli projects?

Upvotes: 3

Views: 2330

Answers (3)

coder87
coder87

Reputation: 167

Aniruddha Das's solution doesn't work anymore as this option isn't there from Angular CLI 6.x version, you can try following -

ng e2e --dev-server-target=

please see following reference

Upvotes: 0

Aniruddha Das
Aniruddha Das

Reputation: 21688

By default when you do ng e2e the command take --serve value as true. It means it will build and serve at that in a particular URL. Not the baseUrl you passed in protractor.conf.js

that is why, you are getting a random URL served when testing you app like http://localhost:49154/

Now as you don't want build during test and want to test existing build (URL) like http://localhost:4200/ you need to pass --no-serve in your command line and it will pick baseUrl from the protractor.conf.js

you can also pass baseUrl in the command line like below. note that this not baseUrl but --base-href=

ng e2e --no-serve --base-href=https://someurl.com:8080

Upvotes: 2

Willwsharp
Willwsharp

Reputation: 723

When running Angular CLI's ng e2e command, it states in the wiki that the default port will be random, as seen here:

https://github.com/angular/angular-cli/wiki/e2e

Under the serve submenu.

The e2e command can take in all the same arguments as serve so to keep the port the same just pass in --port my-port-number to the ng e2e command.

As far as that port being safe to use, I wouldn't use it, it is just a random port after all. I would stick to the default unless you have a use-case for changing it. The port is mainly relevant for the dev server, not so much for where ever the production code runs.

Upvotes: 1

Related Questions