Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Refactored some of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Feb 16, 2015
1 parent 0c8bcfb commit 3e8507f
Show file tree
Hide file tree
Showing 8 changed files with 1,527 additions and 108 deletions.
147 changes: 84 additions & 63 deletions src/browser.js
Expand Up @@ -131,7 +131,7 @@ class Browser extends EventEmitter {
if (this.silent)
debug(`>> ${message}`);
else
console.log(message);
process.stdout.write(message + '\n');
})
.on('log', (...args)=> {
// Message written to browser.log.
Expand Down Expand Up @@ -1280,69 +1280,34 @@ class Browser extends EventEmitter {
process.stdout.write('No document\n');
}

}


Object.assign(Browser, {

VERSION,

// These defaults are used in any new browser instance.
default: {

// Which features are enabled.
features: DEFAULT_FEATURES,

// Tells the browser how many redirects to follow before aborting a request. Defaults to 5
maxRedirects: 5,

// Proxy URL.
//
// Example
// Browser.default.proxy = 'http://myproxy:8080'
proxy: null,

// If true, supress `console.log` output from scripts (ignored when DEBUG=zombie)
silent: false,

// You can use visit with a path, and it will make a request relative to this host/URL.
site: undefined,

// Check SSL certificates against CA. False by default since you're likely
// testing with a self-signed certificate.
strictSSL: false,

// Sets the outgoing IP address in case there is more than on available.
// Defaults to 0.0.0.0 which should select default interface
localAddress: '0.0.0.0',

// User agent string sent to server.
userAgent: `Mozilla/5.0 Chrome/10.0.613.0 Safari/534.15 Zombie.js/${VERSION}`,

// Navigator language code
language: 'en-US',

// Default time to wait (visit, wait, etc).
waitDuration: '5s',

// Indicates whether or not to validate and execute JavaScript, default true.
runScripts: true
// ### zombie.visit(url, callback)
// ### zombie.visit(url, options? callback)
//
// Creates a new Browser, opens window to the URL and calls the callback when
// done processing all events.
//
// * url -- URL of page to open
// * callback -- Called with error, browser
static visit(url, options, callback) {
if (arguments.length === 2 && typeof(options) === 'function')
[options, callback] = [null, options];
const browser = new Browser(options);
if (callback)
browser.visit(url, (error)=> callback(error, browser));
else
return browser.visit(url).then(()=> browser);
},


// Use this function to create a new Browser instance.
create(options) {
static create(options) {
return new Browser(options);
},

// Debug instance. Create new instance when enabling debugging with Zombie.debug
_debug: debug('zombie'),
}

// Enable debugging. You can do this in code instead of setting DEBUG environment variable.
debug() {
static debug() {
debug.enable('zombie');
this._debug = debug('zombie');
},
}

// Allows you to masquerade CNAME, A (IPv4) and AAAA (IPv6) addresses. For
// example:
Expand All @@ -1353,11 +1318,11 @@ Object.assign(Browser, {
// Brower.dns.map('example.com', '::1')
//
// See also Browser.localhost.
get dns() {
static get dns() {
if (!this._dns)
this._dns = new DNSMask();
return this._dns;
},
}

// Allows you to make request against port 80, route them to test server running
// on less privileged port.
Expand All @@ -1370,11 +1335,11 @@ Object.assign(Browser, {
// Browser.ports.map('example.com', '3000')
//
// See also Browser.localhost.
get ports() {
static get ports() {
if (!this._ports)
this._ports = new PortMap();
return this._ports;
},
}

// Allows you to make requests against a named domain and port 80, route them to
// the test server running on localhost and less privileged port.
Expand All @@ -1391,23 +1356,79 @@ Object.assign(Browser, {
// This is equivalent to DNS masking the hostname as 127.0.0.1 (see
// Browser.dns), mapping port 80 to the specified port (see Browser.ports) and
// setting Browser.default.site to the hostname.
localhost(hostname, port) {
static localhost(hostname, port) {
this.dns.localhost(hostname);
this.ports.map(hostname, port);
if (!this.default.site)
this.default.site = hostname.replace(/^\*\./, '');
},
}


// Register a browser extension.
//
// Browser extensions are called for each newly created browser object, and
// can be used to change browser options, register listeners, add methods,
// etc.
extend(extension) {
static extend(extension) {
this._extensions.push(extension);
}

}


Object.assign(Browser, {

VERSION,

// These defaults are used in any new browser instance.
default: {

// Which features are enabled.
features: DEFAULT_FEATURES,

// Tells the browser how many redirects to follow before aborting a request. Defaults to 5
maxRedirects: 5,

// Proxy URL.
//
// Example
// Browser.default.proxy = 'http://myproxy:8080'
proxy: null,

// If true, supress `console.log` output from scripts (ignored when DEBUG=zombie)
silent: false,

// You can use visit with a path, and it will make a request relative to this host/URL.
site: undefined,

// Check SSL certificates against CA. False by default since you're likely
// testing with a self-signed certificate.
strictSSL: false,

// Sets the outgoing IP address in case there is more than on available.
// Defaults to 0.0.0.0 which should select default interface
localAddress: '0.0.0.0',

// User agent string sent to server.
userAgent: `Mozilla/5.0 Chrome/10.0.613.0 Safari/534.15 Zombie.js/${VERSION}`,

// Navigator language code
language: 'en-US',

// Default time to wait (visit, wait, etc).
waitDuration: '5s',

// Indicates whether or not to validate and execute JavaScript, default true.
runScripts: true
},

Assert,
Resources,


// Debug instance. Create new instance when enabling debugging with Zombie.debug
_debug: debug('zombie'),

_extensions: []

});
Expand Down
2 changes: 1 addition & 1 deletion src/dom_focus.js → src/dom/focus.js
@@ -1,7 +1,7 @@
// Support for element focus.


const DOM = require('./dom');
const DOM = require('./index');


const FOCUS_ELEMENTS = ['INPUT', 'SELECT', 'TEXTAREA', 'BUTTON', 'ANCHOR'];
Expand Down
5 changes: 3 additions & 2 deletions src/forms.js → src/dom/forms.js
@@ -1,5 +1,5 @@
// Patches to JSDOM for properly handling forms.
const DOM = require('./dom');
const DOM = require('./index');
const File = require('fs');
const Mime = require('mime');
const Path = require('path');
Expand Down Expand Up @@ -120,7 +120,8 @@ DOM.HTMLFormElement.prototype._eventDefaults.submit = function(event) {
// -------

// Default behavior for clicking on inputs.
DOM.HTMLInputElement.prototype._eventDefaults = {};
DOM.HTMLInputElement.prototype._eventDefaults =
Object.assign({}, DOM.HTMLElement.prototype._eventDefaults)
DOM.HTMLInputElement.prototype._eventDefaults.click = function(event) {
const input = event.target;

Expand Down
4 changes: 2 additions & 2 deletions src/dom_iframe.js → src/dom/iframe.js
@@ -1,8 +1,7 @@
// Support for iframes.


const createHistory = require('./history');
const DOM = require('./dom');
const DOM = require('./index');


// Support for iframes that load content when you set the src attribute.
Expand Down Expand Up @@ -31,6 +30,7 @@ DOM.HTMLFrameElement._init = function() {

// URL created on the fly, or when src attribute set
function createWindow() {
const createHistory = require('../history');
// Need to bypass JSDOM's window/document creation and use ours
const open = createHistory(parentWindow.browser, function(active) {
// Change the focus from window to active.
Expand Down
6 changes: 3 additions & 3 deletions src/dom.js → src/dom/index.js
Expand Up @@ -18,9 +18,9 @@ DOM.exceptionMessages[DOM.TIMEOUT_ERR] = 'Timeout';


// Monkey patching JSDOM. This is why we can't have nice things.
require('./jsdom_patches');
require('./focus');
require('./iframe');
require('./forms');
require('./dom_focus');
require('./dom_iframe');
require('./jsdom_patches');
require('./scripts');

5 changes: 3 additions & 2 deletions src/jsdom_patches.js → src/dom/jsdom_patches.js
@@ -1,7 +1,7 @@
// Fix things that JSDOM doesn't do quite right.


const DOM = require('./dom');
const DOM = require('./index');


DOM.HTMLDocument.prototype.__defineGetter__('scripts', function() {
Expand All @@ -10,7 +10,8 @@ DOM.HTMLDocument.prototype.__defineGetter__('scripts', function() {


// Default behavior for clicking on links: navigate to new URL if specified.
DOM.HTMLAnchorElement.prototype._eventDefaults = {};
DOM.HTMLAnchorElement.prototype._eventDefaults =
Object.assign({}, DOM.HTMLElement.prototype._eventDefaults);
DOM.HTMLAnchorElement.prototype._eventDefaults.click = function(event) {
const anchor = event.target;
if (!anchor.href)
Expand Down
2 changes: 1 addition & 1 deletion src/scripts.js → src/dom/scripts.js
@@ -1,6 +1,6 @@
// For handling JavaScript, mostly improvements to JSDOM

const DOM = require('./dom');
const DOM = require('./index');


// -- Patches to JSDOM --
Expand Down

0 comments on commit 3e8507f

Please sign in to comment.