2019-02-21 13:05:05 +00:00
|
|
|
/* eslint no-console: "off" */
|
2020-05-11 14:04:27 +00:00
|
|
|
|
2020-05-12 15:37:22 +00:00
|
|
|
const elementNotFound = 'Element not found';
|
|
|
|
// this error comes from our pageObject `find `function
|
|
|
|
const pageObjectNotFound = 'PageObject not found';
|
2020-01-31 14:11:46 +00:00
|
|
|
const cannotDestructure = "Cannot destructure property 'context'";
|
|
|
|
const cannotReadContext = "Cannot read property 'context' of undefined";
|
2020-05-12 15:37:22 +00:00
|
|
|
|
2020-01-31 14:11:46 +00:00
|
|
|
// checking for existence of pageObjects is pretty difficult
|
|
|
|
// errors are thrown but we should check to make sure its the error that we
|
|
|
|
// want and not another real error
|
|
|
|
// to make things more difficult depending on how you reference the pageObject
|
|
|
|
// an error with a different message is thrown for example:
|
|
|
|
|
|
|
|
// pageObject[thing]() will give you a Element not found error
|
|
|
|
|
|
|
|
// whereas:
|
|
|
|
|
|
|
|
// const obj = pageObject[thing]; obj() will give you a 'cannot destructure error'
|
|
|
|
// and in CI it will give you a 'cannot read property' error
|
|
|
|
|
|
|
|
// the difference in CI could be a difference due to headless vs headed browser
|
|
|
|
// or difference in Chrome/browser versions
|
|
|
|
|
|
|
|
// ideally we wouldn't be checking on error messages at all, but we want to make sure
|
|
|
|
// that real errors are picked up by the tests, so if this gets unmanageable at any point
|
|
|
|
// look at checking for the instance of e being TypeError or similar
|
|
|
|
const isExpectedError = function(e) {
|
2020-05-12 15:37:22 +00:00
|
|
|
return [pageObjectNotFound, elementNotFound, cannotDestructure, cannotReadContext].some(item =>
|
|
|
|
e.message.startsWith(item)
|
|
|
|
);
|
2020-01-31 14:11:46 +00:00
|
|
|
};
|
2020-05-12 15:37:22 +00:00
|
|
|
const dont = `( don't| shouldn't| can't)?`;
|
2020-09-09 08:12:42 +00:00
|
|
|
export default function(scenario, assert, find, currentPage, $) {
|
2019-02-21 13:05:05 +00:00
|
|
|
scenario
|
2020-10-23 16:26:06 +00:00
|
|
|
.then([`I${dont} $verb the $pageObject object`], function(negative, verb, element, next) {
|
|
|
|
assert[negative ? 'notOk' : 'ok'](element[verb]());
|
|
|
|
setTimeout(() => next());
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
[
|
|
|
|
`I${dont} $verb the $pageObject object with value "$value"`,
|
|
|
|
`I${dont} $verb the $pageObject object from $yaml`,
|
|
|
|
],
|
|
|
|
function(negative, verb, element, data, next) {
|
|
|
|
assert[negative ? 'notOk' : 'ok'](element[verb](data));
|
|
|
|
setTimeout(() => next());
|
|
|
|
}
|
|
|
|
)
|
2020-11-06 14:57:19 +00:00
|
|
|
.then(`the $pageObject object is(n't)? $state`, function(element, negative, state, next) {
|
2020-10-23 16:26:06 +00:00
|
|
|
assert[negative ? 'notOk' : 'ok'](element[state]);
|
|
|
|
setTimeout(() => next());
|
|
|
|
})
|
|
|
|
.then(`I${dont} see $num of the $pageObject objects`, function(negative, num, element, next) {
|
|
|
|
assert[negative ? 'notEqual' : 'equal'](
|
|
|
|
element.length,
|
|
|
|
num,
|
|
|
|
`Expected to${negative ? ' not' : ''} see ${num} ${element.key}`
|
|
|
|
);
|
|
|
|
setTimeout(() => next());
|
|
|
|
})
|
2020-05-12 15:37:22 +00:00
|
|
|
.then(['I see $num of the $component object'], function(num, component) {
|
|
|
|
assert.equal(
|
|
|
|
currentPage()[component].length,
|
|
|
|
num,
|
|
|
|
`Expected to see ${num} items in the ${component} object`
|
|
|
|
);
|
|
|
|
})
|
2019-02-21 13:05:05 +00:00
|
|
|
.then('I see $property on the $component like yaml\n$yaml', function(
|
|
|
|
property,
|
|
|
|
component,
|
|
|
|
yaml
|
|
|
|
) {
|
|
|
|
const _component = currentPage()[component];
|
|
|
|
const iterator = new Array(_component.length).fill(true);
|
|
|
|
// this will catch if we get aren't managing to select a component
|
|
|
|
assert.ok(iterator.length > 0);
|
|
|
|
iterator.forEach(function(item, i, arr) {
|
|
|
|
const actual =
|
|
|
|
typeof _component.objectAt(i)[property] === 'undefined'
|
|
|
|
? null
|
|
|
|
: _component.objectAt(i)[property];
|
|
|
|
|
|
|
|
// anything coming from the DOM is going to be text/strings
|
|
|
|
// if the yaml has numbers, cast them to strings
|
|
|
|
// TODO: This would get problematic for deeper objects
|
|
|
|
// will have to look to do this recursively
|
|
|
|
const expected = typeof yaml[i] === 'number' ? yaml[i].toString() : yaml[i];
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
`Expected to see ${property} on ${component}[${i}] as ${JSON.stringify(
|
|
|
|
expected
|
|
|
|
)}, was ${JSON.stringify(actual)}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})
|
2020-05-11 14:04:27 +00:00
|
|
|
.then('I see $property on the $component vertically like yaml\n$yaml', function(
|
|
|
|
property,
|
|
|
|
component,
|
|
|
|
yaml
|
|
|
|
) {
|
2020-10-26 09:30:07 +00:00
|
|
|
const _component = find(component);
|
2020-05-11 14:04:27 +00:00
|
|
|
const iterator = new Array(_component.length).fill(true);
|
|
|
|
assert.ok(iterator.length > 0);
|
|
|
|
|
|
|
|
const items = _component.toArray().sort((a, b) => {
|
|
|
|
return (
|
|
|
|
$(a.scope)
|
|
|
|
.get(0)
|
|
|
|
.getBoundingClientRect().top -
|
|
|
|
$(b.scope)
|
|
|
|
.get(0)
|
|
|
|
.getBoundingClientRect().top
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
iterator.forEach(function(item, i, arr) {
|
|
|
|
const actual = typeof items[i][property] === 'undefined' ? null : items[i][property];
|
|
|
|
|
|
|
|
const expected = typeof yaml[i] === 'number' ? yaml[i].toString() : yaml[i];
|
|
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
actual,
|
|
|
|
expected,
|
|
|
|
`Expected to see ${property} on ${component}[${i}] as ${JSON.stringify(
|
|
|
|
expected
|
|
|
|
)}, was ${JSON.stringify(actual)}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})
|
2020-05-12 15:37:22 +00:00
|
|
|
.then([`I${dont} see $property`, `I${dont} see $property on the $component`], function(
|
|
|
|
negative,
|
|
|
|
property,
|
|
|
|
component
|
|
|
|
) {
|
|
|
|
const isNegative = typeof negative !== 'undefined';
|
|
|
|
let message = `Expected to${isNegative ? ' not' : ''} see ${property}`;
|
|
|
|
let target;
|
2020-01-31 14:11:46 +00:00
|
|
|
try {
|
2020-05-12 15:37:22 +00:00
|
|
|
if (typeof component === 'string') {
|
|
|
|
property = `${component}.${property}`;
|
|
|
|
message = `${message} on ${component}`;
|
|
|
|
}
|
|
|
|
target = find(property);
|
2020-01-31 14:11:46 +00:00
|
|
|
} catch (e) {
|
2020-05-12 15:37:22 +00:00
|
|
|
if (isNegative) {
|
|
|
|
if (isExpectedError(e)) {
|
|
|
|
assert.ok(true, message);
|
|
|
|
return Promise.resolve();
|
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
2020-01-31 14:11:46 +00:00
|
|
|
} else {
|
2020-05-12 15:37:22 +00:00
|
|
|
console.error(e);
|
2020-01-31 14:11:46 +00:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2020-05-12 15:37:22 +00:00
|
|
|
if (typeof target === 'function') {
|
|
|
|
if (isNegative) {
|
|
|
|
assert.throws(
|
|
|
|
function() {
|
|
|
|
target();
|
|
|
|
},
|
|
|
|
function(e) {
|
|
|
|
return isExpectedError(e);
|
|
|
|
},
|
|
|
|
message
|
|
|
|
);
|
|
|
|
return Promise.resolve();
|
2020-01-31 15:12:22 +00:00
|
|
|
} else {
|
2020-05-12 15:37:22 +00:00
|
|
|
try {
|
|
|
|
target = target();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
throw new Error(`The '${property}' page object doesn't exist`);
|
|
|
|
}
|
2020-01-31 15:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-12 15:37:22 +00:00
|
|
|
assert[isNegative ? 'notOk' : 'ok'](target, message);
|
|
|
|
return Promise.resolve();
|
2019-02-21 13:05:05 +00:00
|
|
|
})
|
2019-06-04 14:53:10 +00:00
|
|
|
.then(
|
|
|
|
[
|
2020-07-29 14:09:40 +00:00
|
|
|
`I see $property on the $component (contains|like) "$value"`,
|
|
|
|
`I see $property on the $component (contains|like) '$value'`,
|
2019-06-04 14:53:10 +00:00
|
|
|
],
|
2020-07-29 14:09:40 +00:00
|
|
|
function(property, component, containsLike, value) {
|
2019-06-04 14:53:10 +00:00
|
|
|
let target;
|
2020-12-14 15:28:35 +00:00
|
|
|
|
|
|
|
if (typeof component === 'string') {
|
|
|
|
property = `${component}.${property}`;
|
2019-06-04 14:53:10 +00:00
|
|
|
}
|
2020-12-14 15:28:35 +00:00
|
|
|
target = find(property);
|
|
|
|
|
2020-07-29 14:09:40 +00:00
|
|
|
if (containsLike === 'like') {
|
|
|
|
assert.equal(
|
|
|
|
target,
|
|
|
|
value,
|
|
|
|
`Expected to see ${property} on ${component} as ${value}, was ${target}`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
assert.ok(
|
|
|
|
target.indexOf(value) !== -1,
|
|
|
|
`Expected to see ${property} on ${component} within ${value}, was ${target}`
|
|
|
|
);
|
|
|
|
}
|
2019-06-04 14:53:10 +00:00
|
|
|
}
|
|
|
|
)
|
2019-03-07 10:51:39 +00:00
|
|
|
.then(['I see $property like "$value"'], function(property, value) {
|
|
|
|
const target = currentPage()[property];
|
|
|
|
assert.equal(target, value, `Expected to see ${property} as ${value}, was ${target}`);
|
2019-02-21 13:05:05 +00:00
|
|
|
});
|
|
|
|
}
|