0

Firstly great testing tool alternative here guys as I heavily use selenium. This might be basic but kind of new to javascript and node.js in paticular.

I would like to be able to use global variables in my scripts but am having trouble doing so

I have a file called variables on the same level as my test.js script for example, and in this example i am assigning google to the variable url, but when running the test it fails to load.

require('variables.js');

module.exports = {
'Log into Traceiq': function (test) {
 test
 .open(url)
 .done();
}
};

I then tried this and the test starts to run but hangs at the opening stage

require('./variables');

module.exports = {
'open Google': function (test) {
 test
  .open(url)
  .done();
}
}; 

This is the output in the console

 Running tests
 Running Browser: Google Chrome
 OS: Linux 3.5.0-45-generic x86_64
 Browser Version: 31.0.1650.63
 >> WARNING: done not called!

 RUNNING TEST - "Open Google"
✔ 0 Assertions run
✔ TEST - "Open Google" SUCCEEDED

Is there something blindly obvious that i am doing wrong here ?

Any help appreciated

Thanks

Richlewis
  • 14,492
  • 34
  • 115
  • 258

2 Answers2

0

Well looks like it was a user error on my part nut will post the answer just incase it helps someone else. Setting variables in Node.js is different from your typical client side Javascript.

So where i was setting

var url = "www.google.co.uk"

I needed to set

global.url = "www.google.co.uk"
Richlewis
  • 14,492
  • 34
  • 115
  • 258
0

Node.js handles "imported" files with require different from what we are used to in browserland. This SO post gives a good overview about the "how" & "why":

What is the purpose of Node.js module.exports and how do you use it?

If you are looking for DalekJS specific resources on how to keep your tests DRY & modular, check out this repository https://github.com/asciidisco/jsdays-workshop/tree/8-dry I made for a workshop. To be more specific, these files in particular:

https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek/form2.js https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek/functions/configuration.js https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek/functions/formHelper.js https://github.com/asciidisco/jsdays-workshop/blob/8-dry/test/dalek/functions/selectors.js

Hope that helps.

Community
  • 1
  • 1