forked from serge1peshcoff/selenium-go-conditions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelement.go
78 lines (67 loc) · 2.29 KB
/
element.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
package conditions
import (
"github.com/tebeka/selenium"
"strings"
)
// ElementIsLocated returns a condition that checks if the element is found on page.
func ElementIsLocated(by, selector string) selenium.Condition {
return func(wd selenium.WebDriver) (bool, error) {
_, err := wd.FindElement(by, selector)
return err == nil, nil
}
}
// ElementIsVisible returns a condition that checks if the element is visible.
func ElementIsVisible(elt selenium.WebElement) selenium.Condition {
return func(wd selenium.WebDriver) (bool, error) {
visible, err := elt.IsDisplayed()
return visible, err
}
}
// ElementIsLocatedAndVisible returns a condition that checks if the element is found on page and is visible.
func ElementIsLocatedAndVisible(by, selector string) selenium.Condition {
return func(wd selenium.WebDriver) (bool, error) {
element, err := wd.FindElement(by, selector)
if err != nil {
return false, nil
}
visible, err := element.IsDisplayed()
return visible, err
}
}
// ElementIsEnabled returns a condition that checks if element's enabled.
func ElementIsEnabled(elt selenium.WebElement) selenium.Condition {
return func (wd selenium.WebDriver) (bool, error) {
enabled, err := elt.IsEnabled()
return enabled, err
}
}
// ElementTextIs returns a condition that checks if element's text equals to string.
func ElementTextIs(elt selenium.WebElement, text string) selenium.Condition {
return func (wd selenium.WebDriver) (bool, error) {
eltText, err := elt.Text()
if err != nil {
return false, err
}
return eltText == text, nil
}
}
// ElementTextContains returns a condition that checks if element's text contains a string.
func ElementTextContains(elt selenium.WebElement, text string) selenium.Condition {
return func (wd selenium.WebDriver) (bool, error) {
eltText, err := elt.Text()
if err != nil {
return false, err
}
return strings.Contains(eltText, text), nil
}
}
// ElementAttributeIs returns a condition that checks if element's attribute equals to string.
func ElementAttributeIs(elt selenium.WebElement, attribute, value string) selenium.Condition {
return func (wd selenium.WebDriver) (bool, error) {
attrValue, err := elt.GetAttribute(attribute)
if err != nil && err.Error() != "nil return value" {
return false, err
}
return attrValue == value, nil
}
}