Path: blob/master/6-Selenium/phantomjs/examples/page_events.js
164 views
// The purpose of this is to show how and when events fire, considering 5 steps1// happening as follows:2//3// 1. Load URL4// 2. Load same URL, but adding an internal FRAGMENT to it5// 3. Click on an internal Link, that points to another internal FRAGMENT6// 4. Click on an external Link, that will send the page somewhere else7// 5. Close page8//9// Take particular care when going through the output, to understand when10// things happen (and in which order). Particularly, notice what DOESN'T11// happen during step 3.12//13// If invoked with "-v" it will print out the Page Resources as they are14// Requested and Received.15//16// NOTE.1: The "onConsoleMessage/onAlert/onPrompt/onConfirm" events are17// registered but not used here. This is left for you to have fun with.18// NOTE.2: This script is not here to teach you ANY JavaScript. It's aweful!19// NOTE.3: Main audience for this are people new to PhantomJS.2021var sys = require("system"),22page = require("webpage").create(),23logResources = false,24step1url = "http://en.wikipedia.org/wiki/DOM_events",25step2url = "http://en.wikipedia.org/wiki/DOM_events#Event_flow";2627if (sys.args.length > 1 && sys.args[1] === "-v") {28logResources = true;29}3031function printArgs() {32var i, ilen;33for (i = 0, ilen = arguments.length; i < ilen; ++i) {34console.log(" arguments[" + i + "] = " + JSON.stringify(arguments[i]));35}36console.log("");37}3839////////////////////////////////////////////////////////////////////////////////4041page.onInitialized = function() {42console.log("page.onInitialized");43printArgs.apply(this, arguments);44};45page.onLoadStarted = function() {46console.log("page.onLoadStarted");47printArgs.apply(this, arguments);48};49page.onLoadFinished = function() {50console.log("page.onLoadFinished");51printArgs.apply(this, arguments);52};53page.onUrlChanged = function() {54console.log("page.onUrlChanged");55printArgs.apply(this, arguments);56};57page.onNavigationRequested = function() {58console.log("page.onNavigationRequested");59printArgs.apply(this, arguments);60};61page.onRepaintRequested = function() {62console.log("page.onRepaintRequested");63printArgs.apply(this, arguments);64};6566if (logResources === true) {67page.onResourceRequested = function() {68console.log("page.onResourceRequested");69printArgs.apply(this, arguments);70};71page.onResourceReceived = function() {72console.log("page.onResourceReceived");73printArgs.apply(this, arguments);74};75}7677page.onClosing = function() {78console.log("page.onClosing");79printArgs.apply(this, arguments);80};8182// window.console.log(msg);83page.onConsoleMessage = function() {84console.log("page.onConsoleMessage");85printArgs.apply(this, arguments);86};8788// window.alert(msg);89page.onAlert = function() {90console.log("page.onAlert");91printArgs.apply(this, arguments);92};93// var confirmed = window.confirm(msg);94page.onConfirm = function() {95console.log("page.onConfirm");96printArgs.apply(this, arguments);97};98// var user_value = window.prompt(msg, default_value);99page.onPrompt = function() {100console.log("page.onPrompt");101printArgs.apply(this, arguments);102};103104////////////////////////////////////////////////////////////////////////////////105106setTimeout(function() {107console.log("");108console.log("### STEP 1: Load '" + step1url + "'");109page.open(step1url);110}, 0);111112setTimeout(function() {113console.log("");114console.log("### STEP 2: Load '" + step2url + "' (load same URL plus FRAGMENT)");115page.open(step2url);116}, 5000);117118setTimeout(function() {119console.log("");120console.log("### STEP 3: Click on page internal link (aka FRAGMENT)");121page.evaluate(function() {122var ev = document.createEvent("MouseEvents");123ev.initEvent("click", true, true);124document.querySelector("a[href='#Event_object']").dispatchEvent(ev);125});126}, 10000);127128setTimeout(function() {129console.log("");130console.log("### STEP 4: Click on page external link");131page.evaluate(function() {132var ev = document.createEvent("MouseEvents");133ev.initEvent("click", true, true);134document.querySelector("a[title='JavaScript']").dispatchEvent(ev);135});136}, 15000);137138setTimeout(function() {139console.log("");140console.log("### STEP 5: Close page and shutdown (with a delay)");141page.close();142setTimeout(function(){143phantom.exit();144}, 100);145}, 20000);146147148