-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
38b6b6c
bcd5628
9f22f72
b098018
0a583a6
914ba0d
fb042f5
33f0c35
18eb792
a696a2a
c6c1ea8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"only": "test" | ||
} |
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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ node_modules | |
npm-debug.log | ||
test/tmp | ||
examples/**/log | ||
test/log | ||
test/**/*.coffee |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"launch": true, | ||
"app": { | ||
"command": "testium-example-app" | ||
} | ||
} |
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. | ||
``` |
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) ]; | ||
}; | ||
|
||
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); | ||
}; |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I miss template strings already 😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could replace it with |
||
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); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* jshint browser: true */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, ''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we polyfill There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are injecting this into the page under test, so polyfilling |
||
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; | ||
}; |
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; |
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); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
exports._forwarded = [ | ||
'acceptAlert', | ||
'dismissAlert', | ||
'getAlertText', | ||
'typeAlert', | ||
]; |
There was a problem hiding this comment.
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]
andresult[1]
. You could destructure but I feel like you should just inline.There was a problem hiding this comment.
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.