1

I have a variable in protractor.conf.js which is exported within onPrepare() function as: global.myVariable = myVariable, and in spec file I'am trying to use/call it, but it seems that it can't be accessible and isn't available for the spec file.

Error:

E/launcher - Error: Error: Cannot find module 'myVariable'

It's being treated as it would be an npm module and can't be found in npm_modules which is correct.

myVariable is actually an object build by a constructor function within protractor.conf.js

How can make it accessible from the spec file?

  • Vincent, would you care to explain in which scenarios you needed to expose a variable globally and access directly in the spec file, as generally it is considered a bad practice . – Vishal Aggarwal Feb 9 '18 at 9:49
  • And in case if you have to do it, have it in separate globals.js file and keep config.js light. – Vishal Aggarwal Feb 9 '18 at 9:55
  • @VishalAggarwal: Sure, I can add more input and info regarding the background of my question... I'll update my question then. – k.vincent Feb 9 '18 at 10:47
  • @VishalAggarwal: Sorry for the late answer! here is the background of my question: I do have a test case where a user registers via UI and then gets an email in the inbox. The email contains a registration confirmation link. I'am using a mailListener library and its initialization is being done in protractor.config.js as: var MailListener = require("mail-listener2"); var mailListener = new MailListener({....}). Therefor I have to pass a variable global.mailListener = mailListener to *.spec.ts file and then invoke a method on it. – k.vincent Feb 12 '18 at 14:08
  • @VishalAggarwal: Now I'am facing other issue if you could help! Here is the issue I'am dealing with right now: E2E: Retrieve email-link – k.vincent Feb 12 '18 at 14:15
2

Issue fixed... solution is:

params: {
    myVariable: function () {
        ...
        ...
    },
    otherProperty: 'Value'
}

and this how you can reach in spec files:

browser.params.myVariable or browser.params.otherProperty

with this we can make a variable accessible from protractor.conf.js to spec test files. The following posting was helpful and also the protractor doc where it's explaining the property: params?: any;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.