Skip to content

Commit

Permalink
DEVPROD-5904: Mock clipboard usage in Cypress (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
minnakt authored May 14, 2024
1 parent cef5220 commit 1391839
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 43 deletions.
11 changes: 4 additions & 7 deletions apps/parsley/cypress/integration/ansiLogs/ansi_logView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ describe("Bookmarking and selecting lines", () => {
"[2022/03/02 17:05:21.050] running setup group because we have a new independent task";

cy.toggleDetailsPanel(true);
// Need to fire a real click here because the copy to clipboard
cy.dataCy("jira-button").realClick();
cy.dataCy("jira-button").click();
cy.assertValueCopiedToClipboard(
`{noformat}\n${logLine0}\n...\n${logLine10}\n${logLine11}\n...\n${logLine297}\n{noformat}`,
);
Expand Down Expand Up @@ -225,20 +224,18 @@ describe("Sharing lines", () => {
cy.dataCy("line-index-2").click({ shiftKey: true });
cy.dataCy("sharing-menu").should("be.visible");
cy.contains("Copy selected contents").should("be.visible");
// Need to fire a real click here because the copy to clipboard
cy.contains("Copy selected contents").realClick();
cy.contains("Copy selected contents").click();
cy.validateToast("success", "Copied 2 lines to clipboard", true);
cy.assertValueCopiedToClipboard(
`{noformat}\n[2022/03/02 17:01:58.587] Starting task spruce_ubuntu1604_test_2c9056df66d42fb1908d52eed096750a91f1f089_22_03_02_16_45_12, execution 0.\n[2022/03/02 17:01:58.701] Running pre-task commands.\n{noformat}`,
"{noformat}\n[2022/03/02 17:01:58.587] Starting task spruce_ubuntu1604_test_2c9056df66d42fb1908d52eed096750a91f1f089_22_03_02_16_45_12, execution 0.\n[2022/03/02 17:01:58.701] Running pre-task commands.\n{noformat}",
);
});
it("should be able to copy a link to the selected lines", () => {
cy.dataCy("line-index-1").click();
cy.dataCy("line-index-2").click({ shiftKey: true });
cy.dataCy("sharing-menu").should("be.visible");
cy.contains("Copy share link to selected lines").should("be.visible");
// Need to fire a real click here because the copy to clipboard
cy.contains("Copy share link to selected lines").realClick();
cy.contains("Copy share link to selected lines").click();
cy.validateToast("success", "Copied link to clipboard", true);
cy.assertValueCopiedToClipboard(
"http://localhost:4173/evergreen/spruce_ubuntu1604_test_2c9056df66d42fb1908d52eed096750a91f1f089_22_03_02_16_45_12/0/task?bookmarks=0%2C297&selectedLineRange=L1-L2&shareLine=1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ describe("Bookmarking and selecting lines", () => {
const logLine1638 = `[ContinuousStepdown:job0] Pausing the stepdown thread.`;

cy.dataCy("details-button").click();
// Need to fire a real click here because the copy to clipboard
cy.dataCy("jira-button").realClick();
cy.dataCy("jira-button").click();
cy.assertValueCopiedToClipboard(
`{noformat}\n${logLine0}\n...\n${logLine10}\n${logLine11}\n...\n${logLine1638}\n{noformat}`,
);
Expand Down Expand Up @@ -288,20 +287,18 @@ describe("Sharing lines", () => {
cy.dataCy("line-index-2").click({ shiftKey: true });
cy.dataCy("sharing-menu").should("be.visible");
cy.contains("Copy selected contents").should("be.visible");
// Need to fire a real click here because the copy to clipboard
cy.contains("Copy selected contents").realClick();
cy.contains("Copy selected contents").click();
cy.validateToast("success", "Copied 2 lines to clipboard", true);
cy.assertValueCopiedToClipboard(
`{noformat}\n+------------------------------------------+--------+-----+-----+\n|full_name |name |port |pid |\n{noformat}`,
"{noformat}\n+------------------------------------------+--------+-----+-----+\n|full_name |name |port |pid |\n{noformat}",
);
});
it("should be able to copy a link to the selected lines", () => {
cy.dataCy("line-index-1").click();
cy.dataCy("line-index-2").click({ shiftKey: true });
cy.dataCy("sharing-menu").should("be.visible");
cy.contains("Copy share link to selected lines").should("be.visible");
// Need to fire a real click here because the copy to clipboard
cy.contains("Copy share link to selected lines").realClick();
cy.contains("Copy share link to selected lines").click();
cy.validateToast("success", "Copied link to clipboard", true);
cy.assertValueCopiedToClipboard(
"http://localhost:4173/test/mongodb_mongo_master_rhel80_debug_v4ubsan_all_feature_flags_experimental_concurrency_sharded_with_stepdowns_and_balancer_4_linux_enterprise_361789ed8a613a2dc0335a821ead0ab6205fbdaa_22_09_21_02_53_24/0/1716e11b4f8a4541c5e2faf70affbfab?bookmarks=0%2C12568&selectedLineRange=L1-L2&shareLine=1",
Expand Down
11 changes: 4 additions & 7 deletions apps/parsley/cypress/integration/resmokeLogs/resmoke_logView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ describe("Bookmarking and selecting lines", () => {
const logLine11079 = `[j0:s1] | 2022-09-21T12:50:28.489+00:00 I NETWORK 22944 [conn60] "Connection ended","attr":{"remote":"127.0.0.1:47362","uuid":{"uuid":{"$uuid":"b28d7d9f-03b6-4f93-a7cd-5e1948135f69"}},"connectionId":60,"connectionCount":2}`;

cy.dataCy("details-button").click();
// Need to fire a real click here because the copy to clipboard
cy.dataCy("jira-button").realClick();
cy.dataCy("jira-button").click();
cy.assertValueCopiedToClipboard(
`{noformat}\n${logLine0}\n...\n${logLine10}\n${logLine11}\n...\n${logLine11079}\n{noformat}`,
);
Expand Down Expand Up @@ -288,20 +287,18 @@ describe("Sharing lines", () => {
cy.dataCy("line-index-2").click({ shiftKey: true });
cy.dataCy("sharing-menu").should("be.visible");
cy.contains("Copy selected contents").should("be.visible");
// Need to fire a real click here because the copy to clipboard
cy.contains("Copy selected contents").realClick();
cy.contains("Copy selected contents").click();
cy.validateToast("success", "Copied 2 lines to clipboard", true);
cy.assertValueCopiedToClipboard(
`{noformat}\n+------------------------------------------+--------+-----+-----+\n|full_name |name |port |pid |\n{noformat}`,
"{noformat}\n+------------------------------------------+--------+-----+-----+\n|full_name |name |port |pid |\n{noformat}",
);
});
it("should be able to copy a link to the selected lines", () => {
cy.dataCy("line-index-1").click();
cy.dataCy("line-index-2").click({ shiftKey: true });
cy.dataCy("sharing-menu").should("be.visible");
cy.contains("Copy share link to selected lines").should("be.visible");
// Need to fire a real click here because the copy to clipboard
cy.contains("Copy share link to selected lines").realClick();
cy.contains("Copy share link to selected lines").click();
cy.validateToast("success", "Copied link to clipboard", true);
cy.assertValueCopiedToClipboard(
"http://localhost:4173/resmoke/7e208050e166b1a9025c817b67eee48d/test/1716e11b4f8a4541c5e2faf70affbfab?bookmarks=0%2C11079&selectedLineRange=L1-L2&shareLine=1",
Expand Down
23 changes: 14 additions & 9 deletions apps/parsley/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Cypress.Commands.add("addSearch", (search: string) => {
cy.dataCy("searchbar-input").type(`${search}`);
});

Cypress.Commands.add("assertValueCopiedToClipboard", (value: string) => {
cy.get("@writeText").should("have.been.calledOnceWith", value);
});

Cypress.Commands.add("clearBounds", () => {
cy.toggleDetailsPanel(true);

Expand Down Expand Up @@ -202,13 +206,14 @@ Cypress.Commands.add(
},
);

Cypress.Commands.add("assertValueCopiedToClipboard", (value: string) => {
cy.window().then((win) => {
win.navigator.clipboard.readText().then((text) => {
expect(text).to.eq(value);
});
});
// This wait is necessary to ensure the clipboard has time to be read
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(50);
Cypress.Commands.overwrite("visit", (originalVisit, url, options = {}) => {
const opts = {
onBeforeLoad(win: Window): void {
// Mock clipboard API.
cy.spy(win.navigator.clipboard, "writeText").as("writeText");
},
...options,
};
// @ts-ignore - TypeScript detects the wrong definition for the original function.
return originalVisit(url, opts);
});
11 changes: 5 additions & 6 deletions apps/parsley/cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "cypress-real-events";
import "./commands";

declare global {
Expand All @@ -19,6 +18,11 @@ declare global {
* @example cy.addSearch('mySearch')
*/
addSearch(search: string): void;
/**
* Custom command to validate that a value was copied to the clipboard.
* @example cy.assertValueCopiedToClipboard("This is some text")
*/
assertValueCopiedToClipboard(text: string): void;
/**
* Custom command to click one of the toggles in the Details Menu panel.
* @param toggleDataCy The data-cy attribute of the toggle to click.
Expand Down Expand Up @@ -99,11 +103,6 @@ declare global {
message?: string,
shouldClose?: boolean,
): void;
/**
* Custom command to validate that a value was copied to the clipboard.
* @example cy.assertValueCopiedToClipboard("This is some text")
*/
assertValueCopiedToClipboard(text: string): void;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/parsley/cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"compilerOptions": {
"target": "ES5",
"lib": ["ES5", "DOM"],
"types": ["cypress", "node", "cypress-real-events"],
"types": ["cypress", "node"],
},
"include": ["**/*.ts"]
}
1 change: 0 additions & 1 deletion apps/parsley/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
"@vitejs/plugin-react": "3.0.1",
"babel-plugin-import-graphql": "2.8.1",
"cypress": "12.7.0",
"cypress-real-events": "1.11.0",
"env-cmd": "10.1.0",
"eslint": "8.56.0",
"eslint-config-evg": "*",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8482,11 +8482,6 @@ csstype@^3.0.2:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.3.tgz#d80ff294d114fb0e6ac500fbf85b60137d7eff81"
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==

[email protected]:
version "1.11.0"
resolved "https://registry.yarnpkg.com/cypress-real-events/-/cypress-real-events-1.11.0.tgz#292fe5281c5b6e955524e766ab7fec46930c7763"
integrity sha512-4LXVRsyq+xBh5TmlEyO1ojtBXtN7xw720Pwb9rEE9rkJuXmeH3VyoR1GGayMGr+Itqf11eEjfDewtDmcx6PWPQ==

[email protected]:
version "12.17.1"
resolved "https://registry.yarnpkg.com/cypress/-/cypress-12.17.1.tgz#777fdcceec4ecd642fc90795f5994853b6ea03f8"
Expand Down

0 comments on commit 1391839

Please sign in to comment.