Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for explicit user agents #20

Merged
merged 1 commit into from
Nov 22, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bin/mocha-phantomjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var program = require('commander'),
program
.version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
.usage('[options] page')
.option('-R, --reporter <name>', 'specify the reporter to use', 'spec');
.option('-R, --reporter <name>', 'specify the reporter to use', 'spec')
.option('-A, --agent <userAgent>', 'specify the user agent to use', '');

program.on('--help', function(){
console.log(' Examples:');
Expand All @@ -27,6 +28,7 @@ if (!program.args.length) { program.outputHelp(); process.exit(1); };

var script = fs.realpathSync(__dirname + '/../lib/mocha-phantomjs.coffee');
var reporter = program.reporter;
var agent = program.agent;
var page = function(){
var arg = program.args[0];
if (arg.match(/file:\/\//)) { return arg; };
Expand All @@ -37,7 +39,7 @@ var page = function(){
return arg;
}();

var phantomjs = spawn('phantomjs', [script, page, reporter]);
var phantomjs = spawn('phantomjs', [script, page, reporter, agent]);

phantomjs.stdout.on('data', function(data){
print(data.toString());
Expand Down
11 changes: 9 additions & 2 deletions lib/mocha-phantomjs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ system = require 'system'
webpage = require 'webpage'

USAGE = """
Usage: phantomjs mocha-phantomjs.coffee URL REPORTER
Usage: phantomjs mocha-phantomjs.coffee URL REPORTER [USER-AGENT]
"""

class Reporter
Expand Down Expand Up @@ -42,7 +42,12 @@ class Reporter
phantom.exit @page.evaluate -> mochaPhantomJS.failures

initPage: ->
@page = webpage.create()
if userAgent
settings =
userAgent: userAgent

@page = webpage.create
settings: settings
@page.onConsoleMessage = (msg) -> console.log msg
@page.onInitialized = =>
@page.evaluate ->
Expand Down Expand Up @@ -214,6 +219,8 @@ class HtmlCov extends Reporter
constructor: -> super 'html-cov'


userAgent = system.args[3]

reporterString = system.args[2] || 'spec'
reporterString = ("#{s.charAt(0).toUpperCase()}#{s.slice(1)}" for s in reporterString.split('-')).join('')
reporterKlass = try
Expand Down