Skip to content

Commit

Permalink
✅ Add tests for all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Apr 16, 2020
1 parent 03e8e1d commit 34c6743
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/commands/files.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { puppet } from "../";
import { saveToFile } from "./files";
import { readFile, unlink } from "fs-extra";
import { join } from "path";
jest.setTimeout(30000);

describe("puppet - files", () => {
it("saves to file", async () => {
await saveToFile("save to example.txt", {} as any, "Hello, world!");
const txt = await readFile(join(".", "example.txt"), "utf8");
expect(txt).toBe("Hello, world!");
});
it("gets HTML and saves", async () => {
await puppet([
"go to example.com",
"get page html",
"save to example.html",
]);
const txt = await readFile(join(".", "example.html"), "utf8");
expect(txt.includes("Example Domain")).toBeTruthy();
});
afterAll(async () => {
await unlink(join(".", "example.txt"));
await unlink(join(".", "example.html"));
});
});
53 changes: 53 additions & 0 deletions src/commands/navigation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { puppet } from "../";
import { launch } from "puppeteer";
import { navigateTo } from "./navigation";
jest.setTimeout(30000);

describe("puppet - HTML", () => {
it("navigate to page", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = (await navigateTo("navigate to example.org", page, "")) || {
url: () => "",
};
await browser.close();
expect(result.url()).toBe("http://example.org/");
});
it("go to url", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = (await navigateTo(
"go to url http://example.org",
page,
""
)) || {
url: () => "",
};
await browser.close();
expect(result.url()).toBe("http://example.org/");
});
it("open page", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = (await navigateTo("open example.org", page, "")) || {
url: () => "",
};
await browser.close();
expect(result.url()).toBe("http://example.org/");
});
it("go to page", async () => {
const result = await puppet(["go to example.com"]);
expect(result.url).toBe("http://example.com/");
});
it("navigate to page", async () => {
const result = await puppet(["navigate to example.com"]);
expect(result.url).toBe("http://example.com/");
});
it("open url", async () => {
const result = await puppet(["open http://example.com"]);
expect(result.url).toBe("http://example.com/");
});
});
130 changes: 130 additions & 0 deletions src/commands/save-page-as.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { puppet } from "../";
import { launch } from "puppeteer";
import { screenshot, saveAsPdf, saveAsHtml } from "./save-page-as";
import { readFile, unlink } from "fs-extra";
import { join } from "path";
jest.setTimeout(30000);

describe("puppet - HTML", () => {
it("saveAsHtml", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = await saveAsHtml("save as HTML", page, "");
await browser.close();
expect(result.length).toBeGreaterThan(1000);
});
it("download page HTML", async () => {
await puppet(["go to example.com", "save as HTML", "save to basic.html"]);
const file = await readFile(join(".", "basic.html"));
expect(file).toBeDefined();
expect(file.length).toBeGreaterThan(100);
});
});

describe("puppet - PDF", () => {
it("saveAsPdf", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = await saveAsPdf("save as PDF", page, "");
await browser.close();
expect(result.length).toBeGreaterThan(1000);
});
it("create a PDF", async () => {
await puppet(["go to example.com", "save as PDF", "save to basic.pdf"]);
const file = await readFile(join(".", "basic.pdf"));
expect(file).toBeDefined();
expect(file.length).toBeGreaterThan(1);
});
});

describe("puppet - screenshot", () => {
it("screenshot", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = await screenshot("take a screenshot", page, "");
await browser.close();
expect(result.length).toBeGreaterThan(1000);
});
it("JPEG screenshot", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = await screenshot("take a JPEG screenshot", page, "");
await browser.close();
expect(result.length).toBeGreaterThan(1000);
});
it("full screenshot", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = await screenshot("take a full screenshot", page, "");
await browser.close();
expect(result.length).toBeGreaterThan(1000);
});
it("transparent screenshot", async () => {
const browser = await launch();
const page = await browser.newPage();
await page.goto("http://example.com");
const result = await screenshot("take a transparent screenshot", page, "");
await browser.close();
expect(result.length).toBeGreaterThan(1000);
});
it("take basic screenshot", async () => {
await puppet([
"go to example.com",
"take a screenshot",
"save to basic.png",
]);
const file = await readFile(join(".", "basic.png"));
expect(file).toBeDefined();
expect(file.length).toBeGreaterThan(1);
});
it("take JPEG screenshot", async () => {
await puppet([
"go to example.com",
"take a JPG screenshot",
"save to basic.jpeg",
]);
const file = await readFile(join(".", "basic.jpeg"));
expect(file).toBeDefined();
expect(file.length).toBeGreaterThan(1);
});
it("take full page screenshot", async () => {
await puppet([
"go to example.com",
"take a full screenshot",
"save to full.png",
]);
const file = await readFile(join(".", "full.png"));
expect(file).toBeDefined();
});
it("take transparent screenshot", async () => {
await puppet([
"go to example.com",
"take a transparent screenshot",
"save to transparent.png",
]);
const file = await readFile(join(".", "transparent.png"));
expect(file).toBeDefined();
});
it("take background-less screenshot", async () => {
await puppet([
"go to example.com",
"take a screenshot, omit background",
"save to transparent.png",
]);
const file = await readFile(join(".", "transparent.png"));
expect(file).toBeDefined();
});
afterAll(async () => {
await unlink(join(".", "basic.pdf"));
await unlink(join(".", "basic.html"));
await unlink(join(".", "basic.png"));
await unlink(join(".", "basic.jpeg"));
await unlink(join(".", "full.png"));
await unlink(join(".", "transparent.png"));
});
});
9 changes: 5 additions & 4 deletions src/commands/save-page-as.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export const screenshot = async (
fullPage: command.includes("full"),
type: command.includes("jpg") || command.includes("jpeg") ? "jpeg" : "png",
omitBackground:
command.includes("background") &&
(command.includes("remove") ||
command.includes("without") ||
command.includes("omit")),
command.includes("transparent") ||
(command.includes("background") &&
(command.includes("remove") ||
command.includes("without") ||
command.includes("omit"))),
});
complete("Took a screenshot");
return shot;
Expand Down
20 changes: 20 additions & 0 deletions src/commands/timers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { puppet } from "../";
import { waitForTime } from "./timers";
jest.setTimeout(30000);

describe("puppet - timers", () => {
it("saves to file", async () => {
const now = new Date().getTime();
await waitForTime("wait for 1 second", {} as any, "");
expect(new Date().getTime() - now).toBeGreaterThanOrEqual(1000);
});
it("waits in puppet", async () => {
const now = new Date().getTime();
await puppet([
"go to example.com",
"wait for 1 second",
"go to example.org",
]);
expect(new Date().getTime() - now).toBeGreaterThanOrEqual(1000);
});
});
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ const _puppet = async (commands: string[]) => {
const _command = async (command: string, page: Page, lastResult: any) => {
debug("Running command", command);

if (command.startsWith("go") || command.startsWith("navigation"))
if (
command.startsWith("go") ||
command.startsWith("open") ||
command.startsWith("navigate")
)
return navigateTo(command, page, lastResult);

if (
Expand Down

0 comments on commit 34c6743

Please sign in to comment.