Skip to content

Commit

Permalink
Add WebPage.ViewportSize().
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjohnson committed Apr 9, 2017
1 parent a7d11f1 commit d8ba130
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
38 changes: 33 additions & 5 deletions phantomjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,19 @@ func (p *WebPage) URL() string {
return resp.Value
}

func (p *WebPage) ViewportSize() string {
panic("TODO")
// ViewportSize returns the size of the viewport on the browser.
func (p *WebPage) ViewportSize() (width, height int) {
var resp struct {
Width int `json:"width"`
Height int `json:"height"`
}
p.ref.process.mustDoJSON("POST", "/webpage/ViewportSize", map[string]interface{}{"ref": p.ref.id}, &resp)
return resp.Width, resp.Height
}

// SetViewportSize sets the size of the viewport.
func (p *WebPage) SetViewportSize(width, height int) {
p.ref.process.mustDoJSON("POST", "/webpage/SetViewportSize", map[string]interface{}{"ref": p.ref.id, "width": width, "height": height}, nil)
}

func (p *WebPage) WindowName() string {
Expand Down Expand Up @@ -677,15 +688,15 @@ func (p *WebPage) Go() {
panic("TODO")
}

func (p *WebPage) IncludeJs() {
func (p *WebPage) IncludeJS() {
panic("TODO")
}

func (p *WebPage) InjectJs() {
func (p *WebPage) InjectJS() {
panic("TODO")
}

func (p *WebPage) OpenUrl() {
func (p *WebPage) OpenURL() {
panic("TODO")
}

Expand Down Expand Up @@ -1009,6 +1020,8 @@ server.listen(system.env["PORT"], function(request, response) {
case '/webpage/SetSettings': return handleWebpageSetSettings(request, response);
case '/webpage/Title': return handleWebpageTitle(request, response);
case '/webpage/URL': return handleWebpageURL(request, response);
case '/webpage/ViewportSize': return handleWebpageViewportSize(request, response);
case '/webpage/SetViewportSize': return handleWebpageSetViewportSize(request, response);
case '/webpage/SwitchToFrameName': return handleWebpageSwitchToFrameName(request, response);
case '/webpage/SwitchToFramePosition': return handleWebpageSwitchToFramePosition(request, response);
Expand Down Expand Up @@ -1287,6 +1300,21 @@ function handleWebpageURL(request, response) {
response.closeGracefully();
}
function handleWebpageViewportSize(request, response) {
var page = ref(JSON.parse(request.post).ref);
var viewport = page.viewportSize;
response.write(JSON.stringify({width: viewport.width, height: viewport.height}));
response.closeGracefully();
}
function handleWebpageSetViewportSize(request, response) {
var msg = JSON.parse(request.post);
var page = ref(msg.ref);
page.viewportSize = {width: msg.width, height: msg.height};
response.closeGracefully();
}
function handleWebpageSwitchToFrameName(request, response) {
var msg = JSON.parse(request.post);
Expand Down
17 changes: 16 additions & 1 deletion phantomjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,22 @@ func TestWebPage_Title(t *testing.T) {
// Set & verify title.
page.SetContent(`<html><head><title>FOO</title></head><body>BAR</body></html>`)
if v := page.Title(); v != `FOO` {
t.Fatalf("unexpected title: %s", v)
t.Fatalf("unexpected plain text: %s", v)
}
}

// Ensure process can set and retrieve the viewport size.
func TestWebPage_ViewportSize(t *testing.T) {
p := MustOpenNewProcess()
defer p.MustClose()

page := p.CreateWebPage()
defer page.Close()

// Set and verify size.
page.SetViewportSize(100, 200)
if w, h := page.ViewportSize(); w != 100 || h != 200 {
t.Fatalf("unexpected size: w=%d, h=%d", w, h)
}
}

Expand Down

0 comments on commit d8ba130

Please sign in to comment.