如何重复使用Jasmine测试

5

我在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();
            });


        });

    });

有没有任何方法可以在另一个测试中以任何方式使用“REUSE THIS TEST”这个'it'呢?
1个回答

4

一种选择是循环遍历测试的URL:

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);
    });
});

已测试 - 对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接