我将如何使用Behat制作假签名 [英] How would I make a fake signature with behat

查看:64
本文介绍了我将如何使用Behat制作假签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试代码的图片/

Picture of tested code / Picture of working signature Hello I am using behat with the selenium driver integrated with mink and I am trying to write a test that inputs a fake signature. We use the mouse to draw the signature on the screen so I want to be able to have selenium do that for me. I tried grabbing the Id of the field and using dragTo('another element on my page'), but it only clicks inside the signature box and doesnt do anything else.

我正在使用的框架是laravel for php.

The framework I am using is laravel for php.

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

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

这不起作用.

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

If I can tell the mouse to click then move 10 pixels to the right then click again that would be perfect as I can use that to draw a signature.

谢谢您的回应,但是当我运行这段代码时,我得到了一个错误,

Thank you for the response but when I run this code I get a error,

$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);

向后发送错误意外的令牌var.如果我尝试在字符串中使用它,则会发送另一个错误消息,提示意外令牌

Sends a error back Unexpected token var. If i try to use inside the string it sends another error saying unexpected token

推荐答案

解决方案是使用可以与valuateScript或executeScript一起执行的JavaScript.

The solution would be to use a javascript that you can execute with evaluateScript or executeScript.

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

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

The $script variable is a string that could contain a script like:

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

如果需要,您可以使用变量来更改绘图的值.

If needed you can change the values of the draw by using variables.

在浏览器中进行了测试,以使其正常工作,您需要确保切换到包含画布的iframe(如果有).

Tested in the browser so it works, what you need to make sure is to switch to the iframe that contains the canvas, if any.

请使用相同的脚本查看下面的步骤,但是稍稍更改一下以编写文本:

Please see bellow step with same script but slightly changed to write text:


   /**
     * @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);
    }

确保使用的选择器将选择画布元素类型.

Make sure the selector used will select the canvas element type.

第二个版本,请尝试使用mousedown事件触发器.

Second version, try mousedown event trigger.

    /**
     * @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);
    }

这篇关于我将如何使用Behat制作假签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