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 #80 from mobify/explicit-preview
Browse files Browse the repository at this point in the history
Explicit preview
  • Loading branch information
ellenmobify authored Jul 14, 2017
2 parents f71f7f5 + 2138d48 commit ab6d83b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 144 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
3.0.0
- A **breaking change** to the `.preview()` command to make it more explicit. `.preview(url, bundle, callback)` essentially replaces the role of `site.js`.
2.1.0
- Adds a `triggerClick` command that uses JavaScript's click.
- Renames `navigate` to `clickAndWaitUntilMobified` to avoid colliding with a core Nightwatch command with the same name. Resolves https://github.com/mobify/nightwatch-commands/issues/74
Expand Down
67 changes: 6 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,74 +180,19 @@ this.demoTest = function (browser) {
};
```

#### preview(url, callback)

The `preview` command uses http://preview.mobify.com to open a website to preview a given bundle. The bundle and the base URL need to be set in the `tests/system/site.json`, `tests/system/site.js` or `system/site.js` file. Note that if the "production" flag is set in the `activeProfile` in `site.json` or `site.js`, the bundle URL will be ignored. Pass in an optional URL as an argument to this command. Upon completion, `waitUntilMobified` is called to ensure that the mobile site adaptation is complete.

Example site.json
```
{
"activeProfile": "production",
"profiles": {
"default": {
"bundleUrl": "http://localhost:8080/adaptive.js",
"siteUrl": "http://www.merlinspotions.com/"
},
"production": {
"bundleUrl": "",
"siteUrl": "http://www.merlinspotions.com/",
"production": true
}
}
}
```

Example site.js
```
var Site = {
/*
activeProfile defines which environment to run tests against.
By default, builds on master branch run against production, without preview.
Builds on any other branch should use preview with local adaptive.js.
Change activeProfile whenever you need to override the default behaviour.
*/
activeProfile: process.env.ACTIVE_PROFILE || 'default',
/*
Define new profiles as needed for different URLs, eg. staging, prod.
*/
profiles: {
default: {
bundleUrl: 'http://localhost:8080/adaptive.js',
siteUrl: 'http://www.merlinspotions.com/'
},
production: {
bundleUrl: '',
siteUrl: 'http://www.merlinspotions.com',
production: true
}
}
};
module.exports = Site;
#### preview(url, bundle, callback)

```

If the project does not have a `site.json` or `site.js` file, this command is equivalent to the `url` protocol command.
The `preview` command uses http://preview.mobify.com to open a website specified by `url` to preview a given bundle specified by `bundle`.

Parameter Name | Parameter Type | Description
------------- | -------------- | -----------
url | String | _optional_ The URL to preview.
url | String | The URL to preview, equivalent to the Site URL field.
bundle | String | _optional_ The bundle URL, equivalent to the Bundle Location field. Default is `https://localhost:8443/loader.js`
callback | Function | _optional_ A function to call after the current command finishes execution.

```
this.demoTest = function (browser) {
browser.preview();
};
this.demoTest = function (browser) {
browser.preview('http://my-awesome-project.com');
this.demoTest = function (client) {
browser.preview('https://www.merlinspotions.com', 'https://localhost:8443/loader.js', );
};
```

Expand Down
106 changes: 24 additions & 82 deletions commands/preview.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,48 @@
/**
* 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/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.
* a given bundle. The site URL and bundle URL should be passed in. 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) {
* browser.preview();
* };
* ```
* or with a URL
*
* Usage:
* ```
* this.demoTest = function (client) {
* browser.preview('http://my-awesome-project.com');
* browser.preview('https://www.merlinspotions.com', 'https://localhost:8443/loader.js');
* };
* ```
*
* @method attributeEquals
* @param {string} [URL] (optional) The URL to be previewed.
* @param {function} callback The function to be called on completion.
* @api assertions
* @method preview
* @param {string} [url] Corresponds to the Site URL field on https://preview.mobify.com
* @param {string} [bundle] Corresponds to the Bundle Location field on https://preview.mobify.com
* @param {function} [callback] Optional callback function to be called when the command finishes.
* @api commands
*/

var path = require('path');
var qs = require('querystring');

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

try {
var siteConfig = 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. Looking for site.js...');
}
}

try {
var siteConfig = require(path.join(path.resolve('./'), '/tests/system/site.js'));
} catch (e) {
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
console.log('Not using optional /tests/system/site.js.');
}
if (arguments.length < 2) {
throw new Error('Usage: browser.preview(url, bundle, callback)');
}

try {
var siteConfig = require(path.join(path.resolve('./'), '/system/site.js'));
} catch (e) {
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
console.log('Not using optional /system/site.js.');
}
if (typeof bundle === 'function') {
callback = bundle;
bundle = null;
}

if (siteConfig) {
var site = siteConfig.profiles[siteConfig.activeProfile];
var bundleUrl = bundle || 'https://localhost:8443/loader.js';

if (typeof url === 'function') {
callback = url;
url = site.siteUrl;
}
var params = qs.stringify({'url': url, 'site_folder': bundleUrl});

// 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 (!url.match(/^http/)) {
throw new Error('Site URL must be correctly formatted');
}

// 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 bundleUrl = site.bundleUrl || 'https://localhost:8443/adaptive.js';

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);
}
});
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": "2.1.0",
"version": "3.0.0",
"description": "A set of Mobify specific custom commands for Nightwatch.js",
"repository": {
"type": "git",
Expand Down

0 comments on commit ab6d83b

Please sign in to comment.