Skip to content
This repository has been archived by the owner on Aug 5, 2020. It is now read-only.

Commit

Permalink
Merge pull request #38 from mobify/optional-site-json
Browse files Browse the repository at this point in the history
Make site.json optional
  • Loading branch information
ellenmobify committed Nov 30, 2015
2 parents c566302 + f82e064 commit 8a6982e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
85 changes: 50 additions & 35 deletions commands/preview.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* Preview will use preview.mobify.com to open a website and allow you to preview
* a given bundle. The bundle and base URL will need to be set in the the
* `tests/integration/site.json` file. Additionally, you can pass a URL as an
* `tests/system/site.json` file. Additionally, you can pass a URL as an
* argument when you call preview(). Upon completion, waitUntilMobified
* is called, to be sure that the adaptation is complete.
*
* If `site.json` does not exist, this command will just go to the specified URL.
*
* ```
* this.demoTest = function (client) {
Expand All @@ -24,52 +26,65 @@
* @api assertions
*/


var path = require('path');
var site = require(path.join(path.resolve('./'), '/tests/system/site.json'));
var qs = require('querystring');

site = site.profiles[site.activeProfile];
try {
var site = require(path.join(path.resolve('./'), '/tests/system/site.json'));
} catch (e) {
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
console.log('Not using optional site.json...');
}
}
var qs = require('querystring');

exports.command = function(url, callback) {
var browser = this;

if (typeof url === 'function') {
callback = url;
url = site.siteUrl;
}
if (site) {
site = site.profiles[site.activeProfile];

// First checks for the URL, otherwise uses the site.siteURL, then makes sure
// that there is an http prefix. The preveiw function doesn't need this, but
// the browser.get() method does.
url = url || site.siteUrl;
if (typeof url === 'function') {
callback = url;
url = site.siteUrl;
}

if (!url.match(/^http/)) {
throw new Error('Site URL must be correctly formatted');
}
// First checks for the URL, otherwise uses the site.siteURL, then makes sure
// that there is an http prefix. The preview function doesn't need this, but
// the browser.get() method does.
url = url || site.siteUrl;

// If the production flag is set, just runs a `get()` on the URL.
if (site.production) {
return browser.get(url, function(result) {
if (typeof callback === 'function') {
callback.call(browser, result);
}
});
}
if (!url.match(/^http/)) {
throw new Error('Site URL must be correctly formatted');
}

var bundleUrl = site.bundleUrl || 'http://localhost:8080';
// If the production flag is set, just runs a `get()` on the URL.
if (site.production) {
return browser.get(url, function(result) {
if (typeof callback === 'function') {
callback.call(browser, result);
}
});
}

var params = qs.stringify({'url': url, 'site_folder': bundleUrl});
var bundleUrl = site.bundleUrl || 'https://localhost:8443/adaptive.js';

return browser.url('https://preview.mobify.com?' + params)
.waitForElementPresent('#authorize', 10000, function() {
this.click('#authorize', function() {
browser.waitUntilMobified(10000, function(result) {
if (typeof callback === 'function') {
callback.call(browser, result);
}
var params = qs.stringify({'url': url, 'site_folder': bundleUrl});

return browser.url('https://preview.mobify.com?' + params)
.waitForElementPresent('#authorize', 10000, function() {
this.click('#authorize', function() {
browser.waitUntilMobified(10000, function(result) {
if (typeof callback === 'function') {
callback.call(browser, result);
}
});
});
});
} else {
return browser.url(url, function(result) {
if (typeof callback === 'function') {
callback.call(browser, result);
}
});

};
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nightwatch-commands",
"version": "1.3.0",
"version": "1.3.1",
"description": "A set of Mobify specific custom commands for Nightwatch.js",
"repository": {
"type": "git",
Expand Down

0 comments on commit 8a6982e

Please sign in to comment.