@progress/kendo-e2e
Version:
Kendo UI end-to-end test utilities.
251 lines • 13 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.expectSelector = expectSelector;
function expectSelector(driver, by) {
const DEFAULT_TIMEOUT = 3000;
const DEFAULT_POLL_INTERVAL = 25;
function fail(message) {
throw new Error(message);
}
const getLocatorString = () => {
return by.toString();
};
const toHaveText = function toHaveText(expected, opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const deadline = Date.now() + timeout;
let lastText = '';
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
const text = (_e = (_d = (_c = (yield element.getText())) === null || _c === void 0 ? void 0 : _c.trim) === null || _d === void 0 ? void 0 : _d.call(_c)) !== null && _e !== void 0 ? _e : '';
lastText = text;
if (expected instanceof RegExp) {
if (expected.test(text))
return;
}
else if (text === expected) {
return;
}
}
catch (_f) {
// retry
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const exp = expected instanceof RegExp ? `/${expected.source}/` : `"${expected}"`;
const locatorStr = getLocatorString();
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} to have text ${exp} but got "${lastText}"`
: `Expected element ${locatorStr} to have text ${exp} but got "${lastText}"`;
fail(errorMessage);
});
};
const toHaveValue = function toHaveValue(expected, opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const deadline = Date.now() + timeout;
let lastValue = '';
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
const value = (_c = (yield element.getAttribute('value'))) !== null && _c !== void 0 ? _c : '';
lastValue = value;
if (value === expected) {
return;
}
}
catch (_d) {
// retry
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const locatorStr = getLocatorString();
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} to have value "${expected}" but got "${lastValue}"`
: `Expected element ${locatorStr} to have value "${expected}" but got "${lastValue}"`;
fail(errorMessage);
});
};
const toHaveFocus = function toHaveFocus(opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
const activeElement = yield driver.switchTo().activeElement();
const elementId = yield element.getId();
const activeId = yield activeElement.getId();
if (elementId === activeId) {
return;
}
}
catch (_c) {
// retry
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const locatorStr = getLocatorString();
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} to have focus within ${timeout}ms`
: `Expected element ${locatorStr} to have focus within ${timeout}ms`;
fail(errorMessage);
});
};
const toHaveNoFocus = function toHaveNoFocus(opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
const activeElement = yield driver.switchTo().activeElement();
const elementId = yield element.getId();
const activeId = yield activeElement.getId();
if (elementId !== activeId) {
return;
}
}
catch (_c) {
// Element not found, so it doesn't have focus
return;
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const locatorStr = getLocatorString();
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} to not have focus within ${timeout}ms`
: `Expected element ${locatorStr} to not have focus within ${timeout}ms`;
fail(errorMessage);
});
};
const toHaveAttribute = function toHaveAttribute(attribute, expected, opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const exactMatch = (_c = opts === null || opts === void 0 ? void 0 : opts.exactMatch) !== null && _c !== void 0 ? _c : true;
const deadline = Date.now() + timeout;
let lastValue = '';
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
const value = (_d = (yield element.getAttribute(attribute))) !== null && _d !== void 0 ? _d : '';
lastValue = value;
if (exactMatch) {
if (value === expected)
return;
}
else {
if (value.includes(expected))
return;
}
}
catch (_e) {
// retry
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const locatorStr = getLocatorString();
const matchType = exactMatch ? 'exactly' : 'to contain';
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} attribute "${attribute}" ${matchType} "${expected}" but got "${lastValue}"`
: `Expected element ${locatorStr} attribute "${attribute}" ${matchType} "${expected}" but got "${lastValue}"`;
fail(errorMessage);
});
};
const toHaveClass = function toHaveClass(expected, opts) {
return __awaiter(this, void 0, void 0, function* () {
return toHaveAttribute('class', expected, opts);
});
};
const toBeVisible = function toBeVisible(opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
if (yield element.isDisplayed()) {
return;
}
}
catch (_c) {
// retry
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const locatorStr = getLocatorString();
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} to be visible within ${timeout}ms`
: `Expected element ${locatorStr} to be visible within ${timeout}ms`;
fail(errorMessage);
});
};
const toBeNotVisible = function toBeNotVisible(opts) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const timeout = (_a = opts === null || opts === void 0 ? void 0 : opts.timeout) !== null && _a !== void 0 ? _a : DEFAULT_TIMEOUT;
const pollInterval = (_b = opts === null || opts === void 0 ? void 0 : opts.pollInterval) !== null && _b !== void 0 ? _b : DEFAULT_POLL_INTERVAL;
const deadline = Date.now() + timeout;
while (Date.now() < deadline) {
try {
const element = yield driver.findElement(by);
if (!(yield element.isDisplayed())) {
return;
}
}
catch (_c) {
// Element not found, so it's not visible
return;
}
yield new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const locatorStr = getLocatorString();
const customMessage = opts === null || opts === void 0 ? void 0 : opts.message;
const errorMessage = customMessage
? `${customMessage} Expected element ${locatorStr} to become hidden within ${timeout}ms`
: `Expected element ${locatorStr} to become hidden within ${timeout}ms`;
fail(errorMessage);
});
};
return {
toHaveText,
toBeVisible,
toHaveValue,
toHaveFocus,
toHaveAttribute,
toHaveClass,
not: {
toBeVisible: toBeNotVisible,
toHaveFocus: toHaveNoFocus
},
};
}
//# sourceMappingURL=expect.js.map