chrec-core
Version:
ChRec's core business logic and model for testing HTML locator robustness
175 lines (174 loc) • 10.1 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const compareVersions = require("compare-versions");
const load_json_file_1 = require("load-json-file");
const write_json_file_1 = require("write-json-file");
const browser_action_test_result_1 = require("../model/action-test-result/browser-action-test-result");
const html_element_action_test_result_1 = require("../model/action-test-result/html-element-action-test-result");
const back_1 = require("../model/action/browser-action/back");
const forward_1 = require("../model/action/browser-action/forward");
const go_to_1 = require("../model/action/browser-action/go-to");
const refresh_1 = require("../model/action/browser-action/refresh");
const switch_to_default_context_1 = require("../model/action/browser-action/switch-to-default-context");
const clear_1 = require("../model/action/html-element-action/clear");
const click_1 = require("../model/action/html-element-action/click");
const read_1 = require("../model/action/html-element-action/read");
const select_1 = require("../model/action/html-element-action/select");
const submit_1 = require("../model/action/html-element-action/submit");
const switch_to_context_1 = require("../model/action/html-element-action/switch-to-context");
const type_1 = require("../model/action/html-element-action/type");
const wait_for_added_html_element_1 = require("../model/action/html-element-action/wait-for-added-html-element");
const wait_for_removed_html_element_1 = require("../model/action/html-element-action/wait-for-removed-html-element");
const bounding_box_1 = require("../model/bounding-box");
const chrome_1 = require("../model/browser/chrome");
const edge_1 = require("../model/browser/edge");
const firefox_1 = require("../model/browser/firefox");
const internet_explorer_1 = require("../model/browser/internet-explorer");
const locator_test_result_1 = require("../model/locator-test-result");
const css_locator_1 = require("../model/locator/css-locator");
const xpath_locator_1 = require("../model/locator/xpath-locator");
const project_1 = require("../model/project");
const sequence_1 = require("../model/sequence");
class ChrecJsonService {
exportChrecJson(absolutePath, project) {
return __awaiter(this, void 0, void 0, function* () {
const json = { name: 'ChRec', version: process.env.npm_package_version, project };
yield write_json_file_1.default(absolutePath, json);
});
}
importChrecJson(absolutePath) {
return __awaiter(this, void 0, void 0, function* () {
const parsedJson = yield load_json_file_1.default(absolutePath);
return this.validateChrecJson(parsedJson);
});
}
validateChrecJson(parsedJson) {
if (parsedJson.name !== 'ChRec') {
throw new Error('Invalid ChRec JSON');
}
if (compareVersions.compare(parsedJson.version, process.env.npm_package_version, '>')) {
throw new Error('Incompatible Versions. Please Upgrade your ChRec version!');
}
return this.reviveProject(parsedJson.project);
}
reviveProject(parsedJson) {
const sequences = [];
for (const sequence of parsedJson.sequences) {
sequences.push(this.reviveSequence(sequence));
}
return new project_1.Project(parsedJson.name, sequences, parsedJson.id);
}
reviveSequence(parsedJson) {
const actions = [];
for (const action of parsedJson.actions) {
actions.push(this.reviveAction(action));
}
return new sequence_1.Sequence(parsedJson.name, actions, parsedJson.id);
}
reviveAction(parsedJson) {
if (parsedJson.className === 'Back' ||
parsedJson.className === 'Forward' ||
parsedJson.className === 'GoTo' ||
parsedJson.className === 'Refresh' ||
parsedJson.className === 'SwitchToDefaultContext') {
return this.reviveBrowserAction(parsedJson);
}
return this.reviveHtmlElementAction(parsedJson);
}
reviveBrowserAction(parsedJson) {
const actionTestResults = [];
for (const actionTestResult of parsedJson.testResults) {
actionTestResults.push(this.reviveBrowserActionTestResult(actionTestResult));
}
switch (parsedJson.className) {
case 'Back':
return new back_1.Back(actionTestResults, parsedJson.image, parsedJson.id);
case 'Forward':
return new forward_1.Forward(actionTestResults, parsedJson.image, parsedJson.id);
case 'GoTo':
return new go_to_1.GoTo(actionTestResults, parsedJson.image, parsedJson.url, parsedJson.id);
case 'Refresh':
return new refresh_1.Refresh(actionTestResults, parsedJson.image, parsedJson.id);
}
return new switch_to_default_context_1.SwitchToDefaultContext(actionTestResults, parsedJson.image, parsedJson.id);
}
reviveHtmlElementAction(parsedJson) {
const actionTestResults = [];
for (const actionTestResult of parsedJson.testResults) {
actionTestResults.push(this.reviveHtmlElementActionTestResult(actionTestResult));
}
const locators = [];
for (const locator of parsedJson.locators) {
locators.push(this.reviveLocator(locator));
}
const boundingBox = this.reviveBoundingBox(parsedJson.boundingBox);
switch (parsedJson.className) {
case 'Clear':
return new clear_1.Clear(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.id);
case 'Click':
return new click_1.Click(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.id);
case 'Read':
return new read_1.Read(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.text, parsedJson.id);
case 'Select':
return new select_1.Select(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.value, parsedJson.id);
case 'Submit':
return new submit_1.Submit(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.id);
case 'SwitchToContext':
return new switch_to_context_1.SwitchToContext(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.id);
case 'Type':
return new type_1.Type(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.value, parsedJson.id);
case 'WaitForAddedHtmlElement':
return new wait_for_added_html_element_1.WaitForAddedHtmlElement(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.timeout, parsedJson.id);
case 'WaitForRemovedHtmlElement':
return new wait_for_removed_html_element_1.WaitForRemovedHtmlElement(actionTestResults, parsedJson.image, locators, boundingBox, parsedJson.timeout, parsedJson.id);
}
throw new Error(`Internal: Could not revive HtmlElementAction with className ${parsedJson.className} from JSON!`);
}
reviveBoundingBox(parsedJson) {
return new bounding_box_1.BoundingBox(parsedJson.x, parsedJson.y, parsedJson.width, parsedJson.height);
}
reviveLocator(parsedJson) {
const locatorTestResults = [];
for (const locatorTestResult of parsedJson.testResults) {
locatorTestResults.push(this.reviveLocatorTestResult(locatorTestResult));
}
switch (parsedJson.className) {
case 'CssLocator':
return new css_locator_1.CssLocator(locatorTestResults, parsedJson.method, parsedJson.value, parsedJson.id);
case 'XpathLocator':
return new xpath_locator_1.XpathLocator(locatorTestResults, parsedJson.method, parsedJson.value, parsedJson.id);
}
throw new Error('Could not revive Locator from JSON!');
}
reviveLocatorTestResult(parsedJson) {
return new locator_test_result_1.LocatorTestResult(parsedJson.replayable, parsedJson.id);
}
reviveBrowserActionTestResult(parsedJson) {
return new browser_action_test_result_1.BrowserActionTestResult(this.reviveBrowser(parsedJson.browser), parsedJson.replayable, parsedJson.id);
}
reviveHtmlElementActionTestResult(parsedJson) {
return new html_element_action_test_result_1.HtmlElementActionTestResult(this.reviveBrowser(parsedJson.browser), parsedJson.id);
}
reviveBrowser(parsedJson) {
switch (parsedJson.className) {
case 'Chrome':
return new chrome_1.Chrome(parsedJson.name, parsedJson.width, parsedJson.height, parsedJson.sleepMsBetweenActions, parsedJson.id);
case 'Edge':
return new edge_1.Edge(parsedJson.name, parsedJson.width, parsedJson.height, parsedJson.sleepMsBetweenActions, parsedJson.id);
case 'Firefox':
return new firefox_1.Firefox(parsedJson.name, parsedJson.width, parsedJson.height, parsedJson.sleepMsBetweenActions, parsedJson.id);
case 'InternetExplorer':
return new internet_explorer_1.InternetExplorer(parsedJson.name, parsedJson.width, parsedJson.height, parsedJson.sleepMsBetweenActions, parsedJson.id);
}
throw new Error('Could not construct Browser from ChRec JSON!');
}
}
exports.ChrecJsonService = ChrecJsonService;