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

First working version #1

Merged
merged 11 commits into from
Oct 18, 2015
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
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"only": "test"
}
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "airbnb/legacy",
"rules": {
"id-length": 0,
"vars-on-top": 0,
"strict": [2, "global"],
"no-param-reassign": 0
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules
npm-debug.log
test/tmp
examples/**/log
test/log
test/**/*.coffee
6 changes: 6 additions & 0 deletions .testiumrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"launch": true,
"app": {
"command": "testium-example-app"
}
}
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# Testium: Sync

Provides a sync API for [testium](https://www.npmjs.com/package/testium).
Uses [webdriver-http-sync](https://www.npmjs.com/package/webdriver-http-sync) for interacting with selenium.

Check out [testium's documentation](https://www.npmjs.com/package/testium) for more information on the `browser` API.

## Usage

```js
import initTestium from 'testium-core';
import createDriver from 'testium-driver-sync';

async function runTests() {
const { browser } = initTestium().then(createDriver);
browser.navigateTo('/');
}
runTests();
```

```coffee
initTestium = require 'testium-core';
createDriver = require 'testium-driver-sync';

initTestium()
.then(createDriver)
.then ({browser}) ->
browser.navigateTo '/'
```

## License *(BSD-3-Clause)*

```
Copyright (c) 2015, Groupon, Inc.
All rights reserved.

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

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

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.

Neither the name of GROUPON nor the names of its contributors 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
HOLDER 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.
```
176 changes: 176 additions & 0 deletions lib/assert/element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
'use strict';

var util = require('util');

var assert = require('assertive');
var _ = require('lodash');

function isTextOrRegexp(textOrRegExp) {
return _.isString(textOrRegExp) || _.isRegExp(textOrRegExp);
}

exports._getElementWithProperty = function _getElementWithProperty(selector, property) {
var element = this._getElement(selector);
return [ element, element.get(property) ];
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this method seems weird and redundant, and makes the code harder to read. The consumers that use it refer to the element and its property value as result[0] and result[1]. You could destructure but I feel like you should just inline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Will switch to inlining. One of the things I should have questioned instead of blindly porting it 1:1.


exports._getElement = function _getElement(selector) {
var elements = this.driver.getElements(selector);
var count = elements.length;

if (count === 0) {
throw new Error('Element not found for selector: ' + selector);
}
if (count !== 1) {
throw new Error(
'assertion needs a unique selector!\n' +
selector + ' has ' + count + ' hits in the page');
}

return elements[0];
};

exports.elementHasAttributes = function elementHasAttributes(doc, selector, attributesObject) {
if (arguments.length === 2) {
attributesObject = selector;
selector = doc;
doc = [
'elementHasAttributes - selector:' + selector,
'attributesObject:' + JSON.stringify(attributesObject),
].join('\n');
} else {
assert.hasType('elementHasAttributes(docstring, selector, attributesObject) - requires String docstring', String, doc);
}

assert.hasType('elementHasAttributes(selector, attributesObject) - requires String selector', String, selector);
assert.hasType('elementHasAttributes(selector, attributesObject) - requires Object attributesObject', Object, attributesObject);

var element = this._getElement(selector);

_.each(attributesObject, function verifyAttribute(val, attribute) {
var actualVal = element.get(attribute);
var attrDoc = util.format(
'%s\nattribute %j was expected to be %j but was %j.',
doc, attribute, val, actualVal);

if (_.isString(val)) {
assert.equal(attrDoc, val, actualVal);
} else {
assert.hasType('elementHasAttributes(selector, attributesObject) - attributesObject requires String or RegExp value', RegExp, val);
assert.match(attrDoc, val, actualVal);
}
});

return element;
};

exports.elementHasText = function elementHasText(doc, selector, textOrRegExp) {
if (arguments.length === 2) {
textOrRegExp = selector;
selector = doc;
doc = 'elementHasText: ' + selector;
} else {
assert.hasType('elementHasText(docstring, selector, textOrRegExp) - requires docstring', String, doc);
}

assert.hasType('elementHasText(selector, textOrRegExp) - requires selector', String, selector);
assert.truthy('elementHasText(selector, textOrRegExp) - requires textOrRegExp', isTextOrRegexp(textOrRegExp));

var result = this._getElementWithProperty(selector, 'text');

if (textOrRegExp === '') {
assert.equal(textOrRegExp, result[1]);
} else {
assert.include(doc, textOrRegExp, result[1]);
}

return result[0];
};

exports.elementLacksText = function elementLacksText(doc, selector, textOrRegExp) {
if (arguments.length === 2) {
textOrRegExp = selector;
selector = doc;
doc = 'elementLacksText: ' + selector;
} else {
assert.hasType('elementLacksText(docstring, selector, textOrRegExp) - requires docstring', String, doc);
}

assert.hasType('elementLacksText(selector, textOrRegExp) - requires selector', String, selector);
assert.truthy('elementLacksText(selector, textOrRegExp) - requires textOrRegExp', isTextOrRegexp(textOrRegExp));

var result = this._getElementWithProperty(selector, 'text');

assert.notInclude(doc, textOrRegExp, result[1]);
return result[0];
};

exports.elementHasValue = function elementHasValue(doc, selector, textOrRegExp) {
if (arguments.length === 2) {
textOrRegExp = selector;
selector = doc;
doc = 'elementHasValue: ' + selector;
} else {
assert.hasType('elementHasValue(docstring, selector, textOrRegExp) - requires docstring', String, doc);
}

assert.hasType('elementHasValue(selector, textOrRegExp) - requires selector', String, selector);
assert.truthy('elementHasValue(selector, textOrRegExp) - requires textOrRegExp', isTextOrRegexp(textOrRegExp));

var result = this._getElementWithProperty(selector, 'value');

if (textOrRegExp === '') {
assert.equal(textOrRegExp, result[1]);
} else {
assert.include(doc, textOrRegExp, result[1]);
}

return result[0];
};

exports.elementLacksValue = function elementLacksValue(doc, selector, textOrRegExp) {
if (arguments.length === 2) {
textOrRegExp = selector;
selector = doc;
doc = 'elementLacksValue: ' + selector;
} else {
assert.hasType('elementLacksValue(docstring, selector, textOrRegExp) - requires docstring', String, doc);
}

assert.hasType('elementLacksValue(selector, textOrRegExp) - requires selector', String, selector);
assert.truthy('elementLacksValue(selector, textOrRegExp) - requires textOrRegExp', isTextOrRegexp(textOrRegExp));

var result = this._getElementWithProperty(selector, 'value');

assert.notInclude(doc, textOrRegExp, result[1]);
return result[0];
};

exports.elementIsVisible = function elementIsVisible(selector) {
assert.hasType('elementIsVisible(selector) - requires (String) selector', String, selector);
var element = this.browser.getElementWithoutError(selector);
assert.truthy('Element not found for selector: ' + selector, element);
assert.truthy('Element should be visible for selector: ' + selector, element.isVisible());
return element;
};

exports.elementNotVisible = function elementNotVisible(selector) {
assert.hasType('elementNotVisible(selector) - requires (String) selector', String, selector);
var element = this.browser.getElementWithoutError(selector);
assert.truthy('Element not found for selector: ' + selector, element);
assert.falsey('Element should not be visible for selector: ' + selector, element.isVisible());
return element;
};

exports.elementExists = function elementExists(selector) {
assert.hasType('elementExists(selector) - requires (String) selector', String, selector);
var element = this.browser.getElementWithoutError(selector);
assert.truthy('Element not found for selector: ' + selector, element);
return element;
};

exports.elementDoesntExist = function elementDoesntExist(selector) {
assert.hasType('elementDoesntExist(selector) - requires (String) selector', String, selector);
var element = this.browser.getElementWithoutError(selector);
assert.falsey('Element found for selector: ' + selector, element);
};
34 changes: 34 additions & 0 deletions lib/assert/imgLoaded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

var assert = require('assertive');
var imgLoadedFn = require('./imgLoaded_client');
var _ = require('lodash');

exports.imgLoaded = function imgLoaded(doc, selector) {
if (arguments.length === 1) {
selector = doc;
doc = undefined;
}
assert.hasType('imgLoaded(selector) - requires (String) selector', String, selector);

var result = this.browser.evaluate(selector, imgLoadedFn);
if (result === true) {
return;
}

function fail(help) {
var message = 'imgLoaded ' + JSON.stringify(selector) + ': ' + help;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss template strings already 😁

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could replace it with util.format('imgLoaded %j: %s', selector, help). But I was too lazy for that. ^^

if (doc) {
message = doc + '\n' + message;
}
throw new Error(message);
}

if (result === 0) {
fail('element not found');
} else if (_.isNumber(result)) {
fail('non-unique selector; count: ' + result);
} else {
fail('failed to load ' + result);
}
};
35 changes: 35 additions & 0 deletions lib/assert/imgLoaded_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* jshint browser: true */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💥

'use strict';

// returns true if the image is loaded and decoded,
// or a number, if it didn't find one single image,
// or a helpful error if it wasn't an image,
// or the path, if it found some non-loaded image
module.exports = function imgLoaded(selector) {
function describe(elem) {
var tag = (elem.tagName || '').toLowerCase();
if (elem.id) {
tag += '#' + elem.id;
}
var classes = elem.className.replace(/^\s+|\s+$/g, '');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we polyfill .trim and just use that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are injecting this into the page under test, so polyfilling .trim sounds dangerous.

if (classes) {
tag += '.' + classes.replace(/\s+/g, '.');
}
return tag;
}

var imgs = document.querySelectorAll(selector);
if (imgs.length !== 1) {
return imgs.length;
}
var img = imgs[0];

if (!img.src) {
if ((img.tagName || '').match(/^img$/i)) {
return 'src-less ' + describe(img);
}
return 'non-image ' + describe(img);
}

return img.complete && img.naturalWidth ? true : img.src;
};
18 changes: 18 additions & 0 deletions lib/assert/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var _ = require('lodash');

function Assertions(driver, browser) {
this.driver = driver;
this.browser = browser;
}

_.each([
require('./element'),
require('./imgLoaded'),
require('./navigation'),
], function applyMixin(mixin) {
_.extend(Assertions.prototype, mixin);
});

module.exports = Assertions;
9 changes: 9 additions & 0 deletions lib/assert/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var assert = require('assertive');

exports.httpStatus = function httpStatus(expectedStatus) {
assert.hasType('assert.httpStatus(status) - requires (Number) status', Number, expectedStatus);
var actualStatus = this.browser.getStatusCode();
assert.equal('statuscode', expectedStatus, actualStatus);
};
8 changes: 8 additions & 0 deletions lib/browser/alert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

exports._forwarded = [
'acceptAlert',
'dismissAlert',
'getAlertText',
'typeAlert',
];
Loading