Commit c287c823 c287c823b2cad41a369178a7d0f4e6ff0a08c279 by Nicolas Perriault

refs #279 - added module (still WiP)

1 parent 9b9fb4b3
...@@ -35,6 +35,7 @@ var events = require('events'); ...@@ -35,6 +35,7 @@ var events = require('events');
35 var fs = require('fs'); 35 var fs = require('fs');
36 var http = require('http'); 36 var http = require('http');
37 var mouse = require('mouse'); 37 var mouse = require('mouse');
38 var casperpage = require('page');
38 var qs = require('querystring'); 39 var qs = require('querystring');
39 var tester = require('tester'); 40 var tester = require('tester');
40 var utils = require('utils'); 41 var utils = require('utils');
...@@ -120,6 +121,7 @@ var Casper = function Casper(options) { ...@@ -120,6 +121,7 @@ var Casper = function Casper(options) {
120 this.options = utils.mergeObjects(this.defaults, options); 121 this.options = utils.mergeObjects(this.defaults, options);
121 // properties 122 // properties
122 this.checker = null; 123 this.checker = null;
124 this.childPages = new casperpage.ChildPages();
123 this.cli = phantom.casperArgs; 125 this.cli = phantom.casperArgs;
124 this.colorizer = this.getColorizer(); 126 this.colorizer = this.getColorizer();
125 this.currentResponse = undefined; 127 this.currentResponse = undefined;
...@@ -138,7 +140,6 @@ var Casper = function Casper(options) { ...@@ -138,7 +140,6 @@ var Casper = function Casper(options) {
138 }; 140 };
139 this.mouse = mouse.create(this); 141 this.mouse = mouse.create(this);
140 this.page = null; 142 this.page = null;
141 this.childPages = [];
142 this.pendingWait = false; 143 this.pendingWait = false;
143 this.requestUrl = 'about:blank'; 144 this.requestUrl = 'about:blank';
144 this.resources = []; 145 this.resources = [];
...@@ -1865,17 +1866,13 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on ...@@ -1865,17 +1866,13 @@ Casper.prototype.waitWhileVisible = function waitWhileVisible(selector, then, on
1865 * Makes the provided child page as the currently active one. Note that the 1866 * Makes the provided child page as the currently active one. Note that the
1866 * active page will be reverted when finished. 1867 * active page will be reverted when finished.
1867 * 1868 *
1868 * @param WebPage childPage A child page instance 1869 * @param String|RegExp|WebPage childPageInfo Target child page information
1869 * @param Function then Next step function 1870 * @param Function then Next step function
1870 * @return Casper 1871 * @return Casper
1871 */ 1872 */
1872 Casper.prototype.withChildPage = function withChildPage(childPage, then) { 1873 Casper.prototype.withChildPage = function withChildPage(childPageInfo, then) {
1873 "use strict"; 1874 "use strict";
1874 if (!utils.isWebPage(childPage) || !this.childPages.some(function(activePage) { 1875 var childPage = this.childPages.find(childPageInfo);
1875 return activePage.id === childPage.id;
1876 })) {
1877 throw new CasperError("withChildPage() invalid or missing child page.");
1878 }
1879 if (!utils.isFunction(then)) { 1876 if (!utils.isFunction(then)) {
1880 throw new CasperError("withChildPage() requires a step function."); 1877 throw new CasperError("withChildPage() requires a step function.");
1881 } 1878 }
......
1 /*!
2 * Casper is a navigation utility for PhantomJS.
3 *
4 * Documentation: http://casperjs.org/
5 * Repository: http://github.com/n1k0/casperjs
6 *
7 * Copyright (c) 2011-2012 Nicolas Perriault
8 *
9 * Part of source code is Copyright Joyent, Inc. and other Node contributors.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 *
29 */
30
31 /*global CasperError console exports phantom require*/
32
33 var utils = require('utils');
34 var f = utils.format;
35
36 /**
37 * Child pages container. Implements Array prototype.
38 *
39 */
40 var ChildPages = function ChildPages(){};
41 exports.ChildPages = ChildPages;
42
43 ChildPages.prototype = [];
44
45 /**
46 * Finds a child page matching the provided information. Information can be:
47 *
48 * - RegExp: matching page url
49 * - String: strict page url value
50 * - WebPage: a direct WebPage instance
51 *
52 * @param Mixed pageInfo
53 * @return WebPage
54 */
55 ChildPages.prototype.find = function find(pageInfo) {
56 "use strict";
57 var childPage, type = utils.betterTypeOf(pageInfo);
58 switch (type) {
59 case "regexp":
60 childPage = this.findByRegExp(pageInfo);
61 break;
62 case "string":
63 childPage = this.findByURL(pageInfo);
64 break;
65 case "qtruntimeobject": // WebPage
66 childPage = pageInfo;
67 if (!utils.isWebPage(childPage) || !this.some(function(activePage) {
68 return activePage.id === childPage.id;
69 })) {
70 throw new CasperError("Invalid or missing child page.");
71 }
72 break;
73 default:
74 throw new CasperError(f("Invalid pageInfo type: %s.", type));
75 }
76 return childPage;
77 };
78
79 /**
80 * Finds the first child page which url matches a given RegExp.
81 *
82 * @param RegExp regexp
83 * @return WebPage
84 */
85 ChildPages.prototype.findByRegExp = function findByRegExp(regexp) {
86 "use strict";
87 var childPage = this.filter(function(activePage) {
88 return regexp.test(activePage.url);
89 })[0];
90 if (!childPage) {
91 throw new CasperError(f("Couldn't find child page with url matching pattern %s", regexp));
92 }
93 return childPage;
94 };
95
96 /**
97 * Finds the first child page matching a given url.
98 *
99 * @param String url The child WebPage url
100 * @return WebPage
101 */
102 ChildPages.prototype.findByURL = function findByURL(url) {
103 "use strict";
104 var childPage = this.filter(function(activePage) {
105 return activePage.url === url;
106 })[0];
107 if (!childPage) {
108 throw new CasperError(f("Couldn't find child page with url %s", url));
109 }
110 return childPage;
111 };