页面对象设计模式量角器

时间:2014-02-26 10:34:47

标签: javascript angularjs protractor

我正在尝试将我的量角器代码移动到我通过登录测试启动的页面对象设计模式,请在下面找到我的代码。

当运行我的测试时,量角器加载页面bu尝试在用户名密码输入中输入文本时失败,我试图使用by.id和by.input找到用户名文本区域,但两者都不起作用。 还请注意,当我在使用页面对象模式之前运行登录测试时,量角器能够找到文本区域。

page-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)

2 个答案:

答案 0 :(得分: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');
}); 

答案 1 :(得分: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();
    }
}