1

I want to set a variable based on whether some text is present on the current page or not.

And then run a protractor test that depends on that variable. I can't do $(':contains') because there's $ != jQuery in this context and i can't see a simple way to do it with getText() which returns a promise. Is there a matcher like expect.toContain? and a way to run some code after that matcher is run? Or is there some other strategy i can use.

4

2 に答える 2

0

私があなたを正しく理解していると思うなら、あなたはこれらの次の行を使うことができます:

これにより、ページのすべてのテキストが得られます。

window.document.getElementsByTagName("html")[0].innerText

次に、正規表現を使用して、次のようにテキストを確認できます。

var res = patt.test(str);

これはあなたが得るもののようなものです:

この例では、「status」:「ok」というテキストを検索しています。

var str = window.document.getElementsByTagName("html")[0].innerText;
var patt = /"status": "ok"/;
var res = patt.test(str);
if(res){console.log(text is present!)}
于 2013-12-16T13:37:39.830 に答える
0

次のようなものが必要です。

 // utility to test expection of an element to match a regex:   
 var expectByCssToMatch = function(css, pattern) {             
   browser.driver.findElement(                                  
     by.css(css)).getText().then(function(text) {               
       expect(text).toMatch(pattern);                           
     });                                                        
 };                                                             

利用方法:

 describe('Logging in, function() {                       
   it('should work', function() {                                           
     var namePattern = new RegExp(param.name, i');
     expectByCssToMatch('.messages', /log\s?in successful/i);        
     expectByCssToMatch('body', namePattern);                       
   });                                                                      
 });                                                                        

あなたが求めていることを達成するために、expect()呼び出しを自分で選んだコールバックに置き換えたい

于 2014-05-24T02:23:38.313 に答える