2013-06-27 65 views
9

我用Selenium2驅動程序使用Behat和Mink,並且我試圖直接輸入表單字段(模擬原始鍵盤輸入),而不是使用fillField()函數。我可以使用Mink和Selenium2發送原始鍵盤輸入嗎?

這就是我想:

$element = $this->getSession()->getPage()->find('css', '#questionName'); 
$element->focus(); 

$element->keyPress('a'); 

// also tried this, with no success 
// $element->keyDown('a'); 
// $element->keyUp('a'); 

還有就是頁面上的元素<input type="text" id="questionName">。它正確接收焦點,但不響應任何模擬鍵盤輸入。

是否可以像這樣模擬原始鍵盤輸入?
我在做什麼錯?

回答

12

似乎有很多帖子抱怨keyPress沒有按預期工作,有些驅動程序根本不支持它。例如: -

Goutte - Keyboard manipulations are not supported by Behat\Mink\Driver\GoutteDriver

特別是硒驅動程序使用自定義的JS庫運行它的命令,但它似乎並沒有工作。我已經嘗試使用$this->getSession()->getDriver()->keyPress()$element->getPress(),但都沒有運氣。

https://github.com/Behat/MinkSelenium2Driver/blob/master/src/Behat/Mink/Driver/Selenium2Driver.php#L815

https://github.com/Behat/MinkSelenium2Driver/blob/master/src/Behat/Mink/Driver/Selenium2/syn.js

有趣的是,有在Selenium2代碼庫的按鍵事件還沒有單元測試(所以我認爲這是目前正在開發中)。

因此,目前,一個合適的解決方案是使用來自Is it possible to simulate key press events programmatically?的關鍵事件的javascript模擬(如果您不使用jQuery,請參閱此替代方法)和Behat Mink的evaluateScript函數。

如果您使用直PHPUnit的測試:

$key = 'a'; 
$script = "jQuery.event.trigger({ type : 'keypress', which : '" . $key . "' });"; 
$this->getSession()->evaluateScript($script); 

或者,如果你使用的黃瓜,添加到您的FeatureContext.php文件,你可以添加此功能:

/** 
* @Given /^(?:|I) manually press "([^"]*)"$/ 
*/ 
public function manuallyPress($key) 
{ 
    $script = "jQuery.event.trigger({ type : 'keypress', which : '" . $key . "' });"; 
    $this->getSession()->evaluateScript($script); 
} 

並使用它在你的功能文件是這樣的:

Given I manually press "a" 

至於使用javascript作爲解決方案,一些的驅動程序使用JavaScript來執行所需的keyPress。例如: -

https://github.com/Behat/MinkZombieDriver/blob/master/src/Behat/Mink/Driver/ZombieDriver.php#L819

0

我已經找到了最簡單的答案是觸發JavaScript中的關鍵事件,並寫入特定貝哈特一步的JS發送到瀏覽器和觸發它。

我們一直在使用YUI,所以我們使用YUI事件模擬,但jquery或原生js處理它。這個概念是重要的。這是我找到的最好的解決方案,直到本地behat支持。

希望這有助於。

public function press_key_in_the_ousupsub_editor($keys, $fieldlocator) { 
     // NodeElement.keyPress simply doesn't work. 
     if (!$this->running_javascript()) { 
      throw new coding_exception('Selecting text requires javascript.'); 
     } 
     // We delegate to behat_form_field class, it will 
     // guess the type properly. 
     $field = behat_field_manager::get_form_field_from_label($fieldlocator, $this); 

     if (!method_exists($field, 'get_value')) { 
      throw new coding_exception('Field does not support the get_value function.'); 
     } 

     $editorid = $this->find_field($fieldlocator)->getAttribute('id'); 

     // Get query values for the range. 
     $js = ' 
    function TriggerKeyPressBehat() { 
    // http://www.wfimc.org/public/js/yui/3.4.1/docs/event/simulate.html 
    YUI().use(\'node-event-simulate\', function(Y) { 
     var id = "'.$editorid.'"; 
     var node = Y.one("#" + id + "editable"); 

     node.focus(); 
     var keyEvent = "keypress"; 
     if (Y.UA.webkit || Y.UA.ie) { 
      keyEvent = "keydown"; 
     } 
     // Key code (up arrow) for the keyboard shortcut which triggers this button: 
     var keys = ['.$keys.']; 
     for(var i=0; i<keys.length;i++) { 
      node.simulate(keyEvent, { charCode: keys[i] }); 
     } 
    }); 
    } 
    TriggerKeyPressBehat();'; 
     $this->getSession()->executeScript($js); 
    } 
1

我用水貂與殭屍。js,因爲它本身不會捕獲鍵盤事件,所以我都聽取了focusoutkeyup jQuery事件。

$('form[name="order"]').find('input[id$="quantity"],input[id$="price"]').bind('keyup focusout', function(){ 
// [...] update order price 
}); 

我已經爲我解決了這個問題,但是我沒有用Selenium2嘗試它。

相關問題