How would I make a fake signature with behat - php

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.
The framework I am using is laravel for php.
$field = $this->getSession()->getPage()->findById('ctlSignature);
$field->dragTo('another id on my page');
This does not work.
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);
Sends a error back Unexpected token var. If i try to use inside the string it sends another error saying unexpected token

The solution would be to use a javascript that you can execute with evaluateScript or executeScript.
$this->getSession()->evaluateScript($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.
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.
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);
}

Related

Google-App-Script vs php in encoding base64

This php code decodes the secret key before hashing with SHA 512
$API_SECRET_KEY="W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg=";
$BDAPI_SECRET_KEY=base64_decode($API_SECRET_KEY);
$HMAC_SIGN = base64_encode(hash_hmac('sha512',$MESSAGE,$BDAPI_SECRET_KEY,true));
echo $HMAC_SIGN;
BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==
I want to replicate this in google app script
var Secret = "W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg="
var BDSecret= Utilities.base64Decode(Secret)
var hmac = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, message, BDSecret ));
Logger.log(hmac)
ew5KhLWSJixn8zw4s6VkpYIwvGBjrmjY3LhNWZr9CVEw6W22LOGg+lVzA3uQgOVyICSCffw2bzTepnBdoYtldw==
If I do not decode the API before hashing they return the same result. But for this particular purpose, the key needs to be decoded. The message variable is just my first name "Parit" in case someone wants to replicate.
I thought that Utilities.computeHmacSignature() might not be able to use []byte for the value. So as a workaround, how about using jsSHA? I think that in your case, you can use https://github.com/Caligatio/jsSHA/blob/master/src/sha512.js.
The flow for using jsSHA is as follows.
Flow :
Download sha512.js.
On script editor, create new script as for example, the filename of sha512.js.
Copy and paste the script of sha512.js to the created script.
Copy and paste the sample script to Code.gs of the script editor.
Run myFunction() of the sample script.
Sample script :
function myFunction() {
var message = "Parit";
var secret = "W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg=";
var obj = new jsSHA("SHA-512", "TEXT");
obj.setHMACKey(secret, "B64");
obj.update(message);
Logger.log(obj.getHMAC("B64"))
}
Note :
When I tested Parit for message, I got BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==.
It this was not useful for you, I'm sorry.
Update :
By the Google's update at June 19, 2018, Utilities.computeHmacSignature() got to be able to use the byte arrays. By this, using only native Google Apps Scvript, the result can be retrieved without using jsSHA. So I would like to update my answer.
Modified script :
function myFunction() {
var message = "Parit";
var secret = "W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg=";
var value = Utilities.base64Decode(Utilities.base64Encode(message));
var key = Utilities.base64Decode(secret);
var out = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, value, key);
var res = Utilities.base64Encode(out)
Logger.log(res)
}
Result :
BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==

how to load php array into javascript variable on runtime

