Can I dynamically create a test spec within a callback?

让人想犯罪 __ 提交于 2019-11-30 22:45:31

There are major problems preventing it to be easily achieved:

  • the specs you are creating are based on the result of asynchronous code - on the elements Protractor should first find
  • you can only have the Protractor/WebDriverJS specific code inside the it, beforeEach, beforeAll, afterEach, afterAll for it to work properly and have the promises put on the Control Flow etc.
  • you cannot have nested it blocks - jasmine would not execute them: Cannot perform a 'it' inside another 'it'

If it were not the elements you want to generate test cases from, but a static variable with a defined value, it would be as simple as:

describe("Check something", function () {
    var arr = [
        {name: "Status Reason", inclusion: true},
        {name: "Status Reason", inclusion: false}
    ];

    arr.map(function(item) {
        it("should look good with item " + item, function () {
            // test smth
        });
    });
});

But, if arr would be a promise, the test would fail at the very beginning since the code inside describe (which is not inside it) would be executed when the tests would be loaded by jasmine.

To conclude, have a single it() block and work inside it:

it("should have elements with a desired property", function() {
    fetchElements().then(element_list) {
        foreach element {
            expect("foo")
        })
    }
}

If you are worried about distinguishing test failures from an element to element, you can, for instance, provide readable error messages so that, if a test fails, you can easily say, which of the elements has not passed the test (did not have a specific property in your pseudo-test case). For example, you can provide custom messages to expect():

expect(1).toEqual(2, 'because of stuff') 

We can generate dynamic tests using jasmin data provider but it is working with only static data.

If we want to generate tests from the asynchronous call in protractor, then we need to use onprepare function in the protractor config js.

Create a bootloader and read the test cases from excel or server and import the data loader in the onprepare function. It is bit difficult to explain because i have faced many issues like import is not supported in this javascript version and expected 2 args but got only 1. finally i have used babel to fix the issues and able to generate tests.

Below is the sample implementation that i have done in the on prepare method

var automationModule = require('./src/startup/bootloader.ts');
var defer = protractor.promise.defer();
automationModule.tests.then(function(res) {
  defer.fulfill(res);
});

bootloader.ts contains the code to read the test suites and tests from excel sheet and sets the tests to the single to class.

Here res is the instance of singleton class which is returning from bootloader.ts

hard to explain everything here but you can take a look at my full implementation in my github https://github.com/mannejkumar/protractor-keyword-driven-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!