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 回答