Skip to content

Commit

Permalink
improved test case
Browse files Browse the repository at this point in the history
- added own test html page
- added tests for get* commands
- fixed wrong implemented commands and protocol commands
- use chai for assertion tests
  • Loading branch information
christian-bromann committed Mar 21, 2013
1 parent 052ca7d commit 6b0eb37
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.hg
.hgignore
/*.png
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/getElementSize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
exports.command = function(using, value, callback) {
exports.command = function(cssSelector, callback) {

var self = this;

this.element(using, value, function(result) {
this.element('css selector', cssSelector, function(result) {
if(result.status === 0) {

self.elementIdSize(result.value.ELEMENT, function(result) {
Expand Down
6 changes: 4 additions & 2 deletions lib/commands/getLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ exports.command = function(cssSelector, callback) {
var self = this;

this.element("css selector", cssSelector, function(result) {
if(result.status === 0) {
if(result.status === 0 && result.value !== undefined) {

self.elementIdLocation(result.value.ELEMENT, function(result) {
if (typeof callback === "function") {
if (typeof callback === "function" && result !== undefined) {
callback({x:result.value.x, y: result.value.y});
} else {
callback(result);
}
});

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/getTagName.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports.command = function(cssSelector, callback) {

self.elementIdName(result.value.ELEMENT, function(result) {
if (typeof callback === "function") {
callback(result.value);
callback(result.value.toLowerCase());
}
});

Expand Down
4 changes: 2 additions & 2 deletions lib/commands/getText.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ exports.command = function(cssSelector, callback) {

self.elementIdText(result.value.ELEMENT, function(result) {
if (typeof callback === "function") {
callback(result);
callback(result.value);
}
});

} else {

if (typeof callback === "function") {
callback(result.value);
callback(result);
}

}
Expand Down
23 changes: 0 additions & 23 deletions lib/commands/getValue.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/protocol/elementIdAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.command = function (id, attributeName, callback) {
};

requestOptions.path = requestOptions.path.replace(/:id/gi, id);
requestOptions.path = requestOptions.path.replace(":name", attributeName);
requestOptions.path = requestOptions.path.replace(/:name/gi, attributeName);

this.executeProtocolCommand(
requestOptions,
Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/elementIdLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.command = function (id, callback) {

this.executeProtocolCommand(
requestOptions,
this.proxyResponseNoReturn(callback),
this.proxyResponse(callback),
{}
);

Expand Down
2 changes: 1 addition & 1 deletion lib/protocol/elementIdLocationInView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exports.command = function (id, callback) {

this.executeProtocolCommand(
requestOptions,
this.proxyResponseNoReturn(callback),
this.proxyResponse(callback),
{}
);

Expand Down
39 changes: 7 additions & 32 deletions lib/protocol/elementIdValue.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
exports.command = function (id, value, callback) {

var commandOptionsPost = {
var requestOptions = {
path:"/session/:sessionId/element/:id/value",
method:"POST"
};

var commandOptionsGet = {
path:"/session/:sessionId/element/:id/value",
method:"GET"
};

var requestOptions = {};

// set or get
if (typeof value === "string") {

requestOptions = commandOptionsPost;
requestOptions.path = requestOptions.path.replace(/:id/gi, id);

this.executeProtocolCommand(
requestOptions,
this.proxyResponseNoReturn(callback),
{"value":value.split("")}
);

} else {

callback = value;

requestOptions = commandOptionsGet;
requestOptions.path = requestOptions.path.replace(/:id/gi, id);
requestOptions.path = requestOptions.path.replace(/:id/gi, id);

this.executeProtocolCommand(
requestOptions,
this.proxyResponse(callback),
{}
);
this.executeProtocolCommand(
requestOptions,
this.proxyResponseNoReturn(callback),
{"value":value.split("")}
);

}
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"test": "mocha"
},
"dependencies": {},
"devDependencies": {},
"devDependencies": {
"chai": "1.x"
},
"optionalDependencies": {},
"tags": [
"web",
Expand Down
99 changes: 70 additions & 29 deletions test/commands-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var assert = require("assert"),
webdriverjs = require("../index"),
githubTitle = 'GitHub · Build software better, together.',
githubRepoTitle = 'Camme/webdriverjs · GitHub';
var assert = require('chai').assert,
fs = require('fs'),
webdriverjs = require('../index'),
testpageURL = 'http://webdriverjs.christian-bromann.com/',
githubTitle = 'GitHub · Build software better, together.',
testpageTitle = 'WebdriverJS Testpage',
testpageSource = "";

describe('test webdriverjs API', function(){

Expand All @@ -12,65 +15,103 @@ describe('test webdriverjs API', function(){
before(function(){
// init client
var capabilities = {
'browserName': 'safari',
// 'safari.binary': '/Applications/Safari.app/Contents/MacOS/Safari'
'browserName': 'phantomjs'
};
client = webdriverjs.remote({desiredCapabilities:capabilities});
});

beforeEach(function(){
client
.init()
.url('https://github.com/Camme/webdriverjs');
.url(testpageURL);

// load source of testpage for getSource() test
fs.readFile('./test/index.html', function (err, html) {
if (err) {
throw err;
}
testpageSource = html.toString();
});
});

describe('test commands', function(){
it('get commands should return the evaluated value', function(done){
client
.getElementCssProperty('id', 'footer', 'color', function(result) {
assert('rgba(119,119,119,1)' === result.replace(/\s+/g, ''));
.getAttribute('.nested', 'style', function(result) {
assert.strictEqual(result,'text-transform: uppercase; ');
})
.getCssProperty('#footer', 'color', function(result) {
assert('rgba(119,119,119,1)' === result.replace(/\s+/g, ''));
.getElementCssProperty('css selector', '.red', 'background-color', function(result) {
assert.strictEqual('rgba(255, 0, 0, 1)',result);
})
.getElementCssProperty('class name', 'repo-label', 'text-transform', function(result) {
assert('uppercase' === result);
.getCssProperty('.green', 'float', function(result) {
assert.strictEqual('left',result);
})
.getCssProperty('.repo-label', 'text-transform', function(result) {
assert('uppercase' === result);
.getElementCssProperty('class name', 'yellow', 'width', function(result) {
assert.strictEqual('100px',result);
})
.getElementCssProperty('id', 'js-repo-pjax-container', 'width', function(result) {
assert('920px' === result);
.getCssProperty('.black', 'background-color', function(result) {
assert.strictEqual('rgba(0, 0, 0, 1)',result);
})
.getCssProperty('.container', 'width', function(result) {
assert('920px' === result);
.getElementCssProperty('id', 'purplebox', 'margin-right', function(result) {
assert.strictEqual('10px',result);
})
.end(done);
.getCssProperty('.purple', 'margin-right', function(result) {
assert.strictEqual('10px',result);
})
.getElementSize('.red', function(result) {
assert.strictEqual(result.width,102);
assert.strictEqual(result.height,102);
})
.getLocation('.green', function(result) {
assert.strictEqual(result.x,120);
assert.strictEqual(result.y,89);
})
.getLocationInView('.green', function(result) {
assert.strictEqual(result.x,120);
assert.strictEqual(result.y,89);
})
.getSource(function(result) {
assert.strictEqual(result,testpageSource);
})
.getTagName('.black', function(result) {
assert.strictEqual(result,'div');
})
.getTagName('#githubRepo', function(result) {
assert.strictEqual(result,'a');
})
.getText('#githubRepo', function(result) {
assert.strictEqual(result,'GitHub Repo');
})
.getTitle(function(title) {
assert.strictEqual(title,testpageTitle);
done();
});
});

it.only('back button should return to the previous page', function(done) {
it('back/foward should return to the previous/next page', function(done) {
client
.getTitle(function(title) {
assert(title === githubRepoTitle);
assert.strictEqual(title,testpageTitle);
})
.click('.header-logo-wordmark')
.click('#githubRepo')
// waitFor fix, to get safari a little bit more time to load
.waitFor('.teaser-illustration',5000)
.getTitle(function(title) {
assert(title === githubTitle);
assert.strictEqual(title,githubTitle);
})
.back()
// waitFor fix, to get safari a little bit more time to load
.waitFor('.public',5000)
.getTitle(function(title) {
assert(title === githubRepoTitle);
assert.strictEqual(title,testpageTitle);
})
.forward()
.waitFor('.teaser-illustration',5000)
.getTitle(function(title) {
assert(title === githubTitle);
assert.strictEqual(title,githubTitle);
})
.end(done);
});
});

after(function() {
client.end();
});
});
41 changes: 41 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html><html><head>
<title>WebdriverJS Testpage</title>

<style type="text/css">
.box {
width: 100px;
height: 100px;
float: left;
border: magenta 1px solid;
margin-right: 10px;
}
#githubRepo {
display:block;
}
.red { background: red; }
.green { background: green; }
.yellow { background: yellow; }
.black { background: black; }
.purple { background: purple; }
</style>

</head>
<body>

<h1>Test CSS Attributes</h1>
<hr>
<div class="box red"></div>
<div class="box green"></div>
<div class="box yellow"></div>
<div class="box black"></div>
<div class="box purple" id="purplebox"></div>

<a href="https://github.com" id="githubRepo" class="clearfix">GitHub Repo</a>

<div class="nested" style="text-transform: uppercase">
<h2>nested elements</h2>
<div>
<span>nested span</span>
</div>
</div>
</body></html>

0 comments on commit 6b0eb37

Please sign in to comment.