0

Im learn Selenium Webdriver. I cant run script with import classes, but i can run it without classes, just with import function. Im run it with babel-cli like this node ./babel-cli/bin/babel-node.js --presets node6 ./test.js

//test.js
import homePage from "./home_page"

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until;

var driver = new webdriver.Builder()
    .forBrowser('firefox')
    .build();

home_page = new homePage(driver);
home_page.go_home_page();

//home_page.js
export default class homePage{
    constructor(driver){
        this.driver = driver;
    }
     go_home_page = function(){
        this.driver.get("https://www.google.com/");
    }
}

error is :home_page.js: Unexpected token (5:15)

Andrey76ru
  • 37
  • 3

2 Answers2

1

Node was originally built with CommonJS modules, and the transition to ES2015 modules will not be easy.

Your example is using both: import is ES2015, and require is CommonJS. If you change your import to require it should work.

The CommonJS equivalent for that import should look like:

var homePage = require('./home_page');
Adam Lassek
  • 34,137
  • 13
  • 87
  • 104
  • See also: https://stackoverflow.com/questions/37132031/nodejs-plans-to-support-import-export-es6-es2015-modules – Adam Lassek Jun 20 '17 at 02:27
  • Since you're using babel, it _should_ be possible to use `import` but I'm not familiar enough to tell you what needs to be done. I do know that the target ES version and module system needs to be configured specifically, although the preset should do a lot of that work for you. – Adam Lassek Jun 20 '17 at 02:46
  • Can you tell me how import my class with require? Please. – Andrey76ru Jun 20 '17 at 02:47
-1

es6 need to use babel to transpile. you need to include babel in your package json file.