Skip to content

fix(selectors): handle when there are a unique selector #63

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/CypressStringifyExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ function formatAsJSLiteral(value: string) {
}

function filterArrayByString(selectors: Schema.Selector[], value: string) {
return selectors.filter((selector) =>
value === 'aria/'
? !selector[0].includes(value)
: selector[0].includes(value)
return selectors.filter((selector) => {
let userSelector = Array.isArray(selector) ? selector[0] : selector;
return value === 'aria/'
? !userSelector.includes(value)
: userSelector.includes(value);
}
);
}

Expand All @@ -187,7 +189,11 @@ function handleSelectors(
if (preferredSelector && preferredSelector[0]) {
return `cy.get(${formatAsJSLiteral(preferredSelector[0][0])})`;
} else {
return `cy.get(${formatAsJSLiteral(nonAriaSelectors[0][0])})`;
if (Array.isArray(nonAriaSelectors[0])) {
return `cy.get(${formatAsJSLiteral(nonAriaSelectors[0][0])})`;
}

return `cy.get(${formatAsJSLiteral(nonAriaSelectors[0])})`;
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/CypressStringifyExtension_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,39 @@ describe("click step", () => {
});
assert.equal(result.toString(), `cy.get("#test").trigger("mouseover");\n`);
});

it('correctly handle when there are a unique selector on a any kind of event', async function () {
const step = {
type: StepType.Click as const,
target: 'main',
selectors: ['div:nth-of-type(1)'],
offsetX: 1,
offsetY: 1,
};

const result = await stringifyStep(step, {
extension,
});

assert.equal(result.toString(), `cy.get("div:nth-of-type(1)").click();\n`);
});

it('correctly skip aria selectors when there are an array of selectors on any kind of event', async function () {
const step = {
type: StepType.Click as const,
target: 'main',
selectors: [
"aria/npm install cypress",
"main button",
],
offsetX: 1,
offsetY: 1,
};

const result = await stringifyStep(step, {
extension,
});

assert.equal(result.toString(), `cy.get("main button").click();\n`);
});
});