generated from AnandChowdhary/node.ts
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
03e8e1d
commit 34c6743
Showing
6 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters