codeceptjs / CodeceptJS

Supercharged End 2 End Testing Framework for NodeJS
http://codecept.io
MIT License
4.11k stars 723 forks source link

The same "I" instance being used in step runtime context cant be injected in custom helper #4520

Open attilagyoriid opened 1 day ago

attilagyoriid commented 1 day ago

What are you trying to achieve?

to inject the same "I" instance as being used in step runtime context in custom helper

What do you get instead?

undefined or different "I" instance

Provide test source code if related I tried it to achieve the I injection in many ways

1.
class MyHelper extends Helper {

constructor(prop) {
    super();

    const {I} = inject(); → "I" is undefined

}
....
2.
class MyHelper  extends Helper { 

myMethod = message => {
    const {I} = inject(); → "I" it is not the same instance being used in the context of step execution
};
.....
3.
class MyHelper  extends Helper

{

async _before() {

    const {I} = inject(); → "I" it is not the same instance being used in the context of step execution
};

Details

codecept.conf.js:
...
module.exports = {
  name: 'project',
  tests: '../src/tests/**/*_test.js',
  output: '../output',
  bootstrap: null,
  helpers: {  
    MyHelper: {
      require: '../src/lib/helpers/MyHelper.js',
    }
  },
......

The question: Is there a way to get a hold of the same "I" instance being used during step exec and inject it into custom helper classes?

Thanks for your help and answer

Best Regards

Attila

kobenguyent commented 18 hours ago

hey @attilagyoriid per my best knowledge, the I actor is not designed to be injected in custom helper. https://codecept.io/helpers/#extending-codeceptjs-with-custom-helpers

perhaps @DavertMik could explain it better than myself.

attilagyoriid commented 4 hours ago

Dear @kobenguyent thanks for the pretty fast reply

So If I understand correctly, custom helper classes are designed to implement those activities can not be achieved using I actor?

Other question being important: In case of Page Object Model, can it be achieved to inject the same actor I instance being used during step context?

Thanks for your answer

Best Regards

Attila

kobenguyent commented 4 hours ago

As far as my experience, with PO, the I object is possible, please check a sample report here https://github.com/kobenguyent/amazon-ui-tests/blob/master/pages/ResultPage.js

Or official guidelines https://codecept.io/pageobjects/#stepobjects

Hope that helps