Search code examples
javascriptnode.jsmocha.jschaiassertions

Proper way to handle Assertion error so that subsequent test can run


What is the proper way of handling any form of error/assertion error so that when one test case fail, the subsequent test still gets run? or is it better to me to just have 1 test case in 1 JavaScript file?

describe('SMP Service Login Page', function() {
    describe('Loggin in', function() {
        // Unhappy flow always first
        var assert = require('assert');
        var useridField;
        var passwordField;
        var loginButton;
        it('should not allow user to login with wrong userid', function(done) {
            throw new Error("haha");

        });        
        it('should fail because of assertion', function(done) {            
            useridField = browser.element('android=new UiSelector().text("User Name").className("android.widget.EditText")');                  
            useridField.setValue('FIRST');
            console.log(useridField);

            passwordField = browser.element('android=new UiSelector().className("android.widget.EditText").instance(1)');                
            passwordField.setValue('FIRST');

            loginButton = browser.element('android=new UiSelector().resourceId("android:id/button1")');
            var text = loginButton.getText(); //text = 'LOG IN'            
            assert.equal(text,'LOG'); //this should fail , throws an error    
            loginButton.click(done);
        });
//this test doesn't get run cause of assertion error thrown

        it('should pass but it fails because it could not locate the useridField', function(done) {            
            console.log(useridField);
            useridField.setValue('userid'); 

            passwordField = browser.element('android=new UiSelector().className("android.widget.EditText").instance(1)');            
            passwordField.setValue('passsword'); 

            loginButton = browser.element('android=new UiSelector().resourceId("android:id/button1")');            
            loginButton.click(done);        
        });
    });
});

Solution

  • The best way is to write your tests to that they are independent from one-another. In the code you show your last test should not depend on the previous test to set useridField. You could move the code that initializes it into a beforeEach hook.