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

时间:2021-09-28 19:31:31

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

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

看到在BEFORE(sendPush.js)测试中我想存储在测试postAddUser.js上使用的变量RUT

How can I do that?

我怎样才能做到这一点?

cheers, rafael

1 个解决方案

#1


0  

A hacky way is: make rut a global variable

一种hacky方式是:使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. You can have an afterEach block where you can delete that user. Take a look at hooks in https://mochajs.org/

请注意,这种方法很麻烦,并且不能很好地扩展,例如,如果两个测试同时运行并且每个测试都希望同时创建/删除用户。一种更好的方法是将这个createUser方法抽象为一个函数,并在每个测试的每个块之前使用它。您可以使用afterEach块删除该用户。看看https://mochajs.org/中的钩子

#1


0  

A hacky way is: make rut a global variable

一种hacky方式是:使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. You can have an afterEach block where you can delete that user. Take a look at hooks in https://mochajs.org/

请注意,这种方法很麻烦,并且不能很好地扩展,例如,如果两个测试同时运行并且每个测试都希望同时创建/删除用户。一种更好的方法是将这个createUser方法抽象为一个函数,并在每个测试的每个块之前使用它。您可以使用afterEach块删除该用户。看看https://mochajs.org/中的钩子