【问题标题】:How do I simulate a click on a specific position using phpUnit / selenium?如何使用 phpUnit / selenium 模拟对特定位置的点击?
【发布时间】:2012-11-02 14:21:21
【问题描述】:

我正在使用 phpunit 3.7 和 selenium 进行测试。为了测试文本突出显示功能,我想单击 div 中的特定 x,y 位置(包含文本)。

我尝试了 moveTo() 函数 (see),但我总是收到错误,尽管该元素是 PHPUnit_Extensions_Selenium2TestCase_Element 类的实例:“PHPUnit_Extensions_Selenium2TestCase_Exception:仅支持移动元素。请传递 PHPUnit_Extensions_Selenium2TestCase_Element 实例。 ”。

$div = $this->byCssSelector('#texthighlight-instant_feedback div div');

if ($div instanceof PHPUnit_Extensions_Selenium2TestCase_Element) {
     $this->moveto(array(
        'element' => $div,
        'xoffset' => 100,
        'yofsset' => 5
     ));
     $this->click();
} else {
    throw new PHPUnit_Extensions_Selenium2TestCase_Exception('no instanceof PHPUnit_Extensions_Selenium2TestCase_Element');
}

此外,“点击”命令似乎不存在或不受支持。 我刚开始使用 phpunit / selenium 并希望其他人现在可以如何模拟对元素中特定位置的点击?谢谢!

【问题讨论】:

    标签: selenium phpunit


    【解决方案1】:

    当你在 SessionCommand\MoveTo.php 中查看源代码时,你似乎不应该这样做

    $this->moveto(array(
        'element' => $div,
        'xoffset' => 100,
        'yofsset' => 5
     ));
    

    但是:

    $this->moveto($div);
    

    但是,我得到了 BadMethodCallException:命令“moveTo”不存在或不支持,无论我尝试了哪种调用方式。

    我想移动光标的原因:所以我可以右键单击一个元素...

    编辑:

    查看代码时我突然意识到我正在调用 moveTo,而不是 moveto.... 至少我摆脱了我的异常。顺便说一句:我的建议应该仍然适用于您的问题。

    您在使用 click() 时遇到什么问题?有两种不同的 click() 方法,一种在会话上 ($this->click()),另一种在元素上 ($div->click)。

    编辑2:

    click() 似乎没有被初始化为会话中的可用命令。定义了 Buttondown 和 buttonup。使用第一个 buttondown 然后 buttonup 应该会给你点击。

    不过我还是不走运。 buttondown 和 buttonup 似乎没有将关联数组作为 JSON 参数传递给 Selenium 服务器,因此我无法模拟鼠标中键或右键。

    现在我在 Session.php 中添加 click 作为 genericpost 选项,因为这样我可以调用:

    $this->click('button'=>2);
    

    右键单击我刚刚将光标移到的元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多