2013-06-27 6 views
9

मैं सेलेनियम 2 ड्राइवर के साथ बेहट और मिंक का उपयोग कर रहा हूं, और मैं fillField() फ़ंक्शन का उपयोग करने के बजाय सीधे फॉर्म फ़ील्ड (कच्चे कीबोर्ड इनपुट सिमुलेट) में टाइप करने का प्रयास कर रहा हूं।क्या मैं मिंक और सेलेनियम 2 का उपयोग करके कच्चे कीबोर्ड इनपुट भेज सकता हूं?

यह मैं क्या कोशिश कर रहा हूँ है:

$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

ऐसा लगता है कि कुंजीपटल के बारे में शिकायत करने वाली बहुत सी पोस्ट इरादे के रूप में काम नहीं कर रही हैं और कुछ ड्राइवर इसका समर्थन नहीं करते हैं। उदा .:

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

विशेष रूप से सेलेनियम चालक यह आदेशों है चलाने के लिए कस्टम जे एस पुस्तकालय का उपयोग करता है, लेकिन यह काम करने के लिए प्रतीत नहीं होता। मैंने भाग्य के बिना $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? से प्रमुख घटनाओं के जावास्क्रिप्ट अनुकरण का उपयोग करने के लिए पर्याप्त समाधान है (यदि आप jQuery का उपयोग नहीं कर रहे हैं तो वैकल्पिक विकल्प के लिए इसे देखें) और बेहट मिंक का मूल्यांकन स्क्रिप्ट फ़ंक्शन।

आप परीक्षण करने के लिए सीधे PHPUnit उपयोग कर रहे हैं:

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

:

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

या आप ककड़ी उपयोग कर रहे हैं, तो आप इस समारोह में जोड़ सकते हैं अपने FeatureContext.php फ़ाइल में जोड़ना और इसे अपनी फीचर फ़ाइल में इस तरह इस्तेमाल करें:

Given I manually press "a" 

समाधान के रूप में जावास्क्रिप्ट का उपयोग करने के लिए, कुछ ड्राइवरों की आवश्यक कुंजीपटल करने के लिए जावास्क्रिप्ट का उपयोग करें। उदा .:

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

0

सबसे आसान जवाब मैं पाया है जावास्क्रिप्ट में महत्वपूर्ण घटना को ट्रिगर और एक विशिष्ट Behat कदम ब्राउज़र को js भेज सकते हैं और यह गति प्रदान करने के बारे में है।

हम यूयूआई का उपयोग कर रहे हैं ताकि हम वाईयूआई कार्यक्रम अनुकरण का उपयोग कर सकें लेकिन jquery या देशी 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

मैं ज़ोंबी के साथ मिंक का उपयोग कर रहा हूं।जेएस और चूंकि यह कुंजीपटल घटनाओं को मूल रूप से नहीं पकड़ता है, इसलिए मैं focusout और keyup jQuery ईवेंट सुनता हूं।

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

मैं मेरे लिए समस्या को हल किया है, लेकिन मैं Selenium2 साथ यह कोशिश नहीं की।

संबंधित मुद्दे

 संबंधित मुद्दे