2017-03-09 5 views
1

ヘルパーファイルの要素からテキストを取り出して、このテキストをスペックファイルで比較する必要がある場合があります。たとえば、次のように分度器:スペックファイルのヘルパーファイルから変数を使用する

どちらのページファイル:

this.matchText = function(elem) { 
    return elem.getText(); 
}; 

ヘルパーファイル:ファイルを仕様に渡すことができます変数としてglobalPriceを保存する方法

 // page objects 
     var calculationPage = require('../calculation_page/calculation_page.js'); 

     // variables 
     var globalPrice = ""; 

     module.exports = exports = function() { 
      describe('... 
       it('... 
        // initialize page object 
        var calculation = new calculationPage(); 

        // store price into global variable 
        calculation.matchText(calculation.price).then(function(text) { 
         globalPrice = text; 
        }); 

        // verify price equals expected 
        expect(calculation.matchText(calculation.priceSum)).toContain(globalPrice); 

        ??? 
       }); 
      }); 
     } 

specファイル:

// page objects 
var homePage = require('../home_page/home_page.js'); 

// helpers 
var getPrice = require('../helpers/get_price.js'); 

describe('... 
    it('... 

     // helper - get price 
     getPrice(); 

     // initialize page object 
     var home = new homePage(); 

     // verify if price equals expected 
     expect(home.matchText(home.price)).toContain(???); 
    }); 
}); 

どのようにスペックファイル内のヘルパーファイルからグローバル変数を読み込むには?

+0

@私の答えを参照してください? – Gunderson

+0

現在、私はこれをヘルパーと呼んでいますが、スペックファイルに変更します。 – jurijk

答えて

1

グローバルに必要な任意の値を、突起物グローバルオブジェクト - browserにダンプできます。

そう言えば、ヘルパーファイルでは値を保存する必要があります。次にこれを行う - browser.globalPrice = text

そして、この値はスペックファイルで利用可能になります。あなたはヘルパーファイルに `describe`、` it`、と `expect`ブロックを持っていないのはなぜ他の値と同様browserオブジェクトexpect(home.matchText(home.price)).toContain(browser.globalPrice);

からのアクセスはProtractor: initialise Global variable in one js and use the same var in other js

+0

感謝!これだよ! ;)まさに私が探していたもの。 – jurijk

+0

ただの簡単な質問;)あなたが書いた方法で何かを保存できますか? – jurijk

+0

@jurijk ..ようこそ!はい、あなたは何かを格納することができます...複雑なオブジェクト、参照何か – AdityaReddy

関連する問題