如何在 Protractor/AngularJS 测试中重用代码 [英] How to reuse code in Protractor / AngularJS Testing

查看:32
本文介绍了如何在 Protractor/AngularJS 测试中重用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在几个 JS 文件中为我们的 AngularJS 应用程序提供了几个 Protractor 端到端测试,它们运行良好.但是,在整个测试过程中存在大量重复代码,我们希望将其弄干.

We have several Protractor end to end tests for our AngularJS app in several JS files and they work great. But, there is a lot of duplicated code throughout the tests and we would like to DRY that up.

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

For instance, every time we log in, we have to click on the text elements, type in the username and password, and then click enter. And right now every single JS file has its own copy of the login function which is called before every test.

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

It would be nice to refactor those out into modules that we can then import. I have been searching for hours, but not found a good solution.

我们应该怎么做?

推荐答案

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

You can create nodejs modules and include them in protractor configuration

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(...);
});

这篇关于如何在 Protractor/AngularJS 测试中重用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