Skip to content

Commit

Permalink
Add a simple extension bar to the SafariDriver. The bar will be used to
Browse files Browse the repository at this point in the history
display banner messages to the user (e.g. the currently executing
command)
  • Loading branch information
jleyba committed Apr 16, 2013
1 parent 0b93de0 commit e6df7ca
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 4 deletions.
11 changes: 11 additions & 0 deletions javascript/safari-driver/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
<string>2.32.0</string>
<key>Chrome</key>
<dict>
<key>Bars</key>
<array>
<dict>
<key>Filename</key>
<string>extension_bar.html</string>
<key>Identifier</key>
<string>safaridriver.extension.bar</string>
<key>Label</key>
<string>WebDriver</string>
</dict>
</array>
<key>Global Page</key>
<string>extension.html</string>
</dict>
Expand Down
4 changes: 3 additions & 1 deletion javascript/safari-driver/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ js_binary(
"deps.js",
"extension/commands.js",
"extension/extension.js",
"extension/extensionbar.js",
"extension/server.js",
"extension/session.js",
"extension/tab.js",
Expand Down Expand Up @@ -235,7 +236,8 @@ folder(
name = "SafariDriver",
srcs = [
"Info.plist",
"extension/extension.html"
"extension/extension.html",
"extension/extension_bar.html",
],
deps = [
":extension",
Expand Down
2 changes: 2 additions & 0 deletions javascript/safari-driver/extension/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ goog.require('safaridriver.console');
goog.require('safaridriver.extension.Server');
goog.require('safaridriver.extension.Session');
goog.require('safaridriver.extension.TabManager');
goog.require('safaridriver.extension.bar');
goog.require('safaridriver.message');
goog.require('safaridriver.message.Alert');
goog.require('safaridriver.message.Connect');
Expand Down Expand Up @@ -57,6 +58,7 @@ safaridriver.extension.init = function() {
// Now that we're initialized, we sit and wait for a page to send us a client
// to attempt connecting to.
safaridriver.extension.LOG_.info('Waiting for connect command...');
safaridriver.extension.bar.setUserMessage('<idle>');
safari.application.addEventListener('message',
safaridriver.extension.onMessage_, false);
};
Expand Down
3 changes: 3 additions & 0 deletions javascript/safari-driver/extension/extension_bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!DOCTYPE html>
WebDriver |<span style="margin-left: 10px; font-style: italic"
id="message">&lt;idle&gt;</span>
43 changes: 43 additions & 0 deletions javascript/safari-driver/extension/extensionbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2013 Selenium committers
// Copyright 2013 Software Freedom Conservancy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview Utilities for working with the browser window extension bar.
*/

goog.provide('safaridriver.extension.bar');


/**
* Sets the message to display to the user in the extension bar.
* @param {string} message The message text.
* @param {string=} opt_color The message color; defaults to black.
*/
safaridriver.extension.bar.setUserMessage = function(message, opt_color) {
var color = opt_color || 'black';
if (message.length > 75) {
message = message.substring(0, 75) + '...';
}

var bars = safari.extension.bars;
for (var i = 0, n = bars.length; i < n; ++i) {
var bar = bars[i];
var msgEl = bar.contentWindow.document.getElementById('message');
if (msgEl) {
msgEl.innerText = message;
msgEl.style.color = color;
}
}
};
11 changes: 9 additions & 2 deletions javascript/safari-driver/extension/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ goog.require('goog.string');
goog.require('safaridriver.Command');
goog.require('safaridriver.alert');
goog.require('safaridriver.extension.commands');
goog.require('safaridriver.extension.bar');
goog.require('safaridriver.message.Command');
goog.require('safaridriver.message.Response');
goog.require('webdriver.CommandName');
Expand Down Expand Up @@ -269,6 +270,7 @@ safaridriver.extension.Server.prototype.attemptConnect_ = function(url) {
safaridriver.extension.Server.connectedUrls_[url] = true;

this.logMessage_('Attempting to connect to ' + url);
safaridriver.extension.bar.setUserMessage('Connecting to ' + url);

// Register the event handlers. Note that it is not possible for these
// callbacks to be missed because it is registered after the web socket is
Expand Down Expand Up @@ -316,6 +318,7 @@ safaridriver.extension.Server.prototype.execute = function(
var result = flow.execute(fn, description).
then(bot.response.createResponse, bot.response.createErrorResponse).
addBoth(function(response) {
safaridriver.extension.bar.setUserMessage('<idle>');
this.session_.setCurrentCommand(null);
return response;
}, this);
Expand All @@ -341,6 +344,8 @@ safaridriver.extension.Server.prototype.execute = function(
safaridriver.extension.Server.prototype.executeCommand_ = function(
command, handler) {
this.logMessage_('Executing command: ' + command.getName());
safaridriver.extension.bar.setUserMessage(
command.getName() + ' ' + JSON.stringify(command.getParameters()));

var alertText = this.session_.getUnhandledAlertText();
if (!goog.isNull(alertText)) {
Expand Down Expand Up @@ -391,9 +396,11 @@ safaridriver.extension.Server.prototype.onClose_ = function(url) {
goog.debug.Logger.Level.WARNING);
if (!this.isDisposed()) {
if (this.ready_.isPending()) {
this.logMessage_('Failed to connect to ' + url);
var message = 'Failed to connect to ' + url;
this.logMessage_(message);
safaridriver.extension.bar.setUserMessage(message, 'red');
this.disposeWebSocket_();
setTimeout(goog.bind(this.attemptConnect_, this, url), 250);
setTimeout(goog.bind(this.attemptConnect_, this, url), 500);
} else {
this.dispose();
}
Expand Down
29 changes: 28 additions & 1 deletion javascript/safari-driver/externs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,34 @@ SafariEventTarget.prototype.removeEventListener = function(type, listener,

/** @constructor */
function SafariExtension() {}
// TODO: finish documenting the SafariExtension type.

/** @type {!Array.<!SafariExtensionBar>} */
SafariExtension.prototype.bars;


/**
* @constructor
* @extends {SafariEventTarget}
*/
function SafariExtensionBar() {}

/** @type {boolean} */
SafariExtensionBar.prototype.visible;

/** @type {!SafariBrowserWindow} */
SafariExtensionBar.prototype.browserWindow;

/** @type {!Window} */
SafariExtensionBar.prototype.contentWindow;

/** @type {string} */
SafariExtensionBar.prototype.identifier;

/** @type {string} */
SafariExtensionBar.prototype.label;

SafariExtensionBar.prototype.hide = function() {};
SafariExtensionBar.prototype.show = function() {};


/** @constructor */
Expand Down
Binary file modified javascript/safari-driver/prebuilt/SafariDriver.safariextz
Binary file not shown.

0 comments on commit e6df7ca

Please sign in to comment.