Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Return type for beforeLaunch and onPrepare does not make sense #3431

Closed
KrauseStefan opened this issue Aug 1, 2016 · 8 comments · Fixed by #3434
Closed

Return type for beforeLaunch and onPrepare does not make sense #3431

KrauseStefan opened this issue Aug 1, 2016 · 8 comments · Fixed by #3434
Assignees
Milestone

Comments

@KrauseStefan
Copy link

The typings for the lifecycle hooks methods in protractor/config.ts should properly and any or void instead of {}

Example of current typings:

beforeLaunch?: () => {};

Current workaround needed because of the current typings:

  beforeLaunch: () => {
    launchWebServer();
    return {};
  },
@cnishina cnishina changed the title Return type for beforeLunch and onPrepare does not make sense Return type for beforeLaunch and onPrepare does not make sense Aug 1, 2016
@cnishina cnishina self-assigned this Aug 1, 2016
@cnishina cnishina modified the milestones: 4.02, 4.0.3 Aug 1, 2016
cnishina added a commit to cnishina/protractor that referenced this issue Aug 1, 2016
@igniteram
Copy link
Contributor

I did not want to raise a separate issue as I feel this is related. I am trying to use onPrepare with some browser related settings such as browser.ignoreSynchronization in my config.ts file with 4.0.3. But I am always getting an error when I run my scripts.

Note: At compile time I do not get any errors, only when I run my scripts i.e. protractor config.js I get this error

TypeError: Cannot read property 'browser' of undefined
    at Object.<anonymous> (/Users/pasalar/Documents/Coding/protractor-typescript/node_modules/protractor/globals.js:3:37)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)

This is my config.ts -

import {Config} from 'protractor';
import {browser} from 'protractor/globals';

export let config: Config = {
  framework: 'jasmine2',
  capabilities: {
    browserName: 'firefox'
  },
  specs: [ 'spec.js' ],
  directConnect: true,
onPrepare: () => {
    browser.ignoreSynchronization =true;
}
};

The error specified above points an issue at line 3 at globals.js file

exports.browser = exports.protractor.browser;

I think there were some changes to the namespace not sure what is causing the issue?

@cnishina
Copy link
Member

Yup. This is a problem... I originally had this as global['protractor']['browser']. This however is assuming that protractor is an object and it has a property of type browser. This should be fixed. Reopening issue.

@cnishina cnishina reopened this Aug 16, 2016
@cnishina cnishina added this to the Upcoming milestone Aug 16, 2016
@cnishina
Copy link
Member

@igniteram From my investigation, if you import the file, protractor/global, values are assigned at import from global. Since protractor generates the global values after it reads in the config file, this could result in having null values. This is definitely a weird gotcha.

So let's say you do not use onPrepare but you use import {browser} from 'protractor/globals';, the way Protractor works, none of your tests will pass. So this is my thought how to get this to work in TypeScript land for the config file:

import {ProtractorBrowser, Config} from 'protractor';

export let config: Config = {
  onPrepare: () => {
    // after the globals are set, you can read the globals
    let globals = require('protractor/globals');

    // then you can assign the type of browser here.
    let browser: ProtractorBrowser = globals.browser;
    console.log('browser:', browser);
  }
}

@KrauseStefan
Copy link
Author

This is the same other Issue that I mentioned in #3430.
Since the values are already globally available, I simply do like this

(<any> browser).ignoreSynchronization(...)

Without importing

@cnishina
Copy link
Member

Yup, that could be done too. As long as you do not import browser from 'protractor/globals' before the global variable is set up.

@igniteram
Copy link
Contributor

Didn't get time to comment on this issue, yes I had this feeling that globals were available after import but it would not work somehow, now this clears it up for me and I am using the above work around and its working. I think the imports will change in the coming releases. @cnishina please include unit tests in releases! just kidding 😄

@cnishina
Copy link
Member

I'll have more information on https://github.com/angular/protractor-cookbook

Going to close this issue.

@thorn0
Copy link
Contributor

thorn0 commented Oct 20, 2016

The current solution looks like a hack. Would be great if browser (or even better protractor) simply was passed to onPrepare as a parameter.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants