Search code examples
javascriptiframejasminefixtures

jasmine fixtures with iframe


I'm using jasmine fixtures and I want to write a test with an HTML that has an iframe in it.

The problem is that the test executes before my iframe loads.
Is there a solution for this in the library itself?
Or will I have to implement my own mechanism to make it wait?

Here's the code:

The fixture:

<iframe id="test-iframe1" class="test-iframe1" src="data/tests/pages/iframe1.html" style="width: 400px; height: 600px;" frameborder="20"></iframe>

The test:

describe("iframe -", function() {

    beforeEach(function() {
        var f = jasmine.getFixtures();
        f.fixturesPath = 'base/tests/pages/';
        f.load('iframe-main.htm');
    });

    it('iframe exists', function() {
        // HERE - the iframe has not yet loaded
        expect($('#test-iframe1').length).toBe(1);
    });
});

Solution

  • You need to add done() function in beforeEach() where you will check loading of iframe

    JS

     beforeEach(function(done) {
        var f = jasmine.getFixtures();
        f.fixturesPath = 'base/tests/pages/';
        f.load('iframe-main.htm');
    
        $('#test-iframe1').load(function(){
            done();
        });
    
    });
    

    And your it() will not run before done() invoke.