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

Commit ab6d83b

Browse files
authored
Merge pull request #80 from mobify/explicit-preview
Explicit preview
2 parents f71f7f5 + 2138d48 commit ab6d83b

File tree

4 files changed

+33
-144
lines changed

4 files changed

+33
-144
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
3.0.0
2+
- A **breaking change** to the `.preview()` command to make it more explicit. `.preview(url, bundle, callback)` essentially replaces the role of `site.js`.
13
2.1.0
24
- Adds a `triggerClick` command that uses JavaScript's click.
35
- 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

README.md

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -180,74 +180,19 @@ this.demoTest = function (browser) {
180180
};
181181
```
182182

183-
#### preview(url, callback)
184-
185-
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.
186-
187-
Example site.json
188-
```
189-
{
190-
"activeProfile": "production",
191-
"profiles": {
192-
"default": {
193-
"bundleUrl": "http://localhost:8080/adaptive.js",
194-
"siteUrl": "http://www.merlinspotions.com/"
195-
},
196-
"production": {
197-
"bundleUrl": "",
198-
"siteUrl": "http://www.merlinspotions.com/",
199-
"production": true
200-
}
201-
}
202-
}
203-
```
204-
205-
Example site.js
206-
```
207-
var Site = {
208-
/*
209-
activeProfile defines which environment to run tests against.
210-
By default, builds on master branch run against production, without preview.
211-
Builds on any other branch should use preview with local adaptive.js.
212-
213-
Change activeProfile whenever you need to override the default behaviour.
214-
*/
215-
activeProfile: process.env.ACTIVE_PROFILE || 'default',
216-
217-
/*
218-
Define new profiles as needed for different URLs, eg. staging, prod.
219-
*/
220-
profiles: {
221-
default: {
222-
bundleUrl: 'http://localhost:8080/adaptive.js',
223-
siteUrl: 'http://www.merlinspotions.com/'
224-
},
225-
production: {
226-
bundleUrl: '',
227-
siteUrl: 'http://www.merlinspotions.com',
228-
production: true
229-
}
230-
}
231-
};
232-
233-
module.exports = Site;
183+
#### preview(url, bundle, callback)
234184

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

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

244193
```
245-
this.demoTest = function (browser) {
246-
browser.preview();
247-
};
248-
249-
this.demoTest = function (browser) {
250-
browser.preview('http://my-awesome-project.com');
194+
this.demoTest = function (client) {
195+
browser.preview('https://www.merlinspotions.com', 'https://localhost:8443/loader.js', );
251196
};
252197
```
253198

commands/preview.js

Lines changed: 24 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,48 @@
11
/**
22
* Preview will use preview.mobify.com to open a website and allow you to preview
3-
* a given bundle. The bundle and base URL will need to be set in the the
4-
* `tests/system/site.json` file. Additionally, you can pass a URL as an
5-
* argument when you call preview(). Upon completion, waitUntilMobified
6-
* is called, to be sure that the adaptation is complete.
3+
* a given bundle. The site URL and bundle URL should be passed in. Upon completion, waitUntilMobified is called, to be sure that the
4+
* adaptation is complete.
75
*
8-
* If `site.json` does not exist, this command will just go to the specified URL.
9-
*
10-
* ```
11-
* this.demoTest = function (client) {
12-
* browser.preview();
13-
* };
14-
* ```
15-
* or with a URL
16-
*
6+
* Usage:
177
* ```
188
* this.demoTest = function (client) {
19-
* browser.preview('http://my-awesome-project.com');
9+
* browser.preview('https://www.merlinspotions.com', 'https://localhost:8443/loader.js');
2010
* };
2111
* ```
2212
*
23-
* @method attributeEquals
24-
* @param {string} [URL] (optional) The URL to be previewed.
25-
* @param {function} callback The function to be called on completion.
26-
* @api assertions
13+
* @method preview
14+
* @param {string} [url] Corresponds to the Site URL field on https://preview.mobify.com
15+
* @param {string} [bundle] Corresponds to the Bundle Location field on https://preview.mobify.com
16+
* @param {function} [callback] Optional callback function to be called when the command finishes.
17+
* @api commands
2718
*/
2819

29-
var path = require('path');
3020
var qs = require('querystring');
3121

32-
exports.command = function(url, callback) {
22+
exports.command = function(url, bundle, callback) {
3323
var browser = this;
3424

35-
try {
36-
var siteConfig = require(path.join(path.resolve('./'), '/tests/system/site.json'));
37-
} catch (e) {
38-
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
39-
console.log('Not using optional site.json. Looking for site.js...');
40-
}
41-
}
42-
43-
try {
44-
var siteConfig = require(path.join(path.resolve('./'), '/tests/system/site.js'));
45-
} catch (e) {
46-
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
47-
console.log('Not using optional /tests/system/site.js.');
48-
}
25+
if (arguments.length < 2) {
26+
throw new Error('Usage: browser.preview(url, bundle, callback)');
4927
}
5028

51-
try {
52-
var siteConfig = require(path.join(path.resolve('./'), '/system/site.js'));
53-
} catch (e) {
54-
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
55-
console.log('Not using optional /system/site.js.');
56-
}
29+
if (typeof bundle === 'function') {
30+
callback = bundle;
31+
bundle = null;
5732
}
5833

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

62-
if (typeof url === 'function') {
63-
callback = url;
64-
url = site.siteUrl;
65-
}
36+
var params = qs.stringify({'url': url, 'site_folder': bundleUrl});
6637

67-
// First checks for the URL, otherwise uses the site.siteURL, then makes sure
68-
// that there is an http prefix. The preview function doesn't need this, but
69-
// the browser.get() method does.
70-
url = url || site.siteUrl;
71-
72-
if (!url.match(/^http/)) {
73-
throw new Error('Site URL must be correctly formatted');
74-
}
75-
76-
// If the production flag is set, just runs a `get()` on the URL.
77-
if (site.production) {
78-
return browser.get(url, function(result) {
79-
if (typeof callback === 'function') {
80-
callback.call(browser, result);
81-
}
82-
});
83-
}
84-
85-
var bundleUrl = site.bundleUrl || 'https://localhost:8443/adaptive.js';
86-
87-
var params = qs.stringify({'url': url, 'site_folder': bundleUrl});
88-
89-
return browser.url('https://preview.mobify.com?' + params)
90-
.waitForElementPresent('#authorize', 10000, function() {
91-
this.click('#authorize', function() {
92-
browser.waitUntilMobified(10000, function(result) {
93-
if (typeof callback === 'function') {
94-
callback.call(browser, result);
95-
}
96-
});
38+
return browser.url('https://preview.mobify.com?' + params)
39+
.waitForElementPresent('#authorize', 10000, function() {
40+
this.click('#authorize', function() {
41+
browser.waitUntilMobified(10000, function(result) {
42+
if (typeof callback === 'function') {
43+
callback.call(browser, result);
44+
}
9745
});
9846
});
99-
} else {
100-
return browser.url(url, function(result) {
101-
if (typeof callback === 'function') {
102-
callback.call(browser, result);
103-
}
10447
});
105-
}
10648
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nightwatch-commands",
3-
"version": "2.1.0",
3+
"version": "3.0.0",
44
"description": "A set of Mobify specific custom commands for Nightwatch.js",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)