2017-04-05 14 views
1

グリッドに値が追加されているかどうかを確認するコード(クレジット@kishanpatel)Traverse-through-each-row-for-a-column-textがあります。私はこれを私のページオブジェクトに入れたいと思う。私はページオブジェクトに要素を追加することを考えていましたが、セレンに似た別のヘルパーファイルにif条件を入れましたが、私は正しいappraochであるかどうかはわかりません。以下の詳細を参照してください。分度器でpageobjectモデルに検証を入れる方法

mo.helperをspec.tsで呼び出すと、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); } }); } }); 

iは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(); 
}); 
+0

に上記を理解するためにあなたの参照のためのリンクをご覧くださいエッシー。私に「taxhome」を見せてください。選択モジュール。 taxhome.selectmoduledropdown(1); 'ヘルパーファイル。また、 'var row_check = element(by.xpath(" // div [(text()、 "+ value +")] ")); return require( './mohhelper.ts') 'は、' 'var row_check = element(by.xpath(" //(div)を含む "+ value +")] ")でなければなりません。 return row_check ' –

+0

tax.selectmoduleは、モジュールがロードされていることを確認するもう1つのステップです。私は質問にヘルパーファイルを添付しています。参照mohelper.ts参照してください。 this.module = function(){ var elem =要素(by.linkText( 'FATCA(201) - 07')) var EC = protractor.ExpectedConditions; browser.wait(EC.visibilityOf(elem)); } this.selectmodule = function(){ 要素(by.id( 's2id_TaxProcessMod'))。 } – kavitha

+0

もし私がreturn row_checkとして更新するなら、それはrowentriesを返していて、ヘルパーファイルで述べたように検証しません。 conf.tsで渡された値さえもmo.grid関数に渡されません。私は "test_protractor"に合格しましたが、出力では未定義に対して検証されました – kavitha

答えて

3

カップル -

1)分度器のAPIメソッドのほとんどは非同期である、すなわち、彼らはあなたがアクションを実行するために拒否/それらを解決しなければならない約束を返します。

isPresent()はまた、あなたがIT-に

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)あなたは活字体を使用しているので、むしろ従来の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よりも、その構文を使用)を維持ページを解決する必要があり、約束を返します。オブジェクトは効率的かつ可読です。

1ページのヘルパーメソッド、要素などは、すべて1つのページオブジェクトに入れる必要があります。それらを別々のクラスに書くと、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"); 

}); 

それがmよりdetail-

+0

ありがとう@ramこれは本当に役に立ちます...私はたくさんのことを学ぶ必要があります:) – kavitha

+0

使用しているTypescriptのバージョンによっては、 'async/await'を使うこともできます。つまり、 'row_check.isPresent()'を 'then'で解決する必要はありませんが、' if(await row_check.isPresent()) 'と言うことができます。待機すると「実行」が停止し、解決されるまで待機します。コードを読みやすくします。簡単な例は、[this](http://www.syntaxsuccess.com/viewarticle/async-and-await-in-typescript)も参照してください。 – wswebcreation

関連する問題