2016-09-13 67 views
0

Picture of tested code/Picture of working signature您好,我正在使用集成了水貂的selenium驱动程序的behat,我试图编写一个输入假签名的测试。我们使用鼠标在屏幕上绘制签名,所以我希望能够让硒为我做。我尝试抓取字段的Id并使用dragTo('我的页面上的另一个元素'),但它只在签名框内单击,并且不执行任何操作。我会如何使用behat伪造签名

我使用的框架是php的laravel。

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

发送一个错误背部意外标记变种。如果我尝试在字符串内部使用,它会发送另一个错误,说出现意外的令牌

+0

?是一个基于jQuery的画布或其他东西? – lauda

+0

啊,好的,是的,我们正在使用SuerSignature.js,他们的网站是www.SuperSignature.com。 –

回答

0

解决方法是使用可以使用evaluateScript或executeScript执行的JavaScript。

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

的$脚本变量是可以包含类似的脚本的字符串:

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

如果需要的话,你可以通过使用变量改变平局的数值。

在浏览器中测试过,所以它可以正常工作,您需要确保切换到包含画布的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); 
    } 

确保用于将选择画布元素类型的选择。

第二个版本,请尝试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); 
 
    }

正在使用的标志性特征是什么框架的开发者
+0

对此的回复是原始问题,谢谢。 –

+0

所以,我可以把它写在屏幕上,但它不接受它作为签名。它似乎要求它是一个鼠标向下的事件来接受签名。 –

+0

好的,那么它可能工作,如果onmousedown事件触发,我会尝试几件事情 – lauda