Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
apendua committed Feb 2, 2015
2 parents 2c86246 + b1c60b2 commit 1eed4a0
Show file tree
Hide file tree
Showing 44 changed files with 1,275 additions and 1,239 deletions.
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
anti:[email protected].0
anti:[email protected].1
[email protected]
[email protected]
[email protected]
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
![gagarin](https://s3.amazonaws.com/gagarinjs/assets/gagarinLogo.svg)

# Important note

Since version `0.4.0` the `server` object created by `meteor()` helper no longer has the `location` property. To make sure the `browser` starts on the proper location, you need to pass `server` as the first argument, so
```javascript
var server = meteor();
var client = browser(server); // before 0.4.0 you would use server.location here
```

# gagarin [![Circle CI](https://circleci.com/gh/anticoders/gagarin/tree/devel.svg?style=svg)](https://circleci.com/gh/anticoders/gagarin/tree/devel)

Gagarin is a tool you can use in your tests to run Meteor apps in a sandboxed environment. It's useful when you need more refined control over the meteor processes and test fancy things, e.g. the behavior of your app on server restarts or when you have multiple app instances writing to the same database. This is currently not achievable with the official Meteor testing framework.
Expand Down Expand Up @@ -76,7 +84,7 @@ A test suite using both server and client may look like this:
```javascript
describe('You can also use browser in your tests', function () {
var server = meteor();
var client = browser(server.location + "/path/to/some/view")
var client = browser(server);

it('should just work', function () {
return client.execute(function () {
Expand Down
2 changes: 1 addition & 1 deletion bin/gagarin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

var program = require('commander');
var Gagarin = require('../lib/gagarin');
var Gagarin = require('../lib/mocha/gagarin');
var path = require('path');
var fs = require('fs');

Expand Down
2 changes: 1 addition & 1 deletion find
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
grep -R $1 lib/*.js tests/specs/*.js
grep -R $1 lib/*/*.js tests/specs/*.js
185 changes: 0 additions & 185 deletions lib/browser.js

This file was deleted.

138 changes: 138 additions & 0 deletions lib/browser/browserManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@

var portscanner = require('portscanner');
var Promise = require('es6-promise').Promise;
var either = require('../tools').either;
var url = require('url');
var wd = require('wd');

module.exports = function createBrowserManager (options) {
"use strict";

var driverLocation = options.webdriver || "http://localhost:9515"; // chromedriver default
var browser = wd.remote(driverLocation);
var dontWaitForMeteor = options.dontWaitForMeteor !== undefined ? !!options.dontWaitForMeteor : false;
var meteorLoadTimeout = options.meteorLoadTimeout !== undefined ? options.meteorLoadTimeout : 2000;
var capabilities = options.capabilities || {};
var windowSize = options.windowSize;
var myLocation = options.location || "http://localhost:3000";
var browserPromise = null;
var getDDPSetup = typeof myLocation === 'string' ? Promise.resolve(myLocation) : myLocation.getDDPSetup;

if (!getDDPSetup) {
throw new Error('the location option must be either string or a meteor server');
}

return function getBrowser () {

var isInitialized = false;

if (browserPromise) {
return browserPromise;
}

browserPromise = new Promise(function (resolve, reject) {

function _reject (err) {
if (isInitialized) {
browser.quit();
}
if (typeof err === 'string') {
return reject(new Error(err));
}
reject(err);
}

var driverLocationParsed = url.parse(driverLocation);

portscanner.checkPortStatus(driverLocationParsed.port, driverLocationParsed.hostname, function (err, status) {
if (err || status !== 'open') {
return _reject(err || 'webdriver not found on ' + driverLocation);
}

// NOTE: it was not easy to find in the docs, so I am leaving it here as a comment
// capabilities.loggingPrefs = { "driver": "INFO", "browser": "INFO" };

browser.init(capabilities, either(_reject).or(function () {

isInitialized = true;

if (windowSize) {
browser.setWindowSize(windowSize.width, windowSize.height, either(_reject).or(function () {
afterResize(either(_reject).or(resolve));
}));
} else {
afterResize(either(_reject).or(resolve));
}
}));
});
});

return browserPromise;
};

function afterResize (done) {
if (getDDPSetup) {
getDDPSetup().then(function (setup) {
if (setup.host) {
getLocation(setup.host, done);
} else {
getLocation('http://localhost:' + setup.port, done);
}
}).catch(done);
} else {
done(null, browser);
}
}

function getLocation (url, done) {
browser.get(url, function (err) {
if (err) {
return done(err);
}

if (dontWaitForMeteor) { // already done, so lets just resolve
return done(null, browser);
}

browser.setAsyncScriptTimeout(meteorLoadTimeout, function (err) {
// XXX asyncScriptTimeout is only relevant in "onStartup" routine
if (err) {
return done(err);
}

var handle1 = null;
var handle2 = null;

handle2 = setTimeout(function () {
clearTimeout(handle1);
done(new Error('Meteor code is still not loaded after ' + meteorLoadTimeout + ' ms'));
}, meteorLoadTimeout);

// wait until all meteor-related files are loaded
(function test() {
browser.execute("return !!window.Meteor && !!window.Meteor.startup", function (err, isOK) {
if (err) {
clearTimeout(handle2);
return done(err);
}
if (isOK) {
onStartup(function (err) {
clearTimeout(handle2);
done(err, !err && browser);
});
} else {
handle1 = setTimeout(test, 100);
}
});
})();
});
});
}

function onStartup (done) {
browser.executeAsync(function (cb) {
Meteor.startup(cb);
}, done);
}

}
Loading

0 comments on commit 1eed4a0

Please sign in to comment.