Can I dynamically create a test spec within a callback?

后端 未结 2 2063
无人共我
无人共我 2021-01-05 21:26

I want to retrieve a list of elements on a page, and for each one, create a test spec. My (pseudo) code is :-

fetchElements().then(element_list) {
   foreach         


        
相关标签:
2条回答
  • 2021-01-05 21:41

    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

    0 讨论(0)
  • 2021-01-05 21:45

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