[Protractor] Testing With Protractor Page Objects

Protractor Page Objects are a recommended for testing your AngularJS applications. Page Objects abstract the interaction between the browser and your functional tests, resulting in much cleaner tests.

 

We don't want use 'browser' and element directly in the test, so we wrap those into a page object:

复制代码
function IndexPage(){
    this.button = element(by.id('button1'));
    this.message = element(by.binding('vm.messageText'));

    this.getPageTitle = function(){
        return browser.getTitle();
    }

    this.get = function(){
        browser.get('http://127.0.0.1:8080');
    }

    this.clickButton = function (){
        this.button.click();
    }

    this.getMessageText = function (){
        return this.message.getText();
    }
}

module.exports = IndexPage;
复制代码

 

Then in the test, we need to call get() method beforeEach.

    var page = new IndexPage();

    beforeEach(function (){
        page.get();
    });

 

 

-------------

复制代码
var IndexPage = require('./indexPage');

describe('Simple page test', function() {

    var page = new IndexPage();

    beforeEach(function (){
        page.get();
    });

    it('Should get title of the page', function() {

        expect(page.getPageTitle()).toBe('E2E Testing');
    });

    it('should update the button text when click the button', function(){

            page.clickButton();
            expect(page.getMessageText()).toBe('button 1 clicked');
    })
});
复制代码

 

posted @   Zhentiw  阅读(306)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-11-23 [ES6] 15. Generators -- 2
2014-11-23 [ES6] 14. Generator -- 1. yield & next()
2014-11-23 [ES6] 13. Using the ES6 spread operator ...
2014-11-23 [ES6] 12. Shorthand Properties in ES6
点击右上角即可分享
微信分享提示