Skip to content
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

Add bindings for CDPSession and Target #43

Merged
merged 8 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
126 changes: 122 additions & 4 deletions __tests__/puppeteer_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ describe("BrowserFetcher", () => {
test("revisionInfo", () => {
let revisionInfo =
browserFetcher^ |> BrowserFetcher.revisionInfo(_, "533271");
revisionInfo##revision |> expect |> toBe("533271");
revisionInfo##executablePath |> expect |> toContainString("chromium");
revisionInfo##folderPath |> expect |> toContainString("chromium");
revisionInfo##local |> expect |> toBe(Js.true_);
revisionInfo##revision |> expect |> toBe("533271") |> ignore;
revisionInfo##executablePath
|> expect
|> toContainString("chromium")
|> ignore;
revisionInfo##folderPath
|> expect
|> toContainString("chromium")
|> ignore;
revisionInfo##local |> expect |> toBe(Js.true_) |> ignore;
revisionInfo##url
|> expect
|> toContainString("https://storage.googleapis.com/");
Expand Down Expand Up @@ -592,6 +598,9 @@ describe("Page", () => {
)
)
);
test("target()", () =>
page^ |> Page.target |> Target.url |> expect |> toBe("about:blank")
);
afterAllPromise(() =>
Js.Promise.(Page.close(page^) |> then_(() => Browser.close(browser^)))
);
Expand Down Expand Up @@ -642,3 +651,112 @@ describe("ElementHandle", () => {
Js.Promise.(Page.close(page^) |> then_(() => Browser.close(browser^)))
);
});

describe("Target", () => {
let browser = ref(Browser.empty());
let target = ref(Target.empty());
beforeAllPromise(() =>
Js.Promise.(
Puppeteer.launch(~options=noSandbox, ())
|> then_(res => {
browser := res;
res |> Browser.newPage;
})
|> then_(page => {
target := page |> Page.target;
target |> resolve;
})
)
);
testPromise("page", () =>
Js.Promise.(
target^
|> Target.page
|> then_(page => page |> expect |> ExpectJs.toBeTruthy |> resolve)
)
);
test("type", () =>
(
switch (target^ |> Target.type_) {
| Some(t) => t === `page
| None => false
}
)
|> expect
|> toBe(true)
);
test("url", () =>
target^ |> Target.url |> expect |> toBe("about:blank")
);
testPromise("createCDPSession", () =>
Js.Promise.(
target^
|> Target.createCDPSession
|> then_(session => session |> expect |> ExpectJs.toBeTruthy |> resolve)
)
);
afterAllPromise(() => browser^ |> Browser.close);
});

describe("CDPSession", () => {
let browser = ref(Browser.empty());
beforeAllPromise(() =>
Js.Promise.(
Puppeteer.launch(~options=noSandbox, ())
|> then_(res => {
browser := res;
res |> resolve;
})
)
);
testPromise("detach", () =>
Js.Promise.(
browser^
|> Browser.newPage
|> then_(page => page |> Page.target |> Target.createCDPSession)
|> then_(session =>
session
|> CDPSession.detach
|> then_(() =>
CDPSession.send(
session,
"Animation.getPlaybackRate",
Js.Obj.empty(),
)
)
|> then_(_res =>
failwith(
"expect with exception: Error: Protocol error (Animation.getPlaybackRate): Session closed. Most likely the page has been closed.",
)
|> resolve
)
|> catch(_err => pass |> resolve)
)
)
);
testPromise("send", () =>
Js.Promise.(
browser^
|> Browser.newPage
|> then_(page => page |> Page.target |> Target.createCDPSession)
|> then_(session =>
CDPSession.send(
session,
"Animation.setPlaybackRate",
{"playbackRate": 3.1415926535},
)
|> then_(_res =>
CDPSession.send(
session,
"Animation.getPlaybackRate",
Js.Obj.empty(),
)
)
|> then_(res =>
res##playbackRate |> expect |> toBe(3.1415926535) |> resolve
)
)
)
);
afterAllPromise(() => browser^ |> Browser.close);
});
78 changes: 78 additions & 0 deletions lib/js/__tests__/puppeteer_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/js/src/CDPSession.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 39 additions & 1 deletion lib/js/src/Target.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/CDPSession.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type t;

[@bs.send] external detach : t => Js.Promise.t(unit) = "";

[@bs.send]
external send : (t, string, Js.t({..})) => Js.Promise.t(Js.t({..})) = "";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The third argument params is optional.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit a980bc6

2 changes: 1 addition & 1 deletion src/Page.re
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ external setUserAgent : (~userAgent: string) => Js.Promise.t(unit) = "";
[@bs.send.pipe: t]
external tap : (~selector: string) => Js.Promise.t(unit) = "";

[@bs.send] external target : t => Target.t = "";
[@bs.send] external target : t => Types.target = "";

[@bs.send] external title : t => Js.Promise.t(string) = "";

Expand Down
17 changes: 16 additions & 1 deletion src/Target.re
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
type t;
type t = Types.target;

external empty : unit => t = "%identity";

[@bs.deriving jsConverter]
type pageType = [ | `page | `service_worker | `browser | `other];

[@bs.send] external createCDPSession : t => Js.Promise.t(CDPSession.t) = "";

[@bs.send] external page : t => Js.Promise.t(Js.Nullable.t(Page.t)) = "";

[@bs.send] external typeString : t => string = "type";

let type_ = t => t |> typeString |> pageTypeFromJs;

[@bs.send] external url : t => string = "";
2 changes: 2 additions & 0 deletions src/Types.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ type request;
type response;

type frameBase;

type target;