Nightwatch: ES6 classes for commands

Created on 26 Sep 2016  路  10Comments  路  Source: nightwatchjs/nightwatch

Hi, is it possible to use es6 classes for commands?

modules.exports = class CustomCommand() {
    command() {
   }
}

At the moment I got such error: Class constructor CreateHubCommand cannot be invoked without 'new'

Any plans to support this?

Most helpful comment

@beatfactor Just curious, is there any roadmap or update on when an ES6 class API might be available? I'd like to know because it would mean we could use TypeScript (ES6 classes + static typing). We can use TS now but the the compiler has a hard time following our code because of the behind-the-scenes magic that Nightwatch does. Cheers.

All 10 comments

Nightwatch is written in ES5 and uses ES5-style class definitions to implement custom command classes. This involves subclassing custom commands and calling their constructors for super on the subclass. Because ES6 does not allow callable constructors, they are not compatible with this approach. Basically its the problem of:

class SuperClass {} // ES6

function SubClass () { // ES5
    SuperClass.call(this); // equiv of super()
}

new SubClass(); // constructor err

So to answer your first question: No, this isn't currently supported. I also doubt there's any pre-existing plan to change that, but that's what these issues are for.

There is a plan to write nightwatch in ES6, but that will take some time. Until then, I'm afraid we cannot support this.

@senocular @beatfactor Sad but true. Thx for reply

@beatfactor Just curious, is there any roadmap or update on when an ES6 class API might be available? I'd like to know because it would mean we could use TypeScript (ES6 classes + static typing). We can use TS now but the the compiler has a hard time following our code because of the behind-the-scenes magic that Nightwatch does. Cheers.

Any update here?

Any update?

I wrote a small snippet that converts basic ES6 classes (test suites or page objects) to Nightwatch compatible objects. We just pass our ES6 classes through this function and get an object out the other end that we export for Nightwatch.

Could this be useful for people in this thread?

Here is the Gist for converting ES6 classes to Nightwatch compatible objects. It doesn't cover sections yet, but that should be a small change.

It does require the class to be written in a specific way - if your classes look different then you can just modify it to fit that. There is a page object and a test suite example at the bottom of the gist to show how you need to structure your classes to export them with that function. Hope it can help someone!

It has been a a year or more since last response. Any update on ES6 re-write?

Still nothing?

Was this page helpful?
0 / 5 - 0 ratings