Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Get window size #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions api/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ func (w *Window) SetSize(width, height int) error {

return w.Send("POST", "size", request, nil)
}

func (w *Window) GetSize() (width int, height int, err error) {
request := struct {
Copy link
Owner

Choose a reason for hiding this comment

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

This struct does not represent a request

Width int `json:"width"`
Height int `json:"height"`
}{}


Copy link
Owner

Choose a reason for hiding this comment

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

Remove newline

if err := w.Send("GET", "size", nil, &request); err != nil {
return 0, 0 , err
}

return request.Width, request.Height, nil
}
19 changes: 19 additions & 0 deletions api/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ var _ = Describe("Window", func() {
})
})
})

Describe("#GetSize", func() {
It("should successfully send a GET request to the size endpoint", func() {
width, height, err := window.GetSize()
Copy link
Owner

Choose a reason for hiding this comment

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

Setup and test return values using the mock bus (ideally in a separate test)

Expect(err).To(Succeed())
Expect(width).To(BeNumerically(">=", 0))
Expect(height).To(BeNumerically(">=", 0))
Expect(bus.SendCall.Method).To(Equal("GET"))
Expect(bus.SendCall.Endpoint).To(Equal("window/some-id/size"))
})

Context("when the bus indicates a failure", func() {
It("should return an error", func() {
bus.SendCall.Err = errors.New("some error")
_, _, err := window.GetSize()
Expect(err).To(MatchError("some error"))
})
})
})
})
14 changes: 14 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ func (p *Page) Size(width, height int) error {
return nil
}

// GetSize gets the current page size in pixels.
func (p *Page) GetSize() (width int, height int, err error) {
window, err := p.session.GetWindow()
if err != nil {
return 0, 0, fmt.Errorf("failed to retrieve window: %s", err)
}

if width, height, err = window.GetSize(); err != nil {
return 0, 0, fmt.Errorf("failed to get window size: %s", err)
}

return width, height, nil
}

// Screenshot takes a screenshot and saves it to the provided filename.
// The provided filename may be an absolute or relative path.
func (p *Page) Screenshot(filename string) error {
Expand Down
39 changes: 38 additions & 1 deletion page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ var _ = Describe("Page", func() {
})
})

Context("when the window fails to retrieve its size", func() {
Context("when the window fails to set its size", func() {
It("should return an error", func() {
session.GetWindowCall.ReturnWindow = window
bus.SendCall.Err = errors.New("some error")
Expand All @@ -345,6 +345,43 @@ var _ = Describe("Page", func() {
})
})

Describe("#GetSize", func() {
var (
bus *mocks.Bus
window *api.Window
)

BeforeEach(func() {
bus = &mocks.Bus{}
window = &api.Window{Session: &api.Session{Bus: bus}}
})

It("should get the window width and height", func() {
session.GetWindowCall.ReturnWindow = window
width, height, err := page.GetSize()
Expect(err).To(Succeed())
Expect(width).To(BeNumerically(">=", 0))
Copy link
Owner

Choose a reason for hiding this comment

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

Setup return values using the mock bus

Expect(height).To(BeNumerically(">=", 0))
})

Context("when the session fails to retrieve a window", func() {
It("should return an error", func() {
session.GetWindowCall.Err = errors.New("some error")
_, _, err := page.GetSize()
Expect(err).To(MatchError("failed to retrieve window: some error"))
})
})

Context("when the window fails to retrieve its size", func() {
It("should return an error", func() {
session.GetWindowCall.ReturnWindow = window
bus.SendCall.Err = errors.New("some error")
_, _, err := page.GetSize()
Expect(err).To(MatchError("failed to get window size: some error"))
})
})
})

Describe("#Screenshot", func() {
It("should successfully saves the screenshot", func() {
session.GetScreenshotCall.ReturnImage = []byte("some-image")
Expand Down