【问题标题】:How to share the same test cases between suites in protractor如何在量角器中的套件之间共享相同的测试用例
【发布时间】:2023-04-05 14:14:01
【问题描述】:

我有一些测试用例可以在测试套件之间共享

假设套件 x 和套件 y 共享同一组测试用例(它起作用)。

我制作了一个单独的 .js 文件,其中包含看起来像这样的共享代码。

module.exports = function(a,b){
//...
test cases..
//....
}

我正在尝试在 x 和 y 中使用这个模块

这就是 x 的样子

var common = require('./module');

describe("description", module(a,b);

这可以吗?还有其他方法吗?

我的代码中常见的js长这样

module.exports = function(a,b) {

beforeAll(function(){
//some code
}
afterAll(function(){
//some code
}

It(‘ads’, function(){
code
}

it(‘ads’, function(){
code
}

it(‘ads’, function(){
code
}


}

我想在另外两个套件中将其用作具有可传递参数的 describe 函数的函数参数。

套房1

var common = ('./common');
describe('this is a test case', common(a,b);

这可能吗?

【问题讨论】:

  • 写入方法例如openBrowser(...) 并在两套西装中运行此方法。
  • 能否详细说明

标签: javascript angular selenium jasmine protractor


【解决方案1】:

如果您的 common.js 文件类似于...

module.exports = function(a,b){
//...
test cases..
//....
}

还有你的 test.js 文件:

var common = require('./common'); // <-- note the change

describe("description", common); // <-- you were calling module*

这是假设您的 common.js 导出函数是格式正确的描述函数。

您还可以导出单个测试用例,例如 (other.js)...

module.exports = {
    testOne: function(something) { return false; },
    testTwo: function(whatever) { return true; }
}

你的测试...

var other = require('./other');

describe("description", function() {
    it('should pass', function() {
        expect(other.testOne()).toEqual(false);
    });
});

【讨论】:

    【解决方案2】:

    据我所知,您不能直接从另一个文件运行“它”。但是,您可以运行函数,并且函数可以执行“它”可以执行的所有操作。例如:

    Helper.js(这是你的函数文件)

    export class helper{
      static itFunctionOne(){
        //Test code goes here
      }
      static itFuncitonTwo(){
        //Test code goes here
      }
    }
    

    然后在您的测试中
    Test1:

    const helper = require('relative/path/to/file/helper.js');
    describe('Doing test 1 stuff',function(){
      it('Should run test',function(){
        helper.itFunctionOne();
        helper.itFunctionTwo();
      }
    }
    

    测试2:

    const helper = require('relative/path/to/file/helper.js');
    describe('Doing test 2 stuff',function(){
      it('Should run test',function(){
        helper.itFunctionOne();
        helper.itFunctionTwo();
      }
    }
    

    【讨论】:

    • 感谢大家的输入我已经修改了问题,最后一部分是我当前代码的样子。请让我知道我的要求是否可行
    • 我也依赖 beforeAll 和 afterAll 函数来登录和注销用户。我无法将任何参数传递给辅助函数
    猜你喜欢
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2020-01-24
    • 2015-06-20
    • 2019-07-06
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多