如何在量子器中将验证放在pageobject模型中

时间:2017-04-05 06:26:17

标签: protractor

我有一个代码(归功于@kishanpatel)Traverse-through-each-row-for-a-column-text,它将验证该值是否已添加到网格中。我想把它放在我的页面对象中。我正在考虑将元素添加到页面对象中,并将if条件添加到类似于selenium的不同帮助文件中,但我不确定是否是正确的appraoch。请参阅下面的详细信息。

如果我在规格中调用mo.helper,则表示gridcheck.ispresent()不是函数。如何处理这种情况?

it('verify the grid that master obligation is added', function () {
        var testvar = "'test_protractor'";
        var row_check = element(by.xpath("//div[contains(text()," + testvar + ")]"));
        if (row_check.isPresent()) {
            row_check.getText().then(function (msg) {
                if (row_check.isPresent()) {
                    console.log("Grid contains========== " + msg);
               }
         });
      }
  });

我在mo.ts(页面对象页面)中有以下方法:

 this.grid = function (value) {
        // var testvar = "'test_protractor'";
        var row_check = element(by.xpath("//div[contains(text()," + value + ")]"));
        return require('./mohelper.ts')
    }
}

mohelper.ts:

require('../page/mo.ts')
var mohelper = function () {

    this.gridvaluepresent = function () {
        require('../page/mo.ts')
        var gridcheck = mo.grid();
        if(gridcheck.isPresent()) {
            gridcheck.getText().then(function (msg) {
                if (gridcheck.isPresent()) {
                    console.log("Grid contains========== " + msg);
                }
            })
        }
   }
}

module.exports = new mohelper();

spec.ts:

it('go to corresponding module and verify whether the master obligation is added ', function () {
        browser.sleep(10000);
        taxhome.selectmodule;
        taxhome.selectmoduledropdown(1);
        mo.grid("test_protractor");
        mohelper.gridvaluepresent();
 });

1 个答案:

答案 0 :(得分:3)

这里要考虑的事情 -

1)大多数量角器的api方法都是异步的,即它们会返回你必须解决/拒绝它们以执行操作的承诺。

isPresent()也会返回一个承诺,您需要解决它 -

var row_check = element(by.xpath("//div[contains(text()," + value + ")]"));
row_check.isPresent().then(function(present) {
    if(present) { // it returns a boolean value
    row_check.getText().then(function (msg) {
    console.log("Grid contains========== " + msg);
});
}
});

2)由于您使用的是TypeScript,因此请使用其语法而不是传统的js-

let row_check = element(by.xpath("//div[contains(text()," + value + ")]")); // Block scoped variable using 'let'
row_check.isPresent().then((present) => {   // notice the thick arrow
    if(present) {
    row_check.getText().then((msg) => {
    console.log("Grid contains========== " + msg);
});
}
});

3)有效且可读地维护页面对象 -

单个页面的所有辅助方法,元素等应该放在单个页面对象中。将它们写在不同的类中,typescript使用类的概念并将它们转换为全局函数。

<强> moHelper.ts

import {ElementFinder, element} from 'protractor';

export class MoHelper {

  public row_check: ElementFinder; // its of element finder type

  gridValueCheck(value : string) {
    row_check = element(by.xpath("//div[contains(text()," + value + ")]")); // please use Css selectors instead of Xpath!    
    row_check.isPresent().then((present) => {
    if(present) {
    row_check.getText().then((msg) => {
      return msg; // here you are returning the msg of the row from your page!
      });
     }
   });
  }      

  }

您的 spec.ts 应验证行msg!

import {MoHelper} from './moHelper.ts'
let mo: MoHelper = new MoHelper();

it('go to corresponding module and verify whether the master obligation is added ', () => {
    browser.sleep(10000); // please refrain from using sleeps instead use Expected Conditions
    taxhome.selectmodule;
    taxhome.selectmoduledropdown(1);
    expect(mo.gridValueCheck("test_protractor")).toEqual("Your Expected Message");

 });

请找到供您参考的链接,以便更详细地了解上述内容 -