Skip to content

Commit

Permalink
Add offline support and bugfixes.
Browse files Browse the repository at this point in the history
- Bugfixes
- Add executable for offline support via Node.JS
- Restructure common and browser specific files
- Update dependencies
- Restructure user and devel dependencies
- Adjust test cases
- Add DOM treewalker polyfill
- Adjust documentation
  • Loading branch information
rickypc committed Jan 19, 2016
1 parent 945999c commit 3e4ac21
Show file tree
Hide file tree
Showing 15 changed files with 1,391 additions and 568 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
### 1.2.6 (2016.01.19)

* Bugfixes
* Add executable for offline support via Node.JS
* Restructure the files between common and browser specific functionalities
* Update dependencies
* Adjust the tests
* Add DOM tree walker polyfill

### 1.2.5 (2015.09.22)

* Bugfixes
Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ A nimble and flexible [Selenium Page Object Model](https://code.google.com/p/sel

Selenium Page Object Generator is an essential tool to improve your workflow. It will generate [Page Object Model](http://martinfowler.com/bliki/PageObject.html) on active [Chrome](https://www.google.com/chrome/browser/desktop/index.html) or [Opera](http://www.opera.com/) tab with a single click, provided that all of the options and template are configured. The generated Page Object Model will be saved to pre-configured Chrome or Opera download folder.

Selenium Page Object Generator is also available in command line by installing [Node.JS](https://nodejs.org/en/) package from [NPM Registry](https://www.npmjs.com/package/selenium-page-object-generator).

The template is using [Handlebars.js](http://handlebarsjs.com/) expression, a clean logic-less semantic templating language.

This is an early BETA release, it expected to have rough edges, and limited functionality. It currently support 3 different targets: [Java](https://en.wikipedia.org/wiki/Java_(programming_language)), [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)), and [Robot Framework](http://robotframework.org/).
Expand All @@ -21,13 +23,41 @@ C#: [http://relevantcodes.com/pageobjects-and-pagefactory-design-patterns-in-sel

Robot Framework: [http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#taking-resource-files-into-use](http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#taking-resource-files-into-use)

(You need to use Chrome 40+ or Opera 15+ to try this out)
(You need to use Chrome 40+ or Opera 15+ or Node.JS 5.x to try this out)

Installation
-

- To install the newest released version on Chrome browser, please download it from [Chrome web store](https://chrome.google.com/webstore/detail/epgmnmcjdhapiojbohkkemlfkegmbebb).
- To install the newest released version on Opera browser, please download it from [Opera addons catalog](https://addons.opera.com/en/extensions/details/selenium-page-object-generator).
- To install the newest released version on command line, please install [Node.js](https://nodejs.org/) and run command below:

```bash
$ npm install selenium-page-object-generator
```

Command Line Usage
-
Selenium Page Object Generator accepts the following command line arguments:

```bash
selenium-page-object-generator [options]

-h, --help Show this help message and exit.
-v, --version Show program's version number and exit.
-t, --target {cs,java,robot} Generator target
-n, --name [PageName] Page name (no-spaces)
-d, --destination [DestinationPageName] Destination page name (no-spaces) (optional)
-s, --source [source.html] Source file
```
Examples
-
To generate Java page object:
```bash
$ node_modules/.bin/selenium-page-object-generator -t java -n MySite -s source.html
```
Development Dependencies
-
Expand Down
103 changes: 103 additions & 0 deletions bin/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env node
'use strict';

var ArgumentParser = require('argparse').ArgumentParser;
var path = require('path');
var rootDir = path.join(__dirname, '..');
var packagejson = require(path.join(rootDir, 'package.json'));
var parser = new ArgumentParser({
description: packagejson.description,
version: packagejson.version
});

parser.addArgument(['-t', '--target'], {
choices: ['cs', 'java', 'robot'],
help: 'Generator target',
required: true
});
parser.addArgument(['-n', '--name'], {
help: 'Page name (no spaces)',
required: true
});
parser.addArgument(['-d', '--destination'], {
help: 'Destination page name (no spaces)',
required: false
});
parser.addArgument(['-s', '--source'], {
help: 'Source file',
required: true
});

var args = parser.parseArgs();
var execDir = process.cwd();
var fs = require('fs');
var jsdom = require('jsdom');
var mkdirp = require('mkdirp');
var commonDir = path.join(rootDir, 'src', 'common');
var common = require(path.join(commonDir, 'common.js'));
GLOBAL.Handlebars = require(path.join(rootDir, 'libs', 'handlebars-v3.0.3.js'));
require(path.join(commonDir, 'helpers.js'));

var overrides = {
model: {
name: args.name.replace(/\s+/g, ''),
target: args.destination
}
};

var paths = {
config: path.join(rootDir, 'configs', args.target + '.json'),
source: path.join(execDir, args.source),
target: path.join(execDir, args.name + '.' + args.target),
template: path.join(rootDir, 'templates', args.target + '.handlebars')
};

var targets = {
cs: { label: 'C#' },
java: { label: 'Java' },
robot: { label: 'Robot Framework' }
};

function getFileContent(path) {
var response = '';
try {
response = fs.readFileSync(path, 'utf-8');
}
catch (ex) {
if (ex.code === 'ENOENT') {
console.error(path + ' does not exist.');
}
throw ex;
}
return response;
}

jsdom.env({
file: paths.source,
scripts: [path.join(rootDir, 'libs', 'treewalker-polyfill-0.2.0.js'),
path.join(commonDir, 'common.js'),
path.join(commonDir, 'generator.js')],
done: function (err, window) {
try {
jsdom.getVirtualConsole(window).sendTo(console);
var specs = targets[args.target];
var config = require(paths.config);
config = window.common.setDefaultValues(config);
overrides.model.include = config.model.include;
overrides.model.namespace = config.model.namespace;
var input = Object.extend({}, config, overrides);
var output = window.POG.generate(input);
var template = getFileContent(paths.template);
var generated = (Handlebars.compile(template))(output);
mkdirp.sync(path.dirname(paths.target));
fs.writeFileSync(paths.target, generated);
console.log('The file is saved: ' + paths.target);
}
catch (ex) {
throw ex;
}
finally {
window.close();
}
}
});
Loading

0 comments on commit 3e4ac21

Please sign in to comment.