2015-10-13 24 views
0

我有一個TEXTBOX(只讀),它包含一些隨機值,例如對於ex。 2(這是產品數量)。 旁邊有一個按鈕(加號按鈕)和一個SPAN來顯示結果。Behat測試:從輸入中獲取一個值,將其乘以一個數字並匹配結果

現在每當我點擊加號按鈕時,它應該乘以5(這是產品的價格)到文本框中的數字(產品數量),並在SPAN中顯示結果。

<input type="text" readonly="readonly" value="2" id="product-qty" /> 
<button id="add-qty" value="Add Quantity"/> 
<span id="show-result">10</span> 

現在使用貝哈特測試我書面方式對上述方案一功能。 有人可以幫我寫這個。

問題是如何從文本框中獲取值並將其乘以5? 並將其與SPAN中的值匹配。

Scenario: Check product cart 
    Given I am on "detail-page" 
    When I click on the element "#add-qty" 
    #fetch value from the input multiply by it 5 and match the value with the content in the SPAN 
    And I wait 2000 milliseconds 
    Then I should see "Product added successfully" 

回答

1

一種方法是爲您的功能創建一個步驟定義。您可以在您的方案中添加And I add quantity for "product-qty"

,然後在FeatureContext.php(或同等學歷)加上這樣的事情:

/** 
* @Given I add quantity for :arg1 
*/ 
public function iAddQuantityFor($arg1) 
{ 
    $this->pressButton('Add Quantity'); 

    $page = $this->getMink()->getSession()->getPage(); 
    $quantity = (int)$page->find('css', '#product-qty')->getAttribute('value'); 

    $result = $quantity * 5; 
    $actual = (int)$page->find('css', '#show-result')->getHtml(); 

    if ($result !== $actual) { 
     throw new \Exception('Incorrect result'); 
    } 
} 
相關問題