我将如何使用 behat 制作假签名

How would I make a fake signature with behat

Picture of tested code / Picture of working signature 您好,我正在将 behat 与 mink 集成的 selenium 驱动程序一起使用,并且我正在尝试编写一个输入假签名的测试。我们使用鼠标在屏幕上绘制签名,所以我希望能够让 selenium 为我做这件事。我尝试获取字段的 Id 并使用 dragTo('another element on my page'),但它只会在签名框内单击,不会执行任何其他操作。

我使用的框架是 laravel for php。

$field = $this->getSession()->getPage()->findById('ctlSignature);

$field->dragTo('another id on my page');

这行不通。

如果我能告诉鼠标单击然后向右移动 10 个像素然后再次单击那将是完美的,因为我可以用它来绘制签名。


感谢您的回复,但是当我 运行 此代码时出现错误,

$script = "var c = document.querySelector('#ctlSignature'); var ctx = c.getContext('2d'); ctx.moveTo(20,20); ctx.lineTo(50,50); ctx.stroke()";

$this->getSession()->evaluateScript($script);

发回错误 意外的令牌变量。如果我尝试在字符串内部使用,它会发送另一个错误,提示意外标记

解决方案是使用可以通过 evaluateScript 或 executeScript 执行的 javascript。

$this->getSession()->evaluateScript($script);

$script 变量是一个字符串,可以包含如下脚本:

var c = document.querySelector("canvas");
var ctx = c.getContext("2d");
ctx.moveTo(20,20);
ctx.lineTo(50,50);
ctx.stroke();

如果需要,您可以使用变量更改平局的值。

已在浏览器中测试,因此它可以正常工作,您需要确保切换到包含 canvas(如果有)的 iframe。

请使用相同的脚本查看以下步骤,但稍微更改以写入文本:


   /**
     * @Then /^signature test$/
     */
    public function signatureTest(){
        $script = 'var c = document.querySelector("canvas"); var ctx = c.getContext("2d"); ctx.fillStyle = "black"; ctx.font = "20px Georgia"; ctx.fillText("automation user",10,90);';
        $this->getSession()->executeScript($script);
    }

确保 select 或使用 select canvas 元素类型。

第二个版本,试试mousedown事件触发。

    /**
     * @Then /^signature test$/
     */
    public function signatureTest(){
        $function = <<<JS
        (function(){
        var c = document.querySelector("canvas");

        event = document.createEvent('MouseEvent');
        event.initEvent('mousedown', true, true);

        var ctx = c.getContext("2d");
        ctx.fillStyle = "black";
        ctx.font = "20px Georgia";
        ctx.fillText("automation user",10,90);

        c.dispatchEvent(event);
        })()
JS;
        $this->getSession()->executeScript($function);
    }