2

Have this method in a Protractor page object test:

public navigateTo = (): Promise<any> => {
    return browser.get('https://material.angular.io/');
};

Want to define the type that is returned i.e. Promise<any>.

Should I import the jasmine promise? Or the webdriver promise?

import * as webdriver from "selenium-webdriver";
import Promise = webdriver.promise.Promise;

or

import Promise = jasmine.Promise;

I'm confused about why I need to specify an implementation of promise. Can I specify an interface for Promise?


edit: What I really wanted to do was:

npm install --save-dev @types/es6-promise

This gave me the Promise<any> type.

Relevant Question: How to use Typescript with native ES6 Promises

Eric Francis
  • 23,039
  • 31
  • 88
  • 122
  • 1
    what's the problem with ES6 Promise and why don't you use it instead? – smnbbrv Feb 08 '17 at 21:32
  • I get a `TSError: Unable to compile TypeScript practice.e2e-spec.ts : Cannot find name 'Promise'` So maybe I can use ES6 promise, but I don't have a type for it? – Eric Francis Feb 08 '17 at 21:40

2 Answers2

3

Importing webdriver.promise.Promise

Most API calls to selenium-webdriver return a webdriver.promise.Promise. These promises are executed in order via the control flow. Recently they have switched from their own implementation of promises to native ES6 style promises.

Do you need to import webdriver.promise.Promise into your Protractor test? Probably not.

Importing jasmine.Promise

jasmine.Promise does not come from the jasmine typings but it comes from the jasminewd2 typings. This is a helper to get the rest of the typings to work. Should you import jasmine.Promise to your test? Absolutely no.

Protractor appears synchronous

My argument that you do not need to import webdriver.promise.Promise is that Protractor helps synchronize the promises. You do not need to know the result of browser.get(URL). It is a promise and that is done for you. So the only reason why you want to import this, is if you are creating a new webdriver.promise.Promise.

Also, I like to import webdriver promises like:

import {promise as wdpromise} from 'selenium-webdriver'

let wpromise = new wdpromise.Promise(resolveCallback, rejectCallback);

// a native promise
let npromise = new Promise(resolveCallback, rejectCallback);

I would avoid importing as Promise since it is also how you create native ES6 promises. See above.

cnishina
  • 5,016
  • 1
  • 23
  • 40
  • Gotcha. Thank you very much. I would like `navigateTo`'s return type to be `Promise`. I've found this question: https://stackoverflow.com/questions/27573365/how-to-use-typescript-with-native-es6-promises#27573589/. This is probably why I'm confused. I came to my answer because Intellij prompted me to import those types when I defined return type `Promise`. – Eric Francis Feb 09 '17 at 15:09
  • Ended up doing `npm install --save @types/es6-promise`. That is what I wanted, but I did not understand what I wanted when I asked the question :). Thanks for your answer. – Eric Francis Feb 09 '17 at 15:27
  • 1
    Regarding `jasmine.Promise`, this was changed in https://github.com/DefinitelyTyped/DefinitelyTyped/commit/d131a79ae894f23d26b8fac5859f040853ae2817 and not the global `Promise` is used. – splintor Feb 13 '18 at 07:53
  • That makes sense. I'm glad this is getting cleaned up. – cnishina Feb 13 '18 at 07:57
1

I am not an expert on TypeScript to answer your second question - I'm confused about why I need to specify an implementation of promise. But I can answer the first part of it

Should I import the jasmine promise? Or the webdriver promise?

Different Protractor commands return a promise of different type - But mostly it is webdriver.promise.Promise. The source of truth for this information is the Protractor API docs

Some examples

         sendKeys() - webdriver.promise.Promise.<void>
         executeScript() - promise.Promise<T>
         getText() - webdriver.promise.Promise.<string>
         restart() - webdriver.promise.Promise<ProtractorBrowser>
         isPresent() - Promise<boolean>

browser.get() returns a webdriver.promise.Promise

AdityaReddy
  • 3,625
  • 12
  • 25