2013-06-27 16 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()との両方を運を使わずに試しました。

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のコードベースでのKeyPressイベントのためのユニットテストは、(私はそれが現在開発中だと仮定)はまだ存在しないということです。

したがって、適切な解決策は、Is it possible to simulate key press events programmatically?(jQueryを使用していない場合はこれを参照)とBehat MinkのevaluateScript関数のキーイベントのjavascriptエミュレーションを使用することです。

あなたがテストにまっすぐには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を使用する場合、必要なkeyPressを実行するためにjavascriptを使用します。例えば:

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

0

私が見つけた最も簡単な答えは、JavaScriptでキーイベントをトリガし、ブラウザにJSを送信し、それをトリガーするために、特定のbehatステップを書くことです。

私たちはYUIイベントシミュレートを使用していますが、jqueryまたはnative jsで処理しています。概念は重要です。ネイティブのサポートがそこにあるまで、私が見つけた最高のソリューションです。

これが役に立ちます。

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

私はMomb with Zombieを使用しています。jsとキーボードイベントをネイティブに捕まえないので、私は両方ともfocusoutkeyup jQueryイベントを聞いています。

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

私はこの問題を解決しましたが、私はSelenium2で試していませんでした。

関連する問題