0

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;
Codium
  • 3,092
  • 5
  • 30
  • 54

1 Answers1

1

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");
lauda
  • 4,043
  • 2
  • 12
  • 27
  • And when you put `return true` to the `myFunction()` you get `true` on PHP also? – Codium Jul 18 '17 at 07:49
  • Yes, I get `true` in PHP also. I am using page objects and I have something like `$this->getDriver()->evaluateScript("return $function");` in a page object. – lauda Jul 18 '17 at 08:02
  • So it's something wrong with my environment. In my case `$result` is always `null` :/ Are you using phantomjs as browser? – Codium Jul 18 '17 at 08:08
  • 1
    No, I am using chrome. – lauda Jul 18 '17 at 08:19