Search code examples
protractorcodeceptjs

How to call a customhelper in CodeCeptjs?


How to call a method defined in customhelper?

MycustomHelper

const { Helper } = codeceptjs;
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const expect = chai.expect;

class MyHelper extends Helper {

  async JavaScriptclick(LocatorVale)  {
    await browser.waitForAngularEnabled(true);       
    browser.executeScript("arguments[0].click();", element(by.xpath(LocatorVale))); 

  }

}

module.exports = MyHelper;

Code.js

helpers: {
    MyHelper: {
      require: './myhelper_helper.js',
    },

Stepfile.js

Scenario('Add a new user group', (I, MyHelper) => {  
   MyHelper.JavaScriptclick(‘.badged-button.mat-raised-button.mat-primar')
});

If I execute the above code, I get the below error

   Add a new user group:
     Object of type MyHelper is not defined in container  

Please help me to resolve this issue. I need to click on a button, but I.click is not able to locate the button. So I had to use javascriptclick in this case. However I.executescript is also not working. Hence I need to use native protractor commands as it works only this way.


Solution

  • There are no need to call helper by it's name. All methods are added in actor (I) object

    As said in documentation:

    Methods of Helper class will be available in tests in I object. This abstracts test scenarios from the implementation and allows easily switching between backends.

    https://codecept.io/helpers/#extending-codeceptjs-with-custom-helopers

    So, in test:

    Scenario('Add a new user group', (I) => {  
       I.JavaScriptclick(‘.badged-button.mat-raised-button.mat-primar')
    });