-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
109 lines (100 loc) · 3.28 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package GOpom
import (
"errors"
"fmt"
"image"
"net/url"
"github.com/tebeka/selenium"
)
/*ByOption is the regular expression for allowed selenium.By* in step
definitions*/
const ByOption = `(id|xpath|link text|partial link text|name|tag name|class name|css selector)`
//GetWebDriver returns the webdriver
func (b *BrowserSteps) GetWebDriver() selenium.WebDriver {
return b.wd
}
//GetURL returns a absolute url given a absolute or relative URL
func (b *BrowserSteps) GetURL(URL string) (*url.URL, error) {
u, err := url.Parse(URL)
if err != nil {
return nil, fmt.Errorf("Malformed URL: %s", URL)
}
if !u.IsAbs() {
if b.URL == nil {
return nil, errors.New("Using a relative URL without a base URL defined. Invoke BrowserSteps.SetBaseURL")
}
return b.URL.ResolveReference(u), nil
}
return u, nil
}
//GetCurrentWindowInnerSize returns window inner size
func (b *BrowserSteps) GetCurrentWindowInnerSize() (*selenium.Size, error) {
widthI, err := b.GetWebDriver().ExecuteScript(`return window.innerWidth ||
document.documentElement.clientWidth || document.body.clientWidth;
`, []interface{}{})
if err != nil {
return nil, err
}
width, ok := widthI.(float64)
if !ok {
return nil, fmt.Errorf("Can not cast width %+v to a float64", widthI)
}
heightI, err := b.GetWebDriver().ExecuteScript(`return window.innerHeight ||
document.documentElement.clientHeight || document.body.clientHeight`, []interface{}{})
if err != nil {
return nil, err
}
height, ok := heightI.(float64)
if !ok {
return nil, fmt.Errorf("Can not cast height %+v to a float64", heightI)
}
return &selenium.Size{Width: int(width), Height: int(height)}, nil
}
//GetCurrentWindowScroll returns window scroll
func (b *BrowserSteps) GetCurrentWindowScroll() (*selenium.Point, error) {
scrollXI, err := b.GetWebDriver().ExecuteScript(`return window.scrollX ||
document.body.scrollLeft || document.documentElement.scrollLeft`, []interface{}{})
if err != nil {
return nil, err
}
scrollX, ok := scrollXI.(float64)
if !ok {
return nil, fmt.Errorf("Can not cast scrollX %+v to a float64", scrollXI)
}
scrollYI, err := b.GetWebDriver().ExecuteScript(`return window.scrollY ||
document.body.scrollTop || document.documentElement.scrollTop`, []interface{}{})
if err != nil {
return nil, err
}
scrollY, ok := scrollYI.(float64)
if !ok {
return nil, fmt.Errorf("Can not cast scrollY %+v to a float64", scrollYI)
}
return &selenium.Point{X: int(scrollX), Y: int(scrollY)}, nil
}
//GetCurrentWindowViewport returns window scroll
func (b *BrowserSteps) GetCurrentWindowViewport() (image.Rectangle, error) {
windowSize, err := b.GetCurrentWindowInnerSize()
if err != nil {
return image.Rectangle{}, err
}
scrollSize, err := b.GetCurrentWindowScroll()
if err != nil {
return image.Rectangle{}, err
}
return image.Rect(scrollSize.X, scrollSize.Y,
scrollSize.X+windowSize.Width, scrollSize.Y+windowSize.Height), nil
}
//GetElementRect returns the element rectangle
func (b *BrowserSteps) GetElementRect(element selenium.WebElement) (image.Rectangle, error) {
location, err := element.Location()
if err != nil {
return image.Rectangle{}, err
}
size, err := element.Size()
if err != nil {
return image.Rectangle{}, err
}
return image.Rect(location.X, location.Y,
location.X+size.Width, location.Y+size.Height), nil
}