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?

5
  • 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 . Commented Feb 9, 2018 at 9:49
  • And in case if you have to do it, have it in separate globals.js file and keep config.js light. Commented Feb 9, 2018 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
    Commented Feb 9, 2018 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
    Commented Feb 12, 2018 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
    Commented Feb 12, 2018 at 14:15

2 Answers 2

3

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;

2
  • This works for getPageTimeout , but any other variable e.g serverURL, doesnt work Commented Sep 16, 2021 at 12:04
  • @DhananjaySonar, Could you pls. add more details? It should normally work for any other variable.
    – k.vincent
    Commented Sep 17, 2021 at 12:48
0

I just put a config in my protractor.conf.js with a variable named getPageTimeout and then in my spec.ts I can reference it with browser.getPageTimeout

enter image description here

Wierd though that I had to set the timeout in the Cucumber framework rather than the Jasmine/Protractor framework. I don't quite understand why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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