i have a website/(mobile, app created with phone gap build), in this site i load some arrays with php into javascript to show some ads. on website it's work very well, but on the mobile apps (iOS and android) i got an empty space. if i fill the array variables in js directly, it works in all versions of the site web and mobile.
the problem is i have about 460 different ads to show. i like to load about 20 ads into js when users start or load the site/app in php i read the mysql db and load these ads witch shown less. after showing the ads, ads-couter +1 .(didn't show it here)
the reason why i'm doing this so, is, because i did'n find something to monetize my html5 css3 query web app, compiled with phone gap build.
now can someone show me or explain me why it works on web but not on app, or why it works both when i fill the variable in js direct??
my php works fine -> to js
in js i have this:
// werbung
function werbung_fill(){
id = new Array;
comment_start = new Array;
bannerLinks = new Array;
adBanners = new Array;
bannerTargets = new Array;
dimension = new Array;
comment_end = new Array;
aktiv = new Array;
angezeigt = new Array;
lasttime = new Array;
$.getJSON('inc/werbung_call.php', function(data) {
/* data will hold the php array as a javascript object */
$.each(data, function(key, val) {
id.push(val.id);
comment_start.push(val.comment_start);
bannerLinks.push(val.bannerLinks);
adBanners.push(val.adBanners);
bannerTargets.push(val.bannerTargets);
dimension.push(val.dimension);
comment_end.push(val.comment_end);
aktiv.push(val.aktiv);
angezeigt.push(val.angezeigt);
lasttime.push(val.lasttime);
});
});
}
function werbung(){
var randNum = Math.floor(Math.random() * (19 - 0 + 1)) + 0;
var topAdBanner = $('#topad > a > img');
var newcomment_start = comment_start[randNum];
var newBannerImg = adBanners[randNum];
var newBannerLink = bannerLinks[randNum];
var newBannerTarget = bannerTargets[randNum];
var newdimension = dimension[randNum];
var newcomment_end = comment_end[randNum];
// update new img src and link HREF value
$(topAdBanner).attr('src',newBannerImg);
$('#topad > a').attr('href',newBannerLink);
$('#topad > a').attr('target',newBannerTarget);
$('#topad > a').attr('alt',newdimension);
var deinTimer = window.setTimeout(werbung, 5000);
//$('#topad1').html(id[randNum]);
}
in
$(document).ready(function() {
// werbungs banner
$('<div id="topad"><img src="" width="320" height="50" alt="" border="0"></div><!-- #end #topad -->').prependTo( $( "#hauptheader" ) );
// werbung
werbung_fill();
werbung();
// werbung
}
with this, i can inject a div without compiling the apps again. (it works fine)
the js, php, index.html are on the same server and the compiled apps use the js css images from server too.
You have a few syntax errors in your JavaScript. You should validate your JavaScript here: http://www.jshint.com/
I suspect Android isn't parsing this correctly:
id = new Array;
Suggest using this syntax instead:
var id = [];

Changing background image src periodically using jQuery and AJAX

I'm trying to have the background image of the body of my page change every (approximatly) N seconds. For this i have a PHP function that echoes random image src when i call it using AJAX. I then replace the current body background image src by this newly generated one, but i can't get it to work.
Here is the code, is there anything wrong in it ?
(function($) {
var $body = $('body');
window.setInterval(function() {
$.ajax({
url: 'http://localhost/some_path/index.php?exec=getNewBkgImgSrc',
}).done(function(data) {
var newImage = new Image();
newImage.onload = function() {
$body.css('background-image', newImage.src);
};
newImage.src = data;
});
}, 5000);
})(window.jQuery);
And the PHP function which does work :
public function getNewBkgImgSrc() {
echo CONTENT_URL_DIR.'/Wallpapers/wallpaper ('.rand(1, 20).').jpg';
}
Could this be because i'm using localhost ? I can mremeber having AJAX problems before when i was developping locally.
EDIT : A clue maybe is that the style property of the body element doesn't sseem to change / be set in Firebug. I also tried $('body').first() and $(document.body) and using url("") (in the value of background-image property) but it doesn't work either. The onload event does fire however.
Thanks for your help.
Why are you creating a new image object if you are getting a URL back from PHP? Just set the background-image property of body to:
$body.css('background-image', "url('" + data + "')");
If using vanilla JS:
document.body.style.backgroundImage="url('" + data + "')";
See proper format for that property here: http://www.w3schools.com/cssref/pr_background-image.asp
might need to cache bust the ajax call. Try changing the url property of the ajax call to
'/some_path/index.php?exec=getNewBkgImgSrc&cb='+((new Date).getTime()).
I have not good sense about var $body = $('body');
use $('body').css('background-image', newImage.src);

Making Tab Persist when Reloading Page

I'm trying to modify Gaya Design's Tabbed Content (Available Here) to have the current tab persist when the page is reloaded, yet have it change when a new tab is clicked. I've already changed it a little to be able to change default tab by using a PHP GET variable. The current condition of the page I'm working on can be viewed here.
So here's my likely scenario. If you've clicked on the link above, you'll see I'm working on a simple PHP shopping cart. Now when a user clicks an add link, it has to reload the page, and when it does that it resets the tab. So, I'm thinking this should easily be solved with a cookie that updates whenever a new tab is clicked....I'm just not too sure how to go about this. Any thoughts, suggestions, or advice will be greatly appreciated.
Here's my current JS:
var TabbedContent = {
init: function() {
$(".category").click(function() {
var background = $(this).parent().find(".selected");
$(background).stop().animate({
left: $(this).position()['left']
}, {
duration: 350
});
TabbedContent.slideContent($(this));
});
},
slideContent: function(obj) {
var margin = $(obj).parent().parent().find(".sliderContainer").width();
margin = margin * ($(obj).prevAll().size() - 1);
margin = margin * -1;
$(obj).parent().parent().find(".displayContent").stop().animate({
marginLeft: margin + "px"
}, {
duration: 1
});
},
gotab: function( obj ) {
var background = $(obj).parent().find(".selected");
$(background).stop().animate({
left: $(obj).position()['left']
}, {
duration: 1
});
TabbedContent.slideContent( $(obj) );
}
}
$(document).ready(function() {
TabbedContent.init();
});
Here's how a tab is initialized when it is linked to:
<?php
// Load a specific tab if required
if(isset($_GET['tab'])) {
// Array storing possible tab IDs
$tabChoices = array('productsTab', 'specsTab', 'brochuresTab', 'bannersTab', 'kitsTab', 'displaysTab');
$tab = '';
if(in_array($_GET['tab'], $tabChoices)) $tab = $_GET['tab'];
// Default to productsTab if not in array list
else $tab = 'productsTab';
// JS to actually do the switch
echo '<script>$(document).ready(function() {TabbedContent.gotab($("#' . $tab . '"))});</script>';
}
?>
You're painting yourself into a corner by inline scripting a solution. You should always only have one $(document).ready... call in your entire product, in order to avoid order dependent explosions in code, and have a clear point of entry.
That said, you are almost there. Instead of calling a function, assign a value.
echo "<script>var selectedTab=$tab;</script>"
Then during your initialization function, make use of that value. My example is in global scope. There may be a race condition if you try to assign it to a namespace. In that case, try putting that script at the bottom of the page.
One more suggestion, have one and only one function handle all of your animations calls for that object.
Instead of using get/post params you could use hash; creating links like this in the tabs:
<a class="tab_item" href="#one_go" id="one">
And then put this in the javascript:
var gototab = document.location.hash.replace('_go',"")
if(gototab){
$(gototab).each(function(){
var pos = $(this).prevAll(".tab_item").length,
left = pos * $(this).outerWidth(),
margin = pos * $(this).parent().parent().find(".slide_content").width() * -1;
$(this).parent().find('.moving_bg').css('left',left)
$(this).parent().parent().find(".tabslider").css('margin-left',margin)
})
}

Flex 4,image create snapshoot from video display?

my code works on localhost,but not on the server,what could be the problem?I also want to return some string back from php to flex app but I can't solve that.
Here is my code :
[Bindable]
public var encodedString:String;
[Bindable]
public var img_name:String;
[Bindable]
public var pid:int;
[Bindable]
public var url_string:String;
protected function btn_save_clickHandler(event:MouseEvent):void
{
var bmpData:BitmapData = new BitmapData(videoDisplay.width,videoDisplay.height-25);
bmpData.draw(videoDisplay);
var bm:Bitmap = new Bitmap(bmpData);
img.source = bm;
var jpg_e:JPEGEncoder = new JPEGEncoder();
var bytes:ByteArray = new ByteArray();
bytes = jpg_e.encode(bmpData);
var b64e:Base64Encoder = new Base64Encoder()
b64e.encodeBytes(bytes);
encodedString = b64e.flush();
}
protected function btn_save_image_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
save_img.send();
}
<mx:request xmlns="">
<img_data>
{encodedString}
</img_data>
<image_name>
{img_name}
</image_name>
<post_id>
{pid}
</post_id>
</mx:request>
</mx:HTTPService>
When click save,I want to save snapshoot from videodisplay to img.Also I send image to php,and want to get simple text answer that is done,but I always get object returned.
and I save image with php,and just print message,like this :
print "this is message!";
I think the problem is when I need to save image from control(video display) to image control,but this problem only occure on server but not on localhost
You need to change the last line from:
str = b64e;
To:
str = b64e.toString();
Note that calling toString() will clear the b64e object.
Update:
Let's try placing the video in a container, and then doing the .draw() on that, we'll add some additional content to it so that we can get that if the video doesn't show up.
<s:Group id="videoContainer">
<mx:Label text="Hello" />
<mx:VideoDisplay id="videoDisplay" />
</s:Group>
Hopefully you are already doing the VideoDisplay part of this, and you can update your code to add the containing group.
When you do the .draw() use videoContainer instead of videoDisplay ie. :
bmpData.draw(videoContainer);
This is just to test, if there is a sandbox issue, which it defintely seems to be, only "Hello" will show up in the bitmap, and the video will still not be seen.
Where does that video come from? Camera? FMS?
It might be either a crossdomain issue or something related to FMS (or another streaming server).
This post could help you : How do I specify a crossdomain policy file to allow Flash to grab a bitmap from an RTMP (Wowza) video stream?

Categories