Monday, 22 May 2017

Selenium Webdriver : Call 2 tests in another one

I'm using selenium Webdriver , i have two running tests , and i wanna develop a thors one which calls the first test to be executed , then the second.

My code looks like this:

before(function (done) {
  // initilization of the driver properties
  driver = common.driver;
  // error handling - if you want do something
  process.on('uncaughtException', function (err) {
    console.log("My error handler... " + err);
    if (driver) {
      //recording screenshot
      driver.takeScreenshot().then(function (img) {
        fs.writeFileSync("./error.png", new Buffer(img, 'base64'));
      });
    }
  });
  // open start page
  driver.get(config.webPage).then(function () {
    console.log("Loading...");
    done();
  });
  // resize window if needed   .setSize(1920, 1000);
  driver.manage().window().maximize();
});

// Test GENERAL
describe('Test 1', function () {

  require('./Test1.js');

});

describe('Test 2', function () {

  require('./Test2.js');
});

// run it once after tests
after(function (done) {
  // works with promise
  driver.quit().then(done);
});

Unfortenately ; that doesn't work : the first test : test1 runs but it fails in the beginning of the second , and it throws this :

driver.get(config.webPage).then(function () { ^

TypeError: Cannot read property 'get' of undefined
    at Suite.<anonymous> (D:\wamp64\www\Pagamac-CRM-Functional-Tests\test.js:45:9)
    at Object.create (C:\Users\firas.koubaa\AppData\Roaming\npm\node_modules\mocha\lib\interfaces\common.js:114:19)
    at context.describe.context.context (C:\Users\firas.koubaa\AppData\Roaming\npm\node_modules\mocha\lib\interfaces\bdd.js:44:27)
    at Object.<anonymous> (D:\wamp64\www\Pagamac-CRM-Functional-Tests\test.js:44:1)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at C:\Users\firas.koubaa\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:222:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\firas.koubaa\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:219:14)
    at Mocha.run (C:\Users\firas.koubaa\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:487:10)
    at Object.<anonymous> (C:\Users\firas.koubaa\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:459:18)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:508:3

This error says that "driver" is no more undefined while calling the second test : Test2, this because in the end of my first test i'm calling this :

after(function (done) {
  // works with promise
  driver.quit().then(done);
});

in the same time i should call it , to be able to launch the first 1 independently when needed .

Any suggestions ??



via firasKoubaa

No comments:

Post a Comment