Skip to content

Commit

Permalink
Merge pull request #87 from ShellyDCMS/Personal/Nayana/Fixed-Non-Exis…
Browse files Browse the repository at this point in the history
…intg-Element-Issue

Fixed issue of non existence element
  • Loading branch information
neneplusdev authored May 27, 2024
2 parents 5ea94f0 + 630a5de commit ce86525
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@shellygo/cypress-test-utils",
"description": "Cypress Test Automation Utilities",
"version": "2.1.5",
"version": "2.1.6",
"author": "Shelly Goldblit",
"private": false,
"license": "MIT",
Expand Down
8 changes: 8 additions & 0 deletions src/cypress-helper.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ describe("cypress helper tests", () => {
then(get.elementByText("My first paragraph")).shouldExist();
});

it("Non existance slot", () => {
then(get.elementByTestId("non-existance")).shouldNotExist();
});

it("Test non-existing element", () => {
then(get.elementByTestId("non-existing-element")).shouldNotExist();
});

it("should have text", () => {
then(get.elementByTestId("div-with-span")).shouldHaveText("Text");
});
Expand Down
33 changes: 23 additions & 10 deletions src/cypress-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ export class CypressHelper {
return checkFunction();
};

private getShadowSlotElement = (dataTestID: string, index: number | undefined) =>
this.get.nthBySelector(dataTestID, index).then(slot =>
cy.wrap(
Cypress.$(slot as JQuery<HTMLSlotElement>)
.get(0)
.assignedNodes()[0].parentElement!
)
);

private shouldHandleShadowDomSlot = (dataTestID: string) =>
this.options.handleSlotShadowDOM && dataTestID.endsWith(`-${this.options.shadowSlotSuffix}`)

beforeAndAfter = () => {
before(() => {
chai.use(chaiSubset);
Expand Down Expand Up @@ -813,16 +825,17 @@ export class CypressHelper {
* @param [index]
*/
elementByTestId: (dataTestID: string, index?: number) =>
this.options.handleSlotShadowDOM &&
dataTestID.endsWith(`-${this.options.shadowSlotSuffix}`)
? this.get.nthBySelector(dataTestID, index).then(slot =>
cy.wrap(
Cypress.$(slot as JQuery<HTMLSlotElement>)
.get(0)
.assignedNodes()[0].parentElement!
)
)
: this.get.nthBySelector(dataTestID, index),
this.get
.nthBySelector(dataTestID, index)
.should(_ => { })
.then(elements => {
if (elements.length === 0) {
return this.get.nthBySelector(dataTestID, index)
}
return this.shouldHandleShadowDomSlot(dataTestID)
? this.getShadowSlotElement(dataTestID, index)
: this.get.nthBySelector(dataTestID, index)
}),
/**
* Get the element currently focused in the document.
* @returns {Cypress.Chainable<JQuery<HTMLElement>>}
Expand Down

0 comments on commit ce86525

Please sign in to comment.