Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以用水貂和硒发送原始键盘输入吗?_Php_Selenium Webdriver_Behat_Mink - Fatal编程技术网

Php 我可以用水貂和硒发送原始键盘输入吗?

Php 我可以用水貂和硒发送原始键盘输入吗?,php,selenium-webdriver,behat,mink,Php,Selenium Webdriver,Behat,Mink,我在Selenium2驱动程序中使用Behat和Mink,并尝试直接在表单字段中键入内容(模拟原始键盘输入),而不是使用fillField()函数 这就是我正在尝试的: $element = $this->getSession()->getPage()->find('css', '#questionName'); $element->focus(); $element->keyPress('a'); // also tried this, with no suc

我在Selenium2驱动程序中使用Behat和Mink,并尝试直接在表单字段中键入内容(模拟原始键盘输入),而不是使用
fillField()
函数

这就是我正在尝试的:

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

$element->keyPress('a');

// also tried this, with no success
// $element->keyDown('a');
// $element->keyUp('a');
页面上有一个
元素。它正确接收焦点,但不响应任何模拟键盘输入

可以这样模拟原始键盘输入吗?

我做错了什么?

似乎有很多帖子抱怨按键没有按预期工作,一些司机根本不支持。e、 g:

Goutte-Behat\Mink\Driver\GoutteDriver不支持键盘操作

Selenium驱动程序尤其使用自定义js库来运行其命令,但它似乎不起作用。我尝试过使用
$this->getSession()->getDriver()->keyPress()
$element->getPress()
,但运气不好

有趣的是,在Selenium2代码库中还没有针对按键事件的单元测试(因此我假设它目前正在开发中)

因此,目前,一个合适的解决方案是使用来自的关键事件的javascript模拟(如果您不使用jQuery,请参阅本文了解替代方案)和Behat Mink的evaluateScript函数

如果您使用直接PHPUnit进行测试:

$key = 'a';
$script = "jQuery.event.trigger({ type : 'keypress', which : '" . $key . "' });";
$this->getSession()->evaluateScript($script);
或者,如果您正在使用Cucumber,请将其添加到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执行所需的按键操作。例如:


我找到的最简单的答案是触发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);
    }
public function在编辑器中按按键($keys,$fieldlocator){
//NodeElement.keyPress根本不起作用。
如果(!$this->running_javascript()){
抛出新的编码异常(“选择文本需要javascript”);
}
//我们将委托给behat_form_field类,它将
//猜对类型。
$field=behat\u field\u manager::从标签获取表单字段($fieldlocator,$this);
如果(!method_存在($field,'get_value')){
抛出新的编码_异常('字段不支持get_value函数');
}
$editorid=$this->find_字段($fieldlocator)->getAttribute('id');
//获取范围的查询值。
$js='1
函数TriggerKeyPressBehat(){
// http://www.wfimc.org/public/js/yui/3.4.1/docs/event/simulate.html
YUI()。使用(\'node-event-simulate\',函数(Y){
var id=“”.$editorid.”;
变量节点=Y.one(“#”+id+“可编辑”);
node.focus();
var keyEvent=“keypress”;
如果(Y.UA.webkit | Y.UA.ie){
keyEvent=“keydown”;
}
//触发此按钮的键盘快捷键的键代码(向上箭头):
var keys=['.$keys.'];

对于(var i=0;i我将Mink与Zombie.js一起使用,并且由于它不以本机方式捕获键盘事件,所以我都会收听
focusout
keyup
jQuery事件

$('form[name="order"]').find('input[id$="quantity"],input[id$="price"]').bind('keyup focusout', function(){
// [...] update order price
});
我已经为自己解决了这个问题,但我没有尝试使用硒2