Angular protractor how to check click with redirect condition

Be patient, I am new to the protractor.

I have this code:

angular.module('my.users')
    .controller('SessionSigninController', ['$scope', '$rootScope', 'Users', '$state',
        function($scope, $rootScope, Users, $state) {
            $scope.user = {};
            $scope.errors = [];
            $scope.save = function() {
                Users.signin($scope.user).then(
                    function(data) { 
                        if(data.success){
                            $rootScope.$emit('loggedin',data.user);
                            return $state.go('home');
                        }
                        $scope.errors = data.errors;
                    }
                );
            };
        }
    ])

      

I am testing it like (green bar):

describe('users :', function() {
    describe('register', function() {
        beforeEach(function() {
            browser.get('/#!/user/register');
        });
        it('btn should be disabled', function() {
            var btn = element(by.css('button.btn-primary'));
            expect(browser.isElementPresent(btn)).toBe(true);
            expect(btn.isEnabled()).toBe(false);
        });
        it('btn should be enabled', function() {
            var btn = element(by.css('button.btn-primary'));
            element(by.model('user.fullname')).sendKeys('test user');
            element(by.model('user.email')).sendKeys('test@etest.io');
            element(by.model('user.username')).sendKeys('testusere');
            element(by.model('user.password')).sendKeys('testsecret');
            element(by.model('user.password_confirmation')).sendKeys('testsecret');
            expect(browser.isElementPresent(btn)).toBe(true);
            expect(btn.isEnabled()).toBe(true);
            btn.click();
            expect(browser.getCurrentUrl()).toMatch(/\/#!/);

        });
    });

});

      

  • Is this the correct way to test a "redirect" to a home?
  • Why is the test green even if the form is invalid (unique email address)?
  • Is there a way to mock the test?

END UP (see answer)

describe('users :', function() {
    describe('register', function() {
        beforeEach(function() {
            browser.get('/#!/user/register');
        });
        it('btn should be disabled', function() {
            var btn = element(by.css('button.btn-primary'));
            expect(browser.isElementPresent(btn)).toBe(true);
            expect(btn.isEnabled()).toBe(false);
        });
        it('btn should be enabled', function() {
            var random = ~~(Math.random() * 1000);
            var btn = element(by.css('button.btn-primary'));
            element(by.model('user.fullname')).sendKeys('test user');
            element(by.model('user.email')).sendKeys('test'+random+'@test.io');
            element(by.model('user.username')).sendKeys('testuser'+random);
            element(by.model('user.password')).sendKeys('testsecret');
            element(by.model('user.password_confirmation')).sendKeys('testsecret');
            expect(browser.isElementPresent(btn)).toBe(true);
            expect(btn.isEnabled()).toBe(true);
            btn.click();
            expect(browser.getCurrentUrl()).toMatch(/\/#!\/$/);
            var userMenu = element.all(by.css('ul.dropdown-menu li'));
            expect(userMenu.count()).toBe(3);
         });
    });

});

      

+3


source to share


1 answer


I think your test looks good. It's easy to read. I would suggest adding more information to make sure you were actually able to log in. Check the heading or link "Logout" or anything that is visible after login.

Your test passes because the regex in

expect(browser.getCurrentUrl()).toMatch(/\/#!/);

      



Compliant '/#!/user/register'

. Try adding a dollar sign at the end:

expect(browser.getCurrentUrl()).toMatch(\/#!/$);

      

+5


source







All Articles