Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
natritmeyer committed Sep 25, 2012
0 parents commit 9e9027d
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
29 changes: 29 additions & 0 deletions LICENSE
@@ -0,0 +1,29 @@
Copyright (c) 2012, Nathaniel Ritmeyer
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name Nathaniel Ritmeyer nor the names of contributors to
this software may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

104 changes: 104 additions & 0 deletions README.md
@@ -0,0 +1,104 @@
# Test Automation Firefox Password Manager

A very basic Firefox plugin that allows updating the Password Manager at
runtime using Javascript. When used in concert with the AuthoAuth
plugin (https://addons.mozilla.org/en-US/firefox/addon/autoauth/ ),
ActiveDirectory authentication dialogs will no longer cause your
tests to fail.

## Why it's awesome

Until this plugin arrived, it has been impossible to dynamically add
passwords to Firefox's Password Manager at runtime. This prevents using
Firefox for testing websites that authenticate against ActiveDirectory.
There are work arounds (as documented here: http://watirmelon.com/2012/06/27/automatic-firefox-authentication-when-using-selenium-webdriver-with-autoauth/ ) but you are then stuck with the burden of having to maintain
a Firefox profile. Now, instead, you can go back to using dynamically
created profiles at runtime and by calling a simple javascript function
you can add as many host/username/password combinations as you like.

## Why it's rubbish

Javascript can only be injected and executed into a page that has already
been loaded. This is frustrating because you can only update the Password
Manager once the first page has loaded. The implications of this are
that you need to first navigate to a page that doesn't present a
password prompt, update the Password Manager from that page and only
then should you navigate to the page that presents the password prompt.
I'd love for someone to replace this plugin with something that doesn't
require this extra step.

## How to use it

(NB: the following examples demonstrate how to use this plugin
with capybara and ruby but the principles should carry across to
anything else that can inject javascript into Firefox at runtime)

### Download the plugins

First, you'll need to download the test-automation-password-manager.xpi
file from this repo, and you'll also need to download the latest
AutoAuth plugin file (autoauth-2.1-fx+fn.xpi at time of writing) from
https://addons.mozilla.org/en-US/firefox/addon/autoauth/ . Add both of
them to a directory in your test project called `ff_plugins`.

### Add the plugins to a dynamically created Firefox profile

When you create your Firefox Profile at runtime, you'll need to add this
plugin and the AutoAuth plugin:

```ruby
Capybara.register_driver :selenium_firefox do |app|
#create the profile:
profile = Selenium::WebDriver::Firefox::Profile.new
#add the 2 plugins
profile.add_extension(File.expand_path("ff_plugins/autoauth-2.1-fx+fn.xpi"))
profile.add_extension(File.expand_path("ff_plugins/test-automation-password-manager.xpi"))

Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
```

### Update Password Manager

The following code shows how to add details to the Password Manager at
runtime:

```ruby
#navigate to a page that won't prompt you for a password:

visit 'http://www.google.com'

#create the javascript that will send the relevant details to the password manager:

pass_man_update_script = <<-SCRIPT
var addCredentialsEvent = new CustomEvent("add_credentials_to_passman", {
detail: {
host: 'http://secure.example.com',
username: 'bob',
password: 'P45sword'
}
});
window.dispatchEvent(addCredentialsEvent);
SCRIPT

#inject the script into the browser:

page.execute_script pass_man_update_script

#navigate to the page that prompts you for a password:

visit 'http://super_secure.example.com'
#This used to present a password dialog that would cause the test to
#fail, but now it doesn't! Woo!
```

## Disclaimer

* I am not a javascript dev
* I am not a Firefox Addon dev
* I only tested this against my current set up as I don't have anything
else to test it against

Please feel free to send me pull requests, or even better, make a better
version of this plugin. The sooner I can retire this, the happier I'll be.

4 changes: 4 additions & 0 deletions data/content_script.js
@@ -0,0 +1,4 @@
window.addEventListener('add_credentials_to_passman', function(event) {
self.port.emit('add_credentials_to_passman', event.detail);
}, false);

Empty file added doc/main.md
Empty file.
43 changes: 43 additions & 0 deletions lib/main.js
@@ -0,0 +1,43 @@
const passwords = require("passwords");
const prefs = require("preferences-service");
const urls = require("url");
const data = require("self").data;

var test_automation_password_manager = {
add_credentials : function(url, username, password) {
passwords.store({
url: url,
realm: url,
username: username,
password: password,
onError: function onError(error) {
if(error.message.indexOf("This login already exists") == -1) {
console.log(error.message)
}
}
});
var url_host = urls.URL(url).host;
this.add_trusted_uri(url_host);
},

add_trusted_uri : function(trusted_uri) {
var trusted_uris_prefs_name = "network.automatic-ntlm-auth.trusted-uris";
var original_trusted_uris = "";
if(prefs.isSet(trusted_uris_prefs_name)) {
original_trusted_uris = prefs.get(trusted_uris_prefs_name) + ",";
}
var trusted_uris = original_trusted_uris + trusted_uri;
prefs.set(trusted_uris_prefs_name, trusted_uris);
},
};

var pageMod = require('page-mod').PageMod({
include: ['*'],
contentScriptFile: data.url("content_script.js"),
onAttach: function(worker) {
worker.port.on('add_credentials_to_passman', function(message) {
test_automation_password_manager.add_credentials(message.host, message.username, message.password);
});
}
});

10 changes: 10 additions & 0 deletions package.json
@@ -0,0 +1,10 @@
{
"name": "test-automation-password-manager",
"license": "BSD",
"author": "Nat Ritmeyer",
"version": "0.1",
"fullName": "test-automation-password-manager",
"id": "jid1-RBSCJoXkcndNCw",
"description": "Inject credentials into the password manager at runtime using JS"
}

8 changes: 8 additions & 0 deletions package.json.backup
@@ -0,0 +1,8 @@
{
"name": "test-automation-password-manager",
"fullName": "test-automation-password-manager",
"description": "a basic add-on",
"author": "",
"license": "MPL 2.0",
"version": "0.1"
}
Binary file added test-automation-password-manager.xpi
Binary file not shown.
6 changes: 6 additions & 0 deletions test/test-main.js
@@ -0,0 +1,6 @@
var main = require("main");

exports.test_run = function(test) {
test.pass("Unit test running!");
};

0 comments on commit 9e9027d

Please sign in to comment.