2017-10-10 28 views
0

我正在测试一个网页过滤器,并且想运行一个简单的测试来验证页面被阻止。我也想在数百页上运行这个测试。下面的代码适用于一个单独的页面:对许多网站运行茉莉花测试

for (var n = 0; n < 3; n++) { 
    describe("Blocked Sites",() => { 

     it('should block ',() => { 
      sites.pageGo(); 
      expect(sites.blockedIDOnLoad.isVisible()).toBeTruthy; 
     }); 

    }); 

}

sites.pageGo()提供了下一个站点每个循环重复一次。只要pageGo()提供的每个页面都被阻塞,这个效果就很好。例如,如果它循环3次,我会得到3个通过测试。我可以看到浏览器加载每个不同的页面并被阻止。但是,如果任何页面未被阻止,则所有测试都会失败。我想对许多网站运行此测试(一些阻止,一些不)。我是初学者,当谈到自动化测试时,我非常感谢您提供的任何指导/知识。有没有一种方法可以用我当前的框架来实现这个测试,还是有更好的方法?

回答

0

我能够通过使用for ...循环来获得它的工作。这是对我有用的东西。希望这可以帮助。

首先,我设置了一个网站类。

export default class Site { 
    url: string; 
    id: string; 

    constructor(url: string, id: string) { 
     this.url = url; 
     this.id = id; 
    } 

    get blockedIDOnLoad() { return browser.element(this.id); }  
} 

然后测试这个样子......

import site from './site'; 

describe("Blocked Sites",() => { 
    //obviously you wouldn't do it this way for a bunch of sites 
    //should help get the idea across though 
    const sites = [ new site("http://www.google.com", "#lst-ib"), 
     new site("http://facebook.com", "#blockedId"), 
     new site("http://twitter.com", "#blockedId") ];  

    for (const s of sites) { 
     it('should block',() => { 
      browser.url(s.url); 
      expect(s.blockedIDOnLoad.isVisible()).toBe(true); 
     });    
    } 
}); 

在谷歌测试通过,因为这是页面上的有效身份证件。

enter image description here

+0

指教,谢谢。 – Gorgotron

0

我想你也可以使用this如下

describe("test power",() => { 

     function test(x){ 
     var result = x * x * x; 
     it(`${x} in the power 3 is ${result}`, function() { 
     assert.equal(pow(x, 3), result); 
    }); 
     for (var x = 1; x <= 5; x++) { 
    test(x); 
    } 
} 

});