2014-02-26 56 views
1

我想將我的量角器代碼移動到頁面對象設計模式,我通過我的登錄測試開始,請在下面找到我的代碼。頁面對象設計模式量角器

當運行我的測試時,量角器加載頁面BU嘗試輸入文本到用戶名密碼輸入失敗時,我試圖找到用戶名文本區域使用by.id和by.input但都沒有工作。 請注意,當我在使用頁面對象模式之前運行我的登錄測試時,量角器能夠找到文本區域。

頁login.js:

var loginPage = function() 
{ 
    this.userName = element(by.input('userName')); 
    this.password = element(by.input('userPassword')) ; 
    this.loginButton = element(by.id('login_form_signin_button')); 
    this.loginText = element(by.css('#mainGlobalSearchBtn')); 
    this.loginError = element(by.xpath('html/body/div[1]/div[1]/div[1]/form/div/p')); 
    this.login = function (userName, password) 
{ 
    loginPage.userName.sendKeys(userName); 
    loginPage.password.sendKeys(password); 
    loginPage.loginButton.click(); 
    browser.waitForAngular(); 
} 

}; 





it('should not login : incorrect login details', function() 

{ 
     var loginPage = new loginPage();    
     loginPage.login('incorrectusername','incorrectpassword'); 
    expect(loginPage.loginError.getText()).toContain('Access denied'); 
}); 

控制檯輸出:

1) Login should not login : incorrect login details 
    Message: 
    TypeError: undefined is not a function 
    Stacktrace: 
TypeError: undefined is not a function 
at null.<anonymous> (C:\Users\orsyp\DUX\k_workload_ar\ui\e2e\login.spec.js:3 
1:26) 
at C:\Users\orsyp\DUX\k_workload_ar\ui\node_modules\grunt-protractor-runner\ 
node_modules\protractor\jasminewd\index.js:54:12 
at webdriver.promise.ControlFlow.runInNewFrame_ (C:\Users\orsyp\DUX\k_worklo 
ad_ar\ui\node_modules\grunt-protractor-runner\node_modules\protractor\node_modul 
es\selenium-webdriver\lib\webdriver\promise.js:1445:20) 
at webdriver.promise.ControlFlow.runEventLoop_ (C:\Users\orsyp\DUX\k_workloa 
d_ar\ui\node_modules\grunt-protractor-runner\node_modules\protractor\node_module 
s\selenium-webdriver\lib\webdriver\promise.js:1310:8) 
at wrapper [as _onTimeout] (timers.js:252:14) 
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) 
+0

我不明白你的'module.exports =新loginPage();'行。在[入門文檔](https://github.com/angular/protractor/blob/master/docs/getting-started.md)中,頁面對象示例使用'var angularHomepage = new AngularHomepage();'到'它的功能。 – glepretre

+0

我很抱歉,但我的測試實際上仍然失敗(忘記配置我的conf.js)我編輯了我的代碼和我遇到的錯誤。 – Ziwdigforbugs

+0

你有一個堆棧跟蹤來知道哪一行拋出這個錯誤? – glepretre

回答

1

如果我按照Getting started doc例子,你應該添加var loginPage = new loginPage();it功能。

it('should not login : incorrect login details', function() { 
    //add this line 
    var loginPage = new loginPage(); 

    loginPage.login('incorrectusername','incorrectpassword'); 
    expect(loginPage.loginError.getText()).toContain('Access denied'); 
}); 
+0

這個解決方案是否解決了原始問題?:) – glepretre

0
const log = Factory.getLogger("Page.DashDashboardPage"); 

export class DashDashboardPage extends PageBase{ 

    private lblDashPageTitle  : any; 
    private lnkDashMyDashboard  : any; 
    private btnDashNewTeamDashboard : any; 
    private txtDashSearchRecord  : any; 

    constructor(){ 
     super(); 
     const element = Elements.DashDashboardPage; 
     this.lblDashPageTitle  = super.findLocators(element.lblDashPageTitle.findBy,element.lblDashPageTitle.value); 
     this.lnkDashMyDashboard  = super.findLocators(element.lnkDashMyDashboard.findBy,element.lnkDashMyDashboard.value); 
     this.btnDashNewTeamDashboard= super.findLocators(element.btnDashNewTeamDashboard.findBy,element.btnDashNewTeamDashboard.value); 
     this.txtDashSearchRecord = super.findLocators(element.txtDashSearchRecord.findBy,element.txtDashSearchRecord.value); 
    } 

    /** 
    * Get: load dash-dashboard base url 
    * @returns {DashDashboardPage} 
    */ 
    public get(): DashDashboardPage{ 
     ConfigRoute.visit_page('http://op.xxx-tek.com/test/'); 
     log.info("Step: navigate to http://op.xxx-tek.com/test/ [:get:]"); 
     return new DashDashboardPage(); 
    } 

    /** 
    * Validate: verify dash-board page title 
    * @param title 
    * @returns {DashDashboardPage} 
    */ 
    public check_And_Validate_dash_page_title(title: string): DashDashboardPage{ 
     this.Helper_Assertion.expectToEqual(this.lblDashPageTitle,title); 
     log.info("Validate: Verify dash page tile [:check_And_Validate_dash_page_title:]"); 
     return new DashDashboardPage(); 
    } 
}