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

Add Screenshot function to Selection. #111

Open
wants to merge 4 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
22 changes: 22 additions & 0 deletions api/element.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/base64"
"errors"
"path"
"strings"
Expand Down Expand Up @@ -145,3 +146,24 @@ func (e *Element) GetLocation() (x, y int, err error) {
func round(number float64) int {
return int(number + 0.5)
}

func (e *Element) GetRect() (x, y, width, height int, err error) {
var rect struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Height float64 `json:"height"`
Width float64 `json:"width"`
}
if err := e.Send("GET", "rect", nil, &rect); err != nil {
return 0, 0, 0, 0, err
}
return round(rect.X), round(rect.Y), round(rect.Width), round(rect.Height), nil
}

func (e *Element) GetScreenshot() ([]byte, error) {
var base64Image string
if err := e.Send("GET", "screenshot", nil, &base64Image); err != nil {
return nil, err
}
return base64.StdEncoding.DecodeString(base64Image)
}
61 changes: 61 additions & 0 deletions api/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,65 @@ var _ = Describe("Element", func() {
})
})
})

Describe("#GetRect", func() {
It("should successfully send a GET request to the rect endpoint", func() {
_, _, _, _, err := element.GetRect()
Expect(err).NotTo(HaveOccurred())
Expect(bus.SendCall.Method).To(Equal("GET"))
Expect(bus.SendCall.Endpoint).To(Equal("element/some-id/rect"))
})

It("should return the rounded rect of the element", func() {
bus.SendCall.Result = `{"x": 100.7, "y": 200, "height": 55.05, "width": 33}`
x, y, width, height, err := element.GetRect()
Expect(err).NotTo(HaveOccurred())
Expect(x).To(Equal(101))
Expect(y).To(Equal(200))
Expect(width).To(Equal(33))
Expect(height).To(Equal(55))
})

Context("when the bus indicates a failure", func() {
It("should return an error indicating the bus failed to retrieve the rect", func() {
bus.SendCall.Err = errors.New("some error")
_, _, _, _, err := element.GetRect()
Expect(err).To(MatchError("some error"))
})
})
})

Describe("#GetScreenshot", func() {
It("should successfully send a GET request to the screenshot endpoint", func() {
_, err := element.GetScreenshot()
Expect(err).NotTo(HaveOccurred())
Expect(bus.SendCall.Method).To(Equal("GET"))
Expect(bus.SendCall.Endpoint).To(Equal("element/some-id/screenshot"))
})

Context("when the image is valid base64", func() {
It("should return the decoded image", func() {
bus.SendCall.Result = `"c29tZS1wbmc="`
image, err := element.GetScreenshot()
Expect(err).NotTo(HaveOccurred())
Expect(string(image)).To(Equal("some-png"))
})
})

Context("when the image is not valid base64", func() {
It("should return an error", func() {
bus.SendCall.Result = `"..."`
_, err := element.GetScreenshot()
Expect(err).To(MatchError("illegal base64 data at input byte 0"))
})
})

Context("when the bus indicates a failure", func() {
It("should return an error", func() {
bus.SendCall.Err = errors.New("some error")
_, err := element.GetScreenshot()
Expect(err).To(MatchError("some error"))
})
})
})
})
13 changes: 8 additions & 5 deletions injector_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package agouti

import "github.com/sclevine/agouti/internal/target"
import (
"github.com/sclevine/agouti/internal/crop"
"github.com/sclevine/agouti/internal/target"
)

