2017-01-20 31 views
1

如何檢查特定的$string(f.e. 123)是否出現在特定的HTML元素中,但不在使用PHP/Codeception驗收測試的外部或其他地方?使用PHP Codeception驗收測試檢查源代碼中的單一事件

實施例這將是細:

<html><body>foobar 234<div id="original">123</div></body></html> 

實施例,其應失敗#1(文本發生):

<html><body>foobar 123<div id="original">123</div></body></html> 

實施例,其應失敗#2(鏈路發生):

<html> 
    <body> 
    foobar 
    <div id="original">123</div> 
    <a href="/link/123">Link</a> 
    </body> 
</html> 

我試過了那個特定頁面以外的測試:

現在
$I->seeInPageSource($alias); 
$I->dontSeeInPageSource($original); 

我會需要這樣的東西

$I->seeInPageSourceElement($original, '#original'); 

$I->dontSeeInPageSourceExceptElement($original, '#original'); 
// could be implemented like this: 
$pageSourceWithoutElement = str_replace(
    $I->grabPageSourceElement('#original'), 
    '', 
    $I->grabPageSource() 
); 
$I->assertNotContains($original, $pageSourceWithoutElement); 

原因: 我有兩個版本,其中一個版本是別名另一個(稱爲「原始」)。我想確保只有別名纔會在除顯示別名定義的「顯示原始」頁面以外的任何地方使用。

回答

0

我找到了解決方法:使用jQuery

  • 分離元件(需要測試頁使用它)
  • 做定期檢測
  • 不完美的重新連接元素

解決方案,但它的作品。

public function dontSeeInPageSourceExceptElement($text, $excludeSelector) 
    { 
     $I = $this; 

     // check for positive occurrence in exclude selector (optional) 
     // $I->see($text, $excludeSelector); 

     // detach the selected element 
     // Problem: append() just re-attaches the element, but this might not be the right position 
     $I->executeJs(
      sprintf(
       // s = subject, p = parent, bs = backup of subject 
       "var s = $(%s), p = s.parent(), bs = s.detach(); " . 
       "setTimeout(function() { p.append(bs); }, 2000);", 
       json_encode($excludeSelector) 
      ) 
     ); 

     // check for negative occurrence now 
     $I->dontSeeInPageSource($text); 

     // wait 2 seconds (re-attach timeout) 
     $I->wait(2); 
    } 
相關問題