0
votes

I am using Protractor + Jasmine.

I have a weird issue. I have 2 test suites (or Spec.js). I want to run them one after another (strictly). However, protractor is starting the Spec1 related BeforeAll(). And quickly jumping to Spec2 related BeforeAll().

In Spec1, there is one login function, and some times the login is taking 40 seconds. Is this is the reason?

And, after logged in from Spec1, Protractor jumps to Spec2 and starts executing "BeforeAll". And later comes back to Spec1 related "describe".

Please bear with me about these big files, as they are critical to debug.

If I run these two Specs separately, ie., not in a sequence, everything was fine.

Spec1.js

beforeAll(function () { 
    originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = globalconstants.wait10Mints;
    browser.waitForAngularEnabled(false);
    browser.get(env.appUrl);
    browser.getTitle().then(function (title) {
        expect(title, "Browser title is not the expected. But " + browser.getTitle()).toBe("Valueone");
    });
    browser.waitForAngularEnabled(true);
    loginPage.login();

    commonPage.navigateToUsers();
    usersPage.deleteUsers(name); 
});

describe('Create Users', function () {
it("Create user", function () {
something 
});

it("Create user", function () {
something 
});
});

Spec2.js

beforeAll(function () { 
    originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = globalconstants.wait10Mints;
    browser.waitForAngularEnabled(false);
    browser.get(env.appUrl);
    browser.getTitle().then(function (title) {
        expect(title, "Browser title is not the expected. But " + browser.getTitle()).toBe("Valueone");
    });
    browser.waitForAngularEnabled(true);
    loginPage.login();

    commonPage.navigateToAccounts();
    accountsPage.deleteAccounts(number); 
});

describe('Create Accounts', function () {
it("Create Savings", function () {
something 
});

it("Create current account", function () {
something 
});

});

Conf.js

var env = require('./Utils/env.js');
var HTMLReport = require('protractor-html-reporter-2');
var JasmineReporters = require('jasmine-reporters');

exports.config =
    {

        baseUrl: 'http://localhost:9999',
        directConnect: true,
        framework: 'jasmine2',

        suites: {
            1sp: './mysuites/1Spec.js',
            2sp: './mySuites/2Spec.js',
        },

        capabilities: {
            'browserName': env.browserName,
            'platform': env.osName,

            //Closes any existing browsers
              'shardTestFiles': false,
              'maxInstances': 1
        },

        params: {
            tempVar: false
        },

        onPrepare: async () => {
            global.result = false;

            var fs = require('fs-extra');
            fs.emptyDir('./Reports/Screenshots/', function (err) {
                console.log(err);
            });
            jasmine.getEnv().addReporter({
                specDone: function (result) {
                    if (result.status == 'failed') {
                        browser.getCapabilities().then(function (caps) {
                            browser.takeScreenshot().then(function (png) {
                                var stream = fs.createWriteStream('./Reports/Screenshots/' + env.browserName + '-' + result.fullName + '.png');
                                stream.write(new Buffer(png, 'base64'));
                                stream.end();
                            });
                        });
                    }
                }
            });

            var width = 1600;
            var height = 1200;
            browser.driver.manage().window().setSize(width, height);

            //Html reporter
            jasmine.getEnv().addReporter(new JasmineReporters.JUnitXmlReporter({
                consolidateAll: true,
                savePath: './Reports',
                filePrefix: 'xmlresults'
            }));
        },

        onComplete: async () => {

            var capsPromise = browser.getCapabilities();
            capsPromise.then(function (caps) {
                testConfig = {
                    reportTitle: 'UI Test Execution Report',
                    outputPath: './Reports',
                    outputFilename: 'UI Test Results',
                    screenshotPath: './screenshots',
                    testBrowser: 'FireFox',
                    modifiedSuiteName: false,
                    screenshotsOnlyOnFailure: true,
                    testPlatform: env.osName
                };
                new HTMLReport().from('./Reports/xmlresults.xml', testConfig);
            });
        }
    };
2
Can you post the capabilities section of your conf also? - DublinDev
Your full conf.js file would be really helpful. That is likely where the issue is - Ben Mohorc
Attached the config - Thanks in advance to spend time to help me out - These are lengthy files, however, they help us to debug the issue. - user3055964
what command are you using to execute your code? - DublinDev
As we have not yet integrated to Jenkins, from VSCode IDE I am running - Could this be the cause ? If everything else looks fine, then problem is with vscode itself. Protractor is running "Parallel". Just starts Spec1 BeforeAll, jumpts to 2nd spec - executing BeforeAll method. - user3055964

2 Answers

1
votes

After some investigation I was able to recreate your issue and it appears to be related to how Jasmine handles beforeAll hooks. There is a complete discussion available here and here but basically (and surprisingly) it appears that anything in your test file not declared within a describe will be executed before ANY tests are executed, including beforeAll hooks. This is surprising to me as I am almost certain I've used beforeAlls outside describes before without issue but can't verify that right now.

However, if you declare your beforeAll hook within your describe it will be hoisted up and still execute before your tests exactly the same as you are currently expecting it to behave now.

Can you try moving your beforeAll blocks within your describes as below:

Spec1.js

describe('Create Accounts', function () {
   beforeAll(function () { 
       originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
       jasmine.DEFAULT_TIMEOUT_INTERVAL = globalconstants.wait10Mints;
       browser.waitForAngularEnabled(false);
       browser.get(env.appUrl);
       browser.getTitle().then(function (title) {
           expect(title, "Browser title is not the expected. But " + browser.getTitle()).toBe("Valueone");
       });
       browser.waitForAngularEnabled(true);
       loginPage.login();

       commonPage.navigateToAccounts();
       accountsPage.deleteAccounts(number); 
   });

   it("Create Savings", function () {
       something 
   });

   it("Create current account", function () {
      something 
   });

});

Spec2.js

describe('Create Users', function () {
    beforeAll(function () {
        originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
        jasmine.DEFAULT_TIMEOUT_INTERVAL = globalconstants.wait10Mints;
        browser.waitForAngularEnabled(false);
        browser.get(env.appUrl);
        browser.getTitle().then(function (title) {
            expect(title, "Browser title is not the expected. But " + browser.getTitle()).toBe("Valueone");
        });
        browser.waitForAngularEnabled(true);
        loginPage.login();

        commonPage.navigateToUsers();
        usersPage.deleteUsers(name);
    });

    it("Create user", function () {
        something
    });

    it("Create user", function () {
        something
    });
})
1
votes

Just add the below one in your config.

specs: ['src/**/*spec.js'], // This should be the relative path to you spec.js

Now the test runs based on the file order in you path.

To run test: protractor config.js

Suites in your config is used to group your test based on functionality or something else.

The best way to solve the issue is to Move all your test in one file and have two describe in your spec.

structure: spec.js

`describe()`
{
beforeall() \\ Spec1.js
it()
it()
}
describe(){
beforeAll()\\spec2.js
it()
it()
}

Hope it helps you