如何重用Jasmine测试

时间:2015-12-11 17:46:56

标签: javascript testing automation jasmine protractor

我在Jasmine中进行了以下测试,我需要为2个不同的URL执行,这2个url是同一产品的不同版本:

describe('TEST ',function(){
    var basePage = new BasePage();
    var page1 = new Page1();

    describe('TEST',function(){

        beforeEach(function(){
            browser.get('URL-1.html');
        });

        it('REUSE THIS TEST' , function (){
            browser.wait(EC.visibilityOf(viewerWidgetPage.videoPlayer));
            page1.videoControls.click();
            expect(basePage.hasClass(page1.videoPlayer, 'vjs-playing')).toBeTruthy();
            page1.audioControl.click();

            //Verify that the video property is muted.
            browser.executeAsyncScript_(function(callback){
                callback(window.player.muted());
            }).then(function(isMuted){
                expect(isMuted).toBeFalsy();
            });

            page1.audioControl.click();

            //Verify that the video property is muted.
            browser.executeAsyncScript_(function(callback){
                callback(window.player.muted());
            }).then(function(isMuted){
                expect(isMuted).toBeTruthy();
            });


        });

    });

有没有办法在另一个测试中使用,'它'“以任何方式重复使用此测试”?

1 个答案:

答案 0 :(得分:4)

一种选择是遍历测试中的网址

describe('TEST ',function(){
    var basePage = new BasePage();
    var page1 = new Page1();
    var urls = ['URL-1.html', 'URL-2.html'];

    urls.map(function (url) {
        describe('TEST ' + url,function(){

            beforeEach(function(){
                browser.get(url);
            });

            it('REUSE THIS TEST' , function (){
                browser.wait(EC.visibilityOf(viewerWidgetPage.videoPlayer));
                page1.videoControls.click();
                expect(basePage.hasClass(page1.videoPlayer, 'vjs-playing')).toBeTruthy();
                page1.audioControl.click();

                //Verify that the video property is muted.
                browser.executeAsyncScript_(function(callback){
                    callback(window.player.muted());
                }).then(function(isMuted){
                    expect(isMuted).toBeFalsy();
                });

                page1.audioControl.click();

                //Verify that the video property is muted.
                browser.executeAsyncScript_(function(callback){
                    callback(window.player.muted());
                }).then(function(isMuted){
                    expect(isMuted).toBeTruthy();
                });


            });
        });
    });
});

另一种可能更好地扩展的方法是使用multiCapabilities并将所需的规范添加到参数化测试网址的每个功能中。

我们的想法是在每个功能上定义参数:

multiCapabilities: [
    {
        browserName: "chrome",
        url: "URL-1.html"
    },
    {
        browserName: "chrome",
        url: "URL-2.html"
    }
],

然后,在您的测试中使用getProcessedConfig()来访问当前功能和url

beforeEach(function () {
    browser.getProcessedConfig().then(function (config) {
        var url = config.capabilities.url;
        browser.get(url);
    });
});

经过测试 - 适合我。