angular / protractor

E2E test framework for Angular apps
http://www.protractortest.org
MIT License
8.75k stars 2.31k forks source link

browser.get inside config.onPrepare breaks after upgrade to 2.0.0 #1978

Closed glepretre closed 9 years ago

glepretre commented 9 years ago

As I said in #1962, our angular app is manually bootstrapped (mainly because of requireJS) but it never caused any issue with Protractor.

Here is the app_test.js (this is the real test, we are starting a new project, with just a Hello World directive for now, while building the app structure).

/* global browser, element, by */

describe('project', function() {
  'use strict';

  describe('startup', function() {
    it('should display "Hello World!"', function() {
      var title = element(by.tagName('h1'));

      expect(title.getText()).toEqual('Hello World!');
    });
  });
});

protractor.conf.js

exports.config = {
  seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

  troubleshoot: true,

  specs: [
    '../e2e/**/*_test.js'
  ],

  multiCapabilities: [
    {'browserName': 'chrome'},
    {'browserName': 'firefox'}
  ],

  baseUrl: 'http://localhost:8000',

  onPrepare: function() {
    browser.driver.manage().window().maximize();
    browser.get('e2e/index.html');
  },

  jasmineNodeOpts: {
    isVerbose: true
  }
};

output

$ protractor protractor.conf.js
Starting Protractor (https://github.com/angular/protractor)
-------------------------------------------------------------------
DEBUG - Running with --troubleshoot
DEBUG - Protractor version: 2.0.0
DEBUG - Your base url for tests is http://localhost:8000
[launcher] Running 2 instances of WebDriver
F
------------------------------------
[chrome #1] PID: 12941
[chrome #1] Specs: /home/gilles/project/e2e/app_test.js
[chrome #1] 
[chrome #1] Using the selenium server at http://127.0.0.1:4444/wd/hub
[chrome #1] DEBUG - WebDriver session successfully started with capabilities { caps_: 
[chrome #1]    { platform: 'LINUX',
[chrome #1]      acceptSslCerts: true,
[chrome #1]      javascriptEnabled: true,
[chrome #1]      browserName: 'chrome',
[chrome #1]      chrome: { userDataDir: '/tmp/.com.google.Chrome.sgJvV0' },
[chrome #1]      rotatable: false,
[chrome #1]      locationContextEnabled: true,
[chrome #1]      mobileEmulationEnabled: false,
[chrome #1]      'webdriver.remote.sessionid': 'a09b3c2e-9314-47dc-9d84-a6c30bc854d4',
[chrome #1]      version: '41.0.2272.101',
[chrome #1]      takesHeapSnapshot: true,
[chrome #1]      cssSelectorsEnabled: true,
[chrome #1]      databaseEnabled: false,
[chrome #1]      handlesAlerts: true,
[chrome #1]      browserConnectionEnabled: false,
[chrome #1]      nativeEvents: true,
[chrome #1]      webStorageEnabled: true,
[chrome #1]      applicationCacheEnabled: false,
[chrome #1]      takesScreenshot: true } }
[chrome #1] project
[chrome #1]   startup
[chrome #1]     should display "Hello World!" - fail
[chrome #1] 
[chrome #1] 
[chrome #1] Failures:
[chrome #1] 
[chrome #1]   1) project startup should display "Hello World!"
[chrome #1]    Message:
[chrome #1]      Error while waiting for Protractor to sync with the page: "angular could not be found on the window"
[chrome #1]    Stacktrace:
[chrome #1]      undefined
[chrome #1] 
[chrome #1] Finished in 0.243 seconds
[chrome #1] 1 test, 1 assertion, 1 failure
[chrome #1] 

(Same for firefox #2 so I don't think it would be relevant to add its output too).

I can see that the browser.driver.manage().window().maximize(); is being called but I think browser.get('e2e/index.html'); isn't.

The address bar contains data:, instead of http://localhost:8000/project/e2e/index.html.

If I move the browser.get() inside the test itself, it works and pass.

Please tell me if you need more info or if you want me to try ideas here :wink:

igorshubovych commented 9 years ago

I have the same issue.

juliemr commented 9 years ago

I can't reproduce this. Here's a config and test that I used to verify that browser.get gets called:

exports.config = {
  directConnect: true,

  capabilities: {
    'browserName': 'chrome'
  },

  framework: 'jasmine2',

  specs: ['example_spec.js'],

  onPrepare: function() {
    browser.driver.manage().window().maximize();
    browser.get('http://juliemr.github.io/protractor-demo');
  }
};
describe('site', function() {
  it('should do stuff', function() {
    expect(browser.getTitle()).toEqual('Super Calculator');
    expect(true).toBe(true);
  });
});

Can you modify this to get the same failure you're seeing?

glepretre commented 9 years ago

directConnect does not affect the issue (same error with or without).

It works with framework: 'jasmine2'. Here's the output:

Starting Protractor (https://github.com/angular/protractor)
-------------------------------------------------------------------
DEBUG - Running with --troubleshoot
DEBUG - Protractor version: 2.0.0
DEBUG - Your base url for tests is http://localhost:8000
[launcher] Running 2 instances of WebDriver
.
------------------------------------
[chrome #1] PID: 26947
[chrome #1] Specs: /home/gilles/project/e2e/app_test.js
[chrome #1] 
[chrome #1] Using the selenium server at http://127.0.0.1:4444/wd/hub
[chrome #1] DEBUG - WebDriver session successfully started with capabilities { caps_: 
[chrome #1]    { platform: 'LINUX',
[chrome #1]      acceptSslCerts: true,
[chrome #1]      javascriptEnabled: true,
[chrome #1]      browserName: 'chrome',
[chrome #1]      chrome: { userDataDir: '/tmp/.com.google.Chrome.GgR1hJ' },
[chrome #1]      rotatable: false,
[chrome #1]      locationContextEnabled: true,
[chrome #1]      mobileEmulationEnabled: false,
[chrome #1]      'webdriver.remote.sessionid': '51c51a3e-6ba6-4165-9fe6-255ff76fcf6b',
[chrome #1]      version: '41.0.2272.101',
[chrome #1]      takesHeapSnapshot: true,
[chrome #1]      cssSelectorsEnabled: true,
[chrome #1]      databaseEnabled: false,
[chrome #1]      handlesAlerts: true,
[chrome #1]      browserConnectionEnabled: false,
[chrome #1]      nativeEvents: true,
[chrome #1]      webStorageEnabled: true,
[chrome #1]      applicationCacheEnabled: false,
[chrome #1]      takesScreenshot: true } }
[chrome #1] Started
[chrome #1] .
[chrome #1] 
[chrome #1] 
[chrome #1] 1 spec, 0 failures
[chrome #1] Finished in 4.207 seconds

(Same for firefox)

We were considering start using jasmine 2 for a while now. I think we are going to use it for new projects (beginning with this one) but it will take us some time for older projects since we have around one hundred of e2e tests using Protractor/Jasmine 1.

Could it be related to #1981 and #1885?

juliemr commented 9 years ago

Ah yes, I can reproduce now, with my config above but with framework: 'jasmine'! Thank you. I think this is related to #1981. I'm pretty sure this is due to changes in WebDriverJS and how it handles scheduling to the control flow.

The easy fix is that onPrepare will force a wait for any promise returned within it to resolve. So, you can change to:

  onPrepare: function() {
    browser.driver.manage().window().maximize();
    return browser.get('http://juliemr.github.io/protractor-demo'); // Added return statement here
  }

and it will work again in both jasmine 1.3 and jasmine 2.

I'm adding a note about this to the changelog!

glepretre commented 9 years ago

The "easy fix" works like a charm! :ok_hand: Thank you for your help. We have started migrating to jasmine 2, it is not as difficult as I expected it to be.

But it's good to know that our older projects will still benefit from an up-to-date Protractor version while waiting for the migration to be complete. :grinning:

mms27 commented 9 years ago

@glepretre "It works with framework: 'jasmine2'" It worked for me as well, thanks!

pleerock commented 9 years ago

I have the same issues, but Im using 'mocha' as a framework, and I cant change it to jasmine since everything is written using mocha. any easy fix here too?

juliemr commented 9 years ago

This should still work for you.

61ue5peed commented 8 years ago

worked

NaveenKVN commented 8 years ago

Even I am getting the same error while trying to run protractor with Cucumber. When I am running in my local machine it is working fine. but when the test is triggered from Team-city on the deployed server I am getting the error. I am using selenium 2.45 jar. Please help.

NaveenKVN commented 8 years ago

leme try this solution https://github.com/angular/protractor/issues/1992

idaneliz commented 8 years ago

I can't run my test cases if i changed the framwork from "jasmine" to "jasmine2" in the conf file this issue doesn't recreate in my local machine only when I run it on Linux by Jenkins. Can you please help me to enlarge the screensize to maximum (or to simulate a widescreen) ? thanks.