func NewTestSelection(session apiSession, elements elementRepository, firstSelector string) *Selection {
func NewTestSelection(session apiSession, elements elementRepository, firstSelector string, cropper crop.Cropper) *Selection {
selector := target.Selector{Type: target.CSS, Value: firstSelector, Single: true}
return &Selection{selectable{session, target.Selectors{selector}}, elements}
return &Selection{selectable{session, target.Selectors{selector}}, elements, cropper}
}

func NewTestMultiSelection(session apiSession, elements elementRepository, firstSelector string) *MultiSelection {
func NewTestMultiSelection(session apiSession, elements elementRepository, firstSelector string, cropper crop.Cropper) *MultiSelection {
selector := target.Selector{Type: target.CSS, Value: firstSelector}
selection := Selection{selectable{session, target.Selectors{selector}}, elements}
selection := Selection{selectable{session, target.Selectors{selector}}, elements, cropper}
return &MultiSelection{selection}
}

Expand Down
20 changes: 20 additions & 0 deletions internal/crop/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Olivier Amblet

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 changes: 4 additions & 0 deletions internal/crop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Crop

This is a modified version of https://github.com/oliamb/cutter.
License is detailed in LICENSE.
62 changes: 62 additions & 0 deletions internal/crop/crop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package crop

import (
"image"
"image/draw"
)

// An interface that is
// image.Image + SubImage method.
type subImageSupported interface {
SubImage(r image.Rectangle) image.Image
}

// Cropper is the interface used to crop images
type Cropper interface {
Crop(img image.Image, width, height int, anchor image.Point) (image.Image, error)
}

// CropperFunc exposes a Crop function that calls itself.
// It implements Cropper.
type CropperFunc func(img image.Image, width, height int, anchor image.Point) (image.Image, error)

// Crop calls the CropperFunc
func (c CropperFunc) Crop(img image.Image, width, height int, anchor image.Point) (image.Image, error) {
return c(img, width, height, anchor)
}

// Crop retrieves an image that is a
// cropped copy of the original img.
func Crop(img image.Image, width, height int, anchor image.Point) (image.Image, error) {
maxBounds := maxBounds(anchor, img.Bounds())
size := computeSize(maxBounds, image.Point{width, height})

Choose a reason for hiding this comment

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

From the logic, there is no need to create maxBounds / computeSize functions.
just need to use
size := image.Point{width, height}

cr := computedCropArea(anchor, img.Bounds(), size)
cr = img.Bounds().Intersect(cr)

if dImg, ok := img.(subImageSupported); ok {
return dImg.SubImage(cr), nil
}
return cropWithCopy(img, cr)
}

func cropWithCopy(img image.Image, cr image.Rectangle) (image.Image, error) {
result := image.NewRGBA(cr)
draw.Draw(result, cr, img, cr.Min, draw.Src)
return result, nil
}

func maxBounds(anchor image.Point, bounds image.Rectangle) image.Rectangle {
return image.Rect(anchor.X, anchor.Y, bounds.Max.X, bounds.Max.Y)
}

// computeSize retrieve the effective size of the cropped image.
func computeSize(bounds image.Rectangle, ratio image.Point) image.Point {
return image.Point{ratio.X, ratio.Y}
}

// computedCropArea retrieve the theorical crop area.
func computedCropArea(anchor image.Point, bounds image.Rectangle, size image.Point) (r image.Rectangle) {
min := bounds.Min
rMin := image.Point{min.X + anchor.X, min.Y + anchor.Y}
return image.Rect(rMin.X, rMin.Y, rMin.X+size.X, rMin.Y+size.Y)
}
13 changes: 13 additions & 0 deletions internal/crop/crop_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package crop_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"testing"
)

func TestCrop(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Crop Suite")
}
34 changes: 34 additions & 0 deletions internal/crop/crop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package crop

import (
"image"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Crop", func() {
It("crops the image", func() {
r, err := Crop(getImage(), 512, 400, image.Point{})
Expect(err).NotTo(HaveOccurred())
Expect(r.Bounds().Dx()).To(Equal(512))
Expect(r.Bounds().Dy()).To(Equal(400))
Expect(r.Bounds().Min.X).To(Equal(0))
Expect(r.Bounds().Min.Y).To(Equal(0))
})

Context("when a different anchor point is used", func() {
It("crops the image", func() {
r, err := Crop(getImage(), 512, 400, image.Point{X: 100, Y: 50})
Expect(err).NotTo(HaveOccurred())
Expect(r.Bounds().Dx()).To(Equal(512))
Expect(r.Bounds().Dy()).To(Equal(400))
Expect(r.Bounds().Min.X).To(Equal(100))
Expect(r.Bounds().Min.Y).To(Equal(50))
})
})
})

func getImage() image.Image {
return image.NewGray(image.Rect(0, 0, 1600, 1437))
}
2 changes: 2 additions & 0 deletions internal/element/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Element interface {
Value(text string) error
Submit() error
GetLocation() (x, y int, err error)
GetRect() (x, y, width, height int, err error)
GetScreenshot() ([]byte, error)
}

func (e *Repository) GetAtLeastOne() ([]Element, error) {
Expand Down
20 changes: 20 additions & 0 deletions internal/mocks/crop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mocks

import "image"

type Cropper struct {
Image image.Image
Width int
Height int
Anchor image.Point
ReturnImage image.Image
Err error
}

func (c *Cropper) Crop(img image.Image, width, height int, anchor image.Point) (image.Image, error) {
c.Image = img
c.Width = width
c.Height = height
c.Anchor = anchor
return c.ReturnImage, c.Err
}
21 changes: 21 additions & 0 deletions internal/mocks/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ type Element struct {
ReturnY int
Err error
}

GetRectCall struct {
ReturnX int
ReturnY int
ReturnHeight int
ReturnWidth int
Err error
}

GetScreenshotCall struct {
ReturnImage []byte
Err error
}
}

func (e *Element) GetElement(selector api.Selector) (*api.Element, error) {
Expand Down Expand Up @@ -161,3 +174,11 @@ func (e *Element) IsEqualTo(other *api.Element) (bool, error) {
func (e *Element) GetLocation() (x, y int, err error) {
return e.GetLocationCall.ReturnX, e.GetLocationCall.ReturnY, e.GetLocationCall.Err
}

func (e *Element) GetRect() (x, y, width, height int, err error) {
return e.GetRectCall.ReturnX, e.GetRectCall.ReturnY, e.GetRectCall.ReturnWidth, e.GetRectCall.ReturnHeight, e.GetRectCall.Err
}

func (e *Element) GetScreenshot() (s []byte, err error) {
return e.GetScreenshotCall.ReturnImage, e.GetScreenshotCall.Err
}
2 changes: 1 addition & 1 deletion multiselection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var _ = Describe("MultiSelection", func() {
BeforeEach(func() {
bus = &mocks.Bus{}
session = &api.Session{Bus: bus}
selection = NewTestMultiSelection(session, nil, "#selector")
selection = NewTestMultiSelection(session, nil, "#selector", nil)
})

Describe("#At", func() {
Expand Down
Loading