javascriptjqueryphantomjsmocha-phantomjs

Phantomjs reloading same page for every 5 seconds


I want my phantomjs script to keep performing reloading/refreshing on given argument input domain https://google.com for every 5 seconds. How do i achieve that?

phantomjs test.js https://google.com

var page = require('webpage').create(),
    system = require('system'),
    address;

page.onAlert = function (msg) {
    console.log("Received an alert: " + msg);
};

page.onConfirm = function (msg) {
    console.log("Received a confirm dialog: " + msg);
    return true;
};

if (system.args.length === 1) {
    console.log("Must provide the address of the webpage");
} else {
    address = system.args[1];
    for(var i=0; i <= 10; i++){
    page.open(address, function (status) {
        if (status === "success") {
            console.log("opened web page successfully!");
            page.evaluate(function () {
                var e = document.createEvent('Events');
                e.initEvent('click', true, false);
                document.getElementById("link").dispatchEvent(e);
            });
        }
    }); }
}

Solution

  • You can use setTimeout to call a function that loads the page a certain amount of time after it loads:

    var page = require('webpage').create(),
        system = require('system'),
        address;
    
    page.onAlert = function (msg) {
        console.log("Received an alert: " + msg);
    };
    
    page.onConfirm = function (msg) {
        console.log("Received a confirm dialog: " + msg);
        return true;
    };
    
    function loadPage() {
      if (system.args.length === 1) {
        console.log("Must provide the address of the webpage");
      } else {
        address = system.args[1];
        page.open(address, function (status) {
          if (status === "success") {
            console.log("opened web page successfully!");
            page.evaluate(function () {
              var e = document.createEvent('Events');
              e.initEvent('click', true, false);
              document.getElementById("link").dispatchEvent(e);
            });
          }
          setTimeout(loadPage, 5000) // Call the function loadPage again in 5 seconds
        });
      }
    }
    
    loadPage()