简体   繁体   中英

Access variable from another file in JS

in my test folder I have two files: an api test (postAdduser.js) and another of the protractor (sendPush.js).

postAdduser.js

var assert = require('chai').assert,
    request = require('supertest'),
    util = require('/Users/rafael/Desktop/projects/my-project/tests/API/spec/util.js'),
    expect = require('chai').expect;

var storeId;
var session    = null;
var rut;

describe('Register valid user', function() {

    before(function (done) {
        request(util.url)
          .post('/admin/login')
          .send(util.adminLogin)
          .end(function(err, res) {
            if (err) return done(err);
            session = res.header['set-cookie'];
            done();
          });
    });

  this.timeout(60000);
  it('Add valid user', function(done) {
    request(util.url)
      .post('/api/v1/users')
      .set('Content-type', 'application/json')
      .send(util.addValidUser)
      .end(function(err, res) {
        if (err) return done(err);
        var results = res.body;
        assert.equal(res.status, 200);
        expect(results).to.include(util.addValidUser);
        storeId = res.body._id;
        rut = res.body.rut;
        done();
      });
  });

sendPush.js

var HomePage = require('../pages/homePage.js');
var LoginPage = require('../pages/loginPage.js');
var Search = require ('../pages/searchPage.js');
var SendPush = require ('../pages/sendPush.js');
require('/Users/rafael/Desktop/projects/my-project/tests/UI/spec/postAddUser.js');


describe('Send notifications to RUT', function() {

  before(function() {
    describe('Register valid user', function() {
      console.log(rut);
    });
    HomePage.get();
  });

  it('should log in in admin', function() {
    LoginPage.login();
  });

  it('search for valid RUT', function() {
    Search.searchRut('73667143');
  });

  it('send push to RUT', function() {
    SendPush.sendPush();
  });

  after(function () {
    browser.quit();
  });
});

see that in the BEFORE (sendPush.js) test I want to store the variable RUT that is being used on test postAddUser.js

How can I do that?

cheers, rafael

A hacky way is: make rut a global variable

GLOBAL.rut

and it will be available in all the files given that the API file runs first.

I had a similar problem and the way setup the framework is first I invoke the file setup.js before each mocha file and in setup.js I do the global declarations that are required in some files.

Note that this approach is hacky and doesn't scale too well, say for example if two tests are running at the same time and each want to create/delete the user at the same time. A better approach is abstract this createUser method into a function and use it beforeEach block of each test. You can have an afterEach block where you can delete that user. Take a look at hooks in https://mochajs.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM