How to reuse code in Protractor / AngularJS Testing

前端 未结 2 714
心在旅途
心在旅途 2020-12-28 09:57

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

相关标签:
2条回答
  • 2020-12-28 10:15

    Our team uses Orchid-js with Jasmine and Protractor, it's designed to do exactly this.

    Your test

    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();
            });
        });
    }
    
    0 讨论(0)
  • 2020-12-28 10:27

    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(...);
    });
    
    0 讨论(0)
提交回复
热议问题