Page Objects 页对象(protractor)
2016-01-11 11:26
417 查看
Using Page Objects to Organize Tests
When writing end-to-end tests, a common pattern is to use Page Objects. Page Objects help you write cleaner tests by encapsulating information about the elements on your application page. A Page Object can be reused across multiple tests, and if the template of your application changes, you only need to update the Page Object.页对象(page objects)能封装应用页面的元素信息,并且能在多个tests中复用。
当页面元素信息修改了以后,只要更新页对象就可以了,不用一个一个修改test里的元素信息。
Without Page Objects
Here’s a simple test script (example_spec.js) for ‘The Basics’ example on the angularjs.org homepage.describe('angularjs homepage', function() { it('should greet the named user', function() { browser.get('http://www.angularjs.org'); element(by.model('yourName')).sendKeys('Julie'); var greeting = element(by.binding('yourName')); expect(greeting.getText()).toEqual('Hello Julie!'); }); });
With PageObjects
To switch to Page Objects, the first thing you need to do is create a Page Object. A Page Object for ‘The Basics’ example on the angularjs.org homepage could look like this:首先:新建一个页对象,包括选择元素,和元素的交互:
var AngularHomepage = function() { var nameInput = element(by.model('yourName')); var greeting = element(by.binding('yourName')); this.get = function() { browser.get('http://www.angularjs.org'); }; this.setName = function(name) { nameInput.sendKeys(name); }; this.getGreeting = function() { return greeting.getText(); }; };
The next thing you need to do is modify the test script to use the PageObject and its properties. Note that the functionality of the test script itself does not change (nothing is added or deleted).
然后就是修改测试脚本,使用页对象和它的属性(之前写的和元素交互的函数)了。
describe('angularjs homepage', function() { it('should greet the named user', function() { var angularHomepage = new AngularHomepage(); angularHomepage.get(); angularHomepage.setName('Julie'); expect(angularHomepage.getGreeting()).toEqual('Hello Julie!'); }); });
Configuring Test Suites
It is possible to separate your tests into various test suites. In your config file, you could setup the suites option as shown below.可以将tests分成几个测试集,可以在配置文件选择测试集。
exports.config = { // The address of a running selenium server. seleniumAddress: 'http://localhost:4444/wd/hub', // Capabilities to be passed to the webdriver instance. capabilities: { 'browserName': 'chrome' }, // Spec patterns are relative to the location of the spec file. They may // include glob patterns. suites: { homepage: 'tests/e2e/homepage/**/*Spec.js', search: ['tests/e2e/contact_search/**/*Spec.js', 'tests/e2e/venue_search/**/*Spec.js'] }, // Options to be passed to Jasmine-node. jasmineNodeOpts: { showColors: true, // Use colors in the command line report. } };
From the command line, you can then easily switch between running one or the other suite of tests. This command will run only the homepage section of the tests:
从命令行可以简单方便的切换测试集,比如只运行主页的测试集:
protractor protractor.conf.js --suite homepage
Additionaly, you can run specific suites of tests with the command:
另外,也可以选择多个测试集:
protractor protractor.conf.js --suite homepage,search
Written with StackEdit.
相关文章推荐
- Objective-C在ARC下结合GCD的单例模式和宏模版
- 编译错:The method createSqlQuery(String, List<Object>) is ambiguous for the type
- django TypeError: 'module' object is not callable
- #Objective - C - UI-design - 第七天 -UIKit框架-UIKit-导航控制器-UINavigationController
- 理解Objective-C的变量以及面相对象的继承特性
- Objective-c的内存管理MRC与ARC
- iOS完全自学手册——[三]Objective-C语言速成,利用Objective-C创建自己的对象
- [Objective-C] 021 KVC、KVO
- 《Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法》--读书笔记
- [6]姥爷幽默谈Objective-C-文件操作类NSFileManager,NSFileHandle
- Mybatis——MetaObject学习
- Objective-C下的NSCoding协议
- object-c 新旧两种弹出框
- [5]姥爷幽默谈Objective-C-字符串(NSString)
- Objective-c下的深拷贝、浅拷贝以及NSCopying协议
- Objective-c开发--MRC和ARC混编--(iOS开发)
- 论文提要“You Only Look Once: Unified, Real-Time Object Detection”
- iOS开发之Objective-C与JavaScript的交互
- Objective-C 9 常用结构体
- Objective-c下变量和属性的区别