How to evaluate javascript code from Behat to Selenium properly?
I have following code (based on this) in my custom step definition:
$js = <<<JS
function checkVisibility( elm, evalType ) {
evalType = evalType || "visible";
var vpH = jQuery(window).height(), // Viewport Height
st = jQuery(window).scrollTop(), // Scroll Top
y = jQuery(elm).offset().top,
elementHeight = jQuery(elm).height();
if (evalType === "visible") return ((y < (vpH + st)) && (y > (st - elementHeight)));
if (evalType === "above") return ((y < (vpH + st)));
}
var el = document.querySelector("$locator");
return checkVisibility(el, "visible");
JS;
$result = $this->getSession()->evaluateScript($js);
Similar code is working well in chrome console, but in Behat context $result
is always null.
I'm using Behat: 3.0.15
with Selenium 3.4
and PhantomJS as a browser.
In general it seems that if I passing one-line code it's working ok. No matter what is inside my function, I've always null, so below code also result with null:
$js = <<<JS
function checkVisibility() {
return true;
}
// var el = document.querySelector("$locator");
return checkVisibility();
JS;
I have something like bellow that works:
$js = <<<JS
(function myFunction() {
//function content
})()
JS;
You should wait for the element before to make sure the element is visible.
Also to get the response you should have something like:
$result = $this->getSession()->evaluateScript("return $js");
链接地址: http://www.djcxy.com/p/83610.html