简体   繁体   English

从JS中另一个文件访问变量

[英]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). 在我的测试文件夹中,我有两个文件:一个api测试(postAdduser.js)和另一个量角器(sendPush.js)。

postAdduser.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 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 看到在BEFORE (sendPush.js)测试中,我想存储在测试postAddUser.js上使用的变量RUT

How can I do that? 我怎样才能做到这一点?

cheers, rafael 干杯,拉斐尔

A hacky way is: make rut a global variable hacky的方法是:将rut设为全局变量

GLOBAL.rut GLOBAL.rut

and it will be available in all the files given that the API file runs first. 只要先运行API文件,它就会在所有文件中可用。

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. 我有一个类似的问题,设置框架的方式是先在每个mocha文件之前调用setup.js文件,在setup.js中我执行某些文件中所需的全局声明。

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. 更好的方法是将此createUser方法抽象为一个函数,并在每个测试的每个块之前使用它。 You can have an afterEach block where you can delete that user. 您可以拥有一个afterEach块,您可以在其中删除该用户。 Take a look at hooks in https://mochajs.org/ 看看https://mochajs.org/中的钩子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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