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

Write protractor tests in ES6 #2049

Closed
Closed

Description

@jonricaurte

Hi,

Saw there was discussion about using coffeescript to write protractor tests, however, I didn't see any that involved ES6. I see the current pattern is to write tests in ES6 and have a gulp task compile them into a separate directory and then have protractor use the compiled files for testing. It would be nice to have something similar to karma-babel-preprocessor to write tests with where we don't need a gulp task.

Thanks.

Activity

  1. juliemr commented on Apr 16, 2015

    @juliemr
    Contributor

    See also #1214

    Many ES6 features are already available in node, provided you have the right flags. What features, specifically, are you looking for? I wonder if the ideal way to do this is to wait for es6 features to hit node so that compilation can be avoided altogether.

  2. jonricaurte commented on Apr 16, 2015

    @jonricaurte
    Author

    I was looking to use classes and arrow functions, which don't seem to be stable. Since coffeescript seems to be supported out of the box, is it possible to have ES6 also be supported by using babel or traceur?

  3. juliemr commented on Apr 16, 2015

    @juliemr
    Contributor

    I'd prefer not to build preprocessors in to the core framework - I think doing this as a gulp task makes the most sense for now.

  4. jonricaurte commented on Apr 17, 2015

    @jonricaurte
    Author

    Ok, that works for me.

  5. jamieconnolly commented on May 9, 2015

    @jamieconnolly

    Stumbled across this issue after finding my own solution for the same thing. It seems my (somewhat basic) needs are fulfilled by adding require('babel/register'); to the prototractor configuration file. I can then use ES6 to write my tests 👍

  6. datatypevoid commented on Jul 20, 2015

    @datatypevoid

    @jamieconnolly Your solution works perfectly for my end to end testing needs. Thanks!

  7. jonricaurte commented on Jul 21, 2015

    @jonricaurte
    Author

    @jamieconnolly Thanks for the tip! I totally forgot about doing it this way; this is how we used to have to do it for running coffeescript tests before it was built in.

  8. stringbeans commented on Sep 22, 2015

    @stringbeans

    @jamieconnolly that method allows us to use ES6 to write our tests, however if there are any errors it doesn't report the line numbers correctly. Do you know how to utilize source maps in this case?

  9. tsubik commented on Oct 7, 2015

    @tsubik

    @stringbeans I've also had the same problem. require('babel/register') generates and installs source maps by itself, but everything is done on the fly. Protractor also try to install source maps, but checking source files from disk which are not changed at all. Looks like this line overrides good source maps loaded by require('babel/register') so as a workaround I'm invoking that hook later in onPrepare function.

    exports.config = {
        framework: "jasmine2",
        baseUrl: "http://localhost:4200",
        seleniumAddress: "http://localhost:4444/wd/hub",
        specs: ["integration/**/*.js"],
        capabilities: {
            "browserName": "chrome"
        },
        onPrepare: function () {
            require("babel/register");
        }
    };
    

    Other option could be add some config for protractor to disable loading source maps.

  10. PinkyJie commented on Nov 3, 2015

    @PinkyJie

    @tsubik Works for me, thanks!

  11. dfguo commented on Nov 25, 2015

    @dfguo

    @tsubik i still couldn't get proper line number after moving require("babel/register"); to onPrepare. Did you manage to get it working for you?

  12. PinkyJie commented on Nov 26, 2015

    @PinkyJie

    @dfguo My full protractor.config.js is here https://github.com/PinkyJie/angular1-webpack-starter/blob/master/protractor.config.js, check this to see if it could help.

  13. vredchenko commented on Dec 16, 2015

    @vredchenko

    Hi, came here looking for solution to the proper line numbering problem. Just want to confirm that @PinkyJie method of moving babel/register to onPrepare() worked for our project, too. Thanks! :)

  14. 0xR commented on Dec 17, 2015

    @0xR

    For me putting babel/register in onPrepare didn't work. Running protractor 2.2.0.

  15. 0xR commented on Dec 17, 2015

    @0xR

    As a workaround this seems to help: require('babel/register')({retainLines: true});
    See https://babeljs.io/docs/usage/options/

  16. nsteenbeek commented on Jan 7, 2016

    @nsteenbeek

    Since Babel 6 you have to use require("babel-core/register")({presets: ["es2015"]}) to get it working.

  17. PeterStegnar commented on Nov 11, 2016

    @PeterStegnar

    require("babel/register"); Is old name.

    You should use: require("babel-register");

  18. hirunatan commented on Dec 7, 2016

    @hirunatan

    Thanks @nsteenbeek and @PeterStegnar, searched a lot of sites, but this was what i needed. Now it works!

  19. PeterStegnar commented on Dec 7, 2016

    @PeterStegnar

    @hirunatan no problem, happy to help!

  20. adityagautam commented on Jun 21, 2017

    @adityagautam

    Can some one tell me what is wrong in my onPrepare function as I am not able to use ES6. Also getting syntax error saying can't use let or arrow functions.

    onPrepare: () => {
             require("babel-register");
             require("babel-core/register")({presets: ["es2015"]});
    
           let origFn = browser.driver.controlFlow().execute;
    
            browser.driver.controlFlow().execute = function () {
                let args = arguments;
    
                origFn.call(browser.driver.controlFlow(), function () {
                    return protractor.promise.delayed(this.delayBrowserTimeInSeconds * 100);
                });
    
                return origFn.apply(browser.driver.controlFlow(), args);
            };
    
            browser.driver.manage().window().setSize(window.screen.availWidth, window.screen.availHeight);
            browser.driver.manage().window().maximize();
        }
    
  21. wswebcreation commented on Jun 22, 2017

    @wswebcreation
    Contributor

    @adityagautam

    You are reacting on a closed issue and secondly asking a question that is not related to Protractor. I don't think it's that wise to do that.

    Having said that, your error suggests that you are on a version of node that is equal to 4.x. Protractor requires 6.9 at least. If you upgrade you will see this will solve your initial problem.

    Secondly. Are you using babel to transpile your ES6 to ES5? If so, then your code isn;t logic,m because you are running an arrow function (ES6) to call Babel to transpile ES6 to ES5. Or am I wrong?

  22. geyuqiu commented on Jun 1, 2018

    @geyuqiu

    Since babel 7 it is now

    require("@babel/register");

    after

    npm install --save-dev @babel/register

    of course :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions