@11joselu/cypress-to-playwright
Version:
Automatic migration from cypress to playwright
92 lines • 5.04 kB
JavaScript
import ts from 'typescript';
import { isCy } from '../is/is-cy.js';
import { fixString } from './fix-string.js';
export function handle(expressionName, propertyExpression, factory) {
if (isCy.click(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "click" /* LOCATOR_PROPERTIES.CLICK */);
}
if (isCy.type(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "type" /* LOCATOR_PROPERTIES.TYPE */);
}
if (isCy.check(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "check" /* LOCATOR_PROPERTIES.CHECK */);
}
if (isCy.uncheck(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "uncheck" /* LOCATOR_PROPERTIES.UNCHECK */);
}
if (isCy.select(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "selectOption" /* LOCATOR_PROPERTIES.SELECT */);
}
if (isCy.scrollTo(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "scroll" /* LOCATOR_PROPERTIES.SCROLL_TO */);
}
if (isCy.scrollIntoView(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "scrollIntoViewIfNeeded" /* LOCATOR_PROPERTIES.SCROLL_INTO_VIEW */);
}
if (isCy.dblclick(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "dblclick" /* LOCATOR_PROPERTIES.DBL_CLICK */);
}
if (isCy.clear(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "fill" /* LOCATOR_PROPERTIES.CLEAR */, [factory.string('')]);
}
if (isCy.focus(expressionName)) {
return createPlaywrightCommand(propertyExpression, factory, "focus" /* LOCATOR_PROPERTIES.FOCUS */);
}
return createPlaywrightCommand(propertyExpression, factory, "blur" /* LOCATOR_PROPERTIES.BLUR */);
}
function createPlaywrightCommand(propertyExpression, factory, command, toInjectArgs = []) {
var _a;
const argumentsOfPropertyAccessExpression = getArgumentsOfPropertyAccessExpression(propertyExpression);
const parent = ts.isCallExpression(propertyExpression.parent) ? propertyExpression.parent : null;
const expressionName = getExpressionName(propertyExpression);
const propertyArgs = [...toInjectArgs, ...((_a = parent === null || parent === void 0 ? void 0 : parent.arguments) !== null && _a !== void 0 ? _a : [])];
if (isCy.isFirst(expressionName) || isCy.isLast(expressionName)) {
const foundExpression = findGetPropertyExpression(propertyExpression);
const expression = factory.callExpression(factory.propertyAccessExpression(factory.playwrightLocatorProperty(isCy.isFirst(expressionName) ? "first" /* LOCATOR_PROPERTIES.FIRST */ : "last" /* LOCATOR_PROPERTIES.LAST */, foundExpression.typeArguments, foundExpression.arguments), command), parent === null || parent === void 0 ? void 0 : parent.typeArguments, propertyArgs);
return factory.await(expression);
}
let propertyAccessArguments = argumentsOfPropertyAccessExpression.propertyAccessArguments;
if (isCy.selectByContains(expressionName)) {
propertyAccessArguments = propertyAccessArguments.map((item) => {
if (ts.isStringLiteral(item))
return factory.string(`text=${fixString(item.getText())}`);
return item;
});
}
return factory.await(factory.playwrightLocatorProperty(command, argumentsOfPropertyAccessExpression.propertyTypeAccessArguments, propertyAccessArguments, parent === null || parent === void 0 ? void 0 : parent.typeArguments, propertyArgs));
}
function getArgumentsOfPropertyAccessExpression(propertyAccessExpression) {
const callExpression = propertyAccessExpression.expression;
const propertyTypeAccessArguments = ts.isCallExpression(callExpression) ? callExpression.typeArguments : undefined;
const propertyAccessArguments = ts.isCallExpression(callExpression)
? callExpression.arguments
: [];
return { propertyTypeAccessArguments, propertyAccessArguments };
}
function getExpressionName(expressions) {
return getListOfExpressionName(expressions).reverse().join('.');
}
function findGetPropertyExpression(propertyExpression) {
const expressionName = getExpressionName(propertyExpression);
if (isCy.get(expressionName)) {
return propertyExpression.parent;
}
if (ts.isCallExpression(propertyExpression.expression)) {
return findGetPropertyExpression(propertyExpression.expression.expression);
}
return propertyExpression.parent;
}
function getListOfExpressionName(expression) {
const result = [];
if ('name' in expression) {
result.push(expression.name.escapedText.toString());
}
if ('escapedText' in expression) {
result.push(expression.escapedText);
}
if ('expression' in expression) {
result.push(...getListOfExpressionName(expression.expression));
}
return result;
}
//# sourceMappingURL=actions.js.map