merlinthemagic / MTS

Automation Tools for PHP
GNU Lesser General Public License v3.0
111 stars 29 forks source link

How to wait page load complete #5

Closed quyeticb closed 8 years ago

quyeticb commented 8 years ago

How to wait web page load complete before clickElement(). I'm using sleep(), but sometime page slow to load.

merlinthemagic commented 8 years ago

Before we embark on another one of these quests, I need to see your modified PJSCtrl.js. Because the stock version of MTS has a function called commandWaitForWindowLoad() which handles the wait. That function relies on onLoadFinished from pjs (http://phantomjs.org/api/webpage/handler/on-load-finished.html).

However think about what constitutes a completely loaded page. Is a page completely loaded when the HTML, JS and images are loaded? For most pages that is ok, but many pages use dynamically fetched content (Ajax) and there is no way to account for that logic to finish processing in a general way.

PM me your PJSCtrl.js

merlinthemagic commented 8 years ago

@quyeticb did you resolve the issue, can i go ahead and close?

quyeticb commented 8 years ago

I can't solved it. Wait suggest from you

merlinthemagic commented 8 years ago

Then please send me your modified PJSCtrl.js.

merlinthemagic commented 8 years ago

If PJSCtrl.js still invokes the original logic then here is an example of logic you could implement to wait for AJAX content to finish loading:

$timeout    = 30; //in seconds
$selector   = "a.one:nth-of-type(2)"; //selector you wish to click

$tTime      = time() + $timeout;
$pageReady  = null;
while($pageReady === null) {

    $sExist     = $windowObj->getSelectorExists($selector);
    if ($sExist === true) {
        $pageReady  = true;
    } elseif (time() > $tTime) {
        //failed to get ready before timeout
        $pageReady  = false;
    }
}

if ($pageReady === false) {
    //the selector did not load in time. Exception or other logic to handle this condition...
    throw new \Exception("Selector: " . $selector . ", did not load in: " . $timeout . " seconds");
} else {
    //selector is ready click it
    $windowObj->clickElement($selector);
    //more logic
}
merlinthemagic commented 8 years ago

Did my last suggestion solve your issue?

quyeticb commented 8 years ago

I have fixed.

merlinthemagic commented 8 years ago

Do you mind sharing what the issue was and how your resolved it?

quyeticb commented 8 years ago

Same as your solved code. Using php try and catch with timeout.

merlinthemagic commented 8 years ago

thx