【问题标题】:How to reuse code in Protractor / AngularJS Testing如何在 Protractor / AngularJS 测试中重用代码
【发布时间】:2014-07-16 13:59:05
【问题描述】:

我们在几个 JS 文件中对我们的 AngularJS 应用程序进行了多个 Protractor 端到端测试,它们运行良好。但是,在整个测试过程中有很多重复的代码,我们想把它干掉。

例如,每次登录时,我们都必须单击文本元素,输入用户名和密码,然后单击 Enter。现在每个 JS 文件都有自己的登录函数副本,在每次测试之前都会调用它。

将它们重构为我们可以导入的模块会很好。我一直在寻找几个小时,但没有找到一个好的解决方案。

我们应该怎么做?

【问题讨论】:

    标签: javascript angularjs testing protractor


    【解决方案1】:

    您可以创建 nodejs 模块并将它们包含在量角器配置中

    login-helpers.js

    exports.loginToPage = function () {
        //nodejs code to login
    };
    

    protractor.conf.js

    exports.config = {
        //...
        onPrepare: function () {
            protractor.loginHelpers = require('./helpers/login-helpers.js');
        }
        //...
    };
    

    page.spec.js

    it('should do smth', () => {
        protractor.loginHelpers.loginToPage()
    
        //expect(...).toBe(...);
    });
    

    【讨论】:

    • 你确定登录助手不应该是module.exports.loginToPage = function...
    • 你能告诉我们你为什么将loginToPage函数绑定到exports吗?
    【解决方案2】:

    我们的团队将 Orchid-js 与 Jasmine 和 Protractor 一起使用,它的设计正是为了做到这一点。

    你的测试

    Describe('Login user',require('../login.js'))("username","password");
    

    login.js

    module.exports = function(username,password){ 
        describe('login the user',function(){
            it('should login the user',function(){
                element(by.id('usernameField')).sendKeys(username);
                element(by.id('passwordField')).sendKeys(password);
                element(by.id('loginButton')).click();
            });
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多