Skip to content

Commit

Permalink
Merge remote-tracking branch 'ysono/master' into enhance-click
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
	lib/commands/buttonClick.js
	lib/commands/click.js
  • Loading branch information
vvo committed Jan 5, 2014
2 parents 465fb2a + ef5c8ce commit b6578f1
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 200 deletions.
14 changes: 9 additions & 5 deletions README.md
Expand Up @@ -176,18 +176,18 @@ These are the current implemented helper methods. All methods take from 0
to a couple of parameters. Also all methods accept a callback so that we
can assert values or have more logic when the callback is called.

<<<<<<< HEAD
- **addValue(`String` selector, `String|String[]` value, `Function` callback)**<br>adds a value to an object found by a selector. You can also use unicode characters like `Left arrow` or `Back space`. You'll find all supported characters [here](https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/value). To do that, the value has to correspond to a key from the table.
- **buttonClick(`String` selector, `Function` callback)**<br>click on a button using a selector
- **call(callback)**<br>call given function in async order of current command queue
- **clearElement(`String` selector, `Function` callback)**<br>clear an element of text
- **click(`String` selector, `Function` callback)**<br>Clicks on an element based on a selector
- **click(`String` selector, `Function` callback)**<br>Clicks on an element based on a selector.
- **close([`String` tab ID to focus on,] `Function` callback)**<br>Close the current window (optional: and switch focus to opended tab)
- **deleteCookie(`String` name, `Function` callback)**<br>Delete a cookie for current page.
- **doubleClick(`String` selector, `Function` callback)**<br>Clicks on an element based on a selector
- **dragAndDrop(`String` sourceCssSelector, `String` destinationCssSelector, `Function` callback)**<br>Drags an item to a destination
- **end(`Function` callback)**<br>Ends a sessions (closes the browser)
- **endAll(`Function` callback)**<br>Ends all sessions (closes the browser)
- **execute(`String` script, `Array` arguments, `Function` callback)**<br>Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame.
- **execute(`String` or `Function` script, `Array` arguments, `Function` callback)**<br>Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. If script is a `Function`, arguments is required.
- **getAttribute(`String` selector, `String` attribute name, `Function` callback)**<br>Get an attribute from an dom obj based on the selector and attribute name
- **getCookie(`String` name, `Function` callback)**<br>Gets the cookie for current page.
- **getCssProperty(`String` selector, `String` css property name, `Function` callback)**<br>Gets a css property from a dom object selected with a selector
Expand All @@ -203,10 +203,13 @@ can assert values or have more logic when the callback is called.
- **getValue(`String` selector, `Function` callback)**<br>Gets the value of a dom obj found by selector
- **isSelected(`String` selector, `Function` callback)**<br>Return true or false if an OPTION element, or an INPUT element of type checkbox or radiobutton is currently selected (found by selector).
- **isVisible(`String` selector, `Function` callback)**<br>Return true or false if the selected dom obj is visible (found by selector)
- **leftClick(`String` selector, `Function` callback)**<br>Apply left click at an element. If selector is not provided, click at the last moved-to location.
- **middleClick(`String` selector, `Function` callback)**<br>Apply middle click at an element. If selector is not provided, click at the last moved-to location.
- **moveToObject(`String` selector, `Function` callback)**<br>Moves the page to the selected dom object
- **newWindow(`String` url, `String` name for the new window, `String` new window features (e.g. size, position, scrollbars, etc.), `Function` callback)**<br>equivalent function to `Window.open()` in a browser
- **pause(`Integer` milliseconds, `Function` callback)**<br>Pauses the commands by the provided milliseconds
- **refresh(`Function` callback)**<br>Refresh the current page
- **rightClick(`String` selector, `Function` callback)**<br>Apply right click at an element. If selector is not provided, click at the last moved-to location.
- **saveScreenshot(`String` path to file, `Function` callback)**<br>Saves a screenshot as a png from the current state of the browser
- **setCookie(`Object` cookie, `Function` callback)**<br>Sets a [cookie](http://code.google.com/p/selenium/wiki/JsonWireProtocol#Cookie_JSON_Object) for current page.
- **setValue(`String` selector, `String|String[]` value, `Function` callback)**<br>Sets a value to an object found by a selector (clears value before). You can also use unicode characters like `Left arrow` or `Back space`. You'll find all supported characters [here](https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/value). To do that, the value has to correspond to a key from the table.
Expand All @@ -221,8 +224,9 @@ Here are the implemented bindings (and links to the official json protocol bindi
- [alertDismiss](https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/dismiss_alert)
- [alertText](https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/alert_text)
- [back](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/back)
- [buttondown](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttondown)
- [buttonup](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttonup)
- [buttonPress](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/click)
- [buttonDown](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttondown)
- [buttonUp](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/buttonup)
- [cookie](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie)
- [cookieName](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie/:name)
- [doubleclick](http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/doubleclick)
Expand Down
38 changes: 4 additions & 34 deletions lib/commands/buttonClick.js 100644 → 100755
@@ -1,34 +1,4 @@
module.exports = function buttonClick (cssSelector, callback) {
var self = this;
this.element(cssSelector, function(err,result){

if(err === null && result.value){

self.moveTo(result.value.ELEMENT, function(err,result){

if(err === null) {

self.buttonDown(function(){

if(err === null) {

self.buttonUp(callback);

} else {
callback(err, result);
}

});

} else {
callback(err, result);
}
});

} else {

callback(err, result);

}
});
};
// Deprecated but included for backward compatibility. Alias for 'click' command.
module.exports = function buttonClick () {
return this.click.apply(this, arguments);
}
2 changes: 0 additions & 2 deletions lib/commands/dragAndDrop.js
@@ -1,7 +1,5 @@
module.exports = function dragAndDrop (cssSelectorItem, cssSelectorDropDestination, callback) {

var self = this;

this
.moveToObject(cssSelectorItem)
.buttonDown()
Expand Down
12 changes: 12 additions & 0 deletions lib/commands/leftClick.js
@@ -0,0 +1,12 @@
var handleMouseButtonCommand = require('../helpers/handleMouseButtonCommand');

module.exports = function leftClick (cssSelector, callback) {

handleMouseButtonCommand.call(
this,
cssSelector,
'left',
callback
);

};
12 changes: 12 additions & 0 deletions lib/commands/middleClick.js
@@ -0,0 +1,12 @@
var handleMouseButtonCommand = require('../helpers/handleMouseButtonCommand');

module.exports = function middleClick (cssSelector, callback) {

handleMouseButtonCommand.call(
this,
cssSelector,
'middle',
callback
);

};
12 changes: 12 additions & 0 deletions lib/commands/rightClick.js
@@ -0,0 +1,12 @@
var handleMouseButtonCommand = require('../helpers/handleMouseButtonCommand');

module.exports = function rightClick (cssSelector, callback) {

handleMouseButtonCommand.call(
this,
cssSelector,
'right',
callback
);

};
41 changes: 41 additions & 0 deletions lib/helpers/handleMouseButtonCommand.js
@@ -0,0 +1,41 @@
// call must be scoped to the webdriverjs client
module.exports = function(cssSelector, button, callback) {

var self = this;

if(typeof cssSelector === 'function') {
callback = cssSelector;
cssSelector = undefined;
}
callback = callback || function() {};

if(button === 'left' && cssSelector) {
self.click(cssSelector, callback);
} else if(! cssSelector) {
self.buttonPress(button, callback);
} else {

self.element("css selector", cssSelector, function(err,result) {

if(err === null && result.value) {

self.moveTo(result.value.ELEMENT, function(err,result){

if(err === null) {
self.buttonPress(button, callback);
} else {
callback(err, result);
}

});

} else {

callback(err,result);

}
});

}

};
28 changes: 28 additions & 0 deletions lib/helpers/handleMouseButtonProtocol.js
@@ -0,0 +1,28 @@
// call must be scoped to the webdriverjs client
module.exports = function(requestPath, button, callback) {

var buttonEnum = {
left: 0,
middle: 1,
right: 2
};

if(typeof callback !== 'function' && typeof button === 'function') {
callback = button;
button = buttonEnum.left;
}
if(typeof button !== 'number') {
button = buttonEnum[button || 'left'];
}

this.requestHandler.create(
{
path: requestPath,
method: "POST"
},
{
button: button
},
callback
);
};
13 changes: 7 additions & 6 deletions lib/protocol/buttonDown.js
@@ -1,10 +1,11 @@
module.exports = function buttonDown (callback) {
var handleMouseButtonProtocol = require('../helpers/handleMouseButtonProtocol');

var requestOptions = {
path:"/session/:sessionId/buttondown",
method:"POST"
};
module.exports = function buttonDown (button, callback) {

this.requestHandler.create(requestOptions,{},callback);
handleMouseButtonProtocol.call(
this,
"/session/:sessionId/buttondown",
button, callback
);

};
11 changes: 11 additions & 0 deletions lib/protocol/buttonPress.js
@@ -0,0 +1,11 @@
var handleMouseButtonProtocol = require('../helpers/handleMouseButtonProtocol');

module.exports = function buttonPress (button, callback) {

handleMouseButtonProtocol.call(
this,
"/session/:sessionId/click",
button, callback
);

};
13 changes: 7 additions & 6 deletions lib/protocol/buttonUp.js
@@ -1,10 +1,11 @@
module.exports = function buttonUp (callback) {
var handleMouseButtonProtocol = require('../helpers/handleMouseButtonProtocol');

var requestOptions = {
path:"/session/:sessionId/buttonup",
method:"POST"
};
module.exports = function buttonUp (button, callback) {

this.requestHandler.create(requestOptions,{},callback);
handleMouseButtonProtocol.call(
this,
"/session/:sessionId/buttonup",
button, callback
);

};
8 changes: 3 additions & 5 deletions lib/protocol/moveTo.js
@@ -1,22 +1,20 @@
var http = require("http");

module.exports = function moveTo (element, xoffset, yoffset, callback) {

var data = {};

if (typeof element === "string" && typeof element !== "function") {
if (typeof element === "string") {
data.element = element;
} else if (typeof element === "function") {
callback = element;
}

if (typeof xoffset === "number" && typeof xoffset !== "function") {
if (typeof xoffset === "number") {
data.xoffset = xoffset;
} else if (typeof xoffset === "function") {
callback = xoffset;
}

if (typeof yoffset === "number" && typeof yoffset !== "function") {
if (typeof yoffset === "number") {
data.yoffset = yoffset;
} else if (typeof yoffset === "function") {
callback = yoffset;
Expand Down
27 changes: 23 additions & 4 deletions test/site/index.html
Expand Up @@ -33,7 +33,8 @@
bottom: 50px;
}
.btn1_clicked,.btn2_clicked,.btn3_clicked,.btn4_clicked,
.btn1_dblclicked,.btn2_dblclicked,.btn3_dblclicked,.btn4_dblclicked {
.btn1_dblclicked,.btn2_dblclicked,.btn3_dblclicked,.btn4_dblclicked,
.btn1_middle_clicked, .btn1_right_clicked {
display:none;
}
</style>
Expand Down Expand Up @@ -62,6 +63,8 @@ <h2>nested elements</h2>

<button class="btn1">Klick #1</button>
<div class="btn1_clicked">Button #1 clicked</div>
<div class="btn1_middle_clicked">Button #1 middle-clicked</div>
<div class="btn1_right_clicked">Button #1 right-clicked</div>
<div class="btn1_dblclicked">Button #1 dblclicked</div>
<button class="btn2" disabled="">Klick #2</button>
<div class="btn2_clicked">Button #2 clicked</div>
Expand All @@ -76,9 +79,25 @@ <h2>nested elements</h2>

<script src="./jquery.min.js"></script>
<script type="text/javascript">
$('.btn1').click(function() {
$('.btn1_clicked').css('display','block');
});
$('.btn1')
.mousedown(function(e) {
switch(e.which) {
case 1:
$('.btn1_clicked').css('display','block');
break;
case 2:
$('.btn1_middle_clicked').css('display','block');
break;
case 3:
$('.btn1_right_clicked').css('display','block');
break;
}
return false;
})
.contextmenu(function() {
console.info('on contextmenu');
return false;
});
$('.btn2').click(function() {
$('.btn2_clicked').css('display','block');
});
Expand Down

0 comments on commit b6578f1

Please sign in to comment.