Page 295 - AngularJS权威教程
P. 295
19.20 编写测试 275
}, 19
seleniumAddress: 'http://localhost:4444/wd/hub',
baseUrl: 'http://localhost:9000', 20
specs: ['test/protractor/**/*.js'],
21
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
} 22
};
23
19.20 编写测试
当用Protractor开始测试时,我们通过Jasmine来设置测试。也就是说,我们只是像编写Karma 24
测试那样来编写测试。例如,一个简单的Protractor测试设置可能是这样的:
describe('homepage', function() { 25
beforeEach(function() {
// 函数执行之前
}); 26
it('should load the page', function() {
// 将测试代码放在这里
expect(...).toEqual('hello'); 27
});
});
28
尽管上面测试的内容还没写完,但结构很熟悉了:它是用Jasmine建立的。我们使用
beforeEach()和afterEach()函数以及嵌套的describe()块作为测试的结构。
29
要真正实现测试,使用Jasmine给予我们的同样的expect()语法。
编写Protractor测试需要我们在测试中跟Protractor暴露的一些全局变量打交道。下面列出了这 30
些全局变量:
1. browser 31
browser变量是围绕webdriver实例的一个包装。我们使用browser变量来做各种导航,或者
是从页面上抓取任意信息。 32
可以使用browser变量的get()函数来导航到一个页面:
beforeEach(function() { 33
browser.get('http://127.0.0.1:9000/');
});
34
可以用browser对象来玩一些妙招。例如,可以用browser对象上的debugger()方法来调试
页面。
35
it('should find title element', function() {
browser.get('app/index.html');
browser.debugger(); 36