/
Should.java
122 lines (97 loc) · 3.43 KB
/
Should.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.selenquery.core;
import org.openqa.selenium.WebElement;
public class Should {
private int timeout = 0;
private boolean expect = true;
private SelenElement selenWebElement = null;
private SelenDriver driver = null;
public Should(SelenDriver driver, SelenElement selenWebElement, boolean expect) {
this.driver = driver;
this.selenWebElement = selenWebElement;
this.expect = expect;
}
public Should(SelenDriver driver, SelenElement selenWebElement, boolean expect, int timeoutInSec) {
this.driver = driver;
this.selenWebElement = selenWebElement;
this.expect = expect;
this.timeout = timeoutInSec;
}
public void haveText(String regex) {
haveText(regex, true);
}
public void haveText(String regex, boolean caseSensitive) {
String txt = selenWebElement.getText();
if (!caseSensitive) {
regex = regex.toLowerCase();
txt = txt.toLowerCase();
}
boolean res = Assertion.test(expect, txt.matches(regex), "Test for Text matching");
if (!res) {
Assertion.printDetails(expect, regex, txt);
}
}
public void haveClass(String className) {
String actualClassName = selenWebElement.getAttribute("class");
Assertion.test(expect, actualClassName, className, "Test for Class Name matching");
}
public void haveAttr(String attrName) {
try {
selenWebElement.getAttribute(attrName);
Assertion.test(expect, true, "Test for Attribute availability");
} catch (Exception e) {
Assertion.test(expect, false, "Test for Attribute availability");
}
}
public void haveValue(String value) {
haveAttrVal("value", value, true);
}
public void haveValue(String value, boolean caseSensitive) {
haveAttrVal("value", value, caseSensitive);
}
public void haveAttrVal(String attrName, String attrValue) {
haveAttrVal(attrName, attrValue, true);
}
public void haveAttrVal(String attrName, String attrValue, boolean caseSensitive) {
String attibValue = selenWebElement.getAttribute(attrName);
if (!caseSensitive) {
Assertion.test(expect, attrValue.toLowerCase(), attibValue.toLowerCase(), "Have Attribute value");
} else {
Assertion.test(expect, attrValue, attibValue, "Have Attribute value");
}
}
public void haveStyle(String styleProperty, String styleValue) {
String val = selenWebElement.getCssValue(styleProperty);
Assertion.test(expect, styleValue.toLowerCase(), val.toLowerCase(), "Test for css Style property matching");
}
public void displayed() {
Assertion.test(expect, selenWebElement.isDisplayed(), "Test for elment displying");
}
public void readonly() {
String x = selenWebElement.getAttribute("readonly");
if (x == null) {
Assertion.test(expect, false, "readonly");
} else if (x.equals("true")) {
Assertion.test(expect, true, "readonly");
}
}
public void disabled() {
String x = selenWebElement.getAttribute("disabled");
if (x == null) {
Assertion.test(expect, false, "disabled");
} else if (x.equals("true")) {
Assertion.test(expect, true, "disabled");
}
}
public void focused() {
WebElement elm = (WebElement) driver.executeScript("return document.activeElement");
if (elm != null && elm.equals(selenWebElement.getWebElement())) {
Assertion.test(expect, true, "Test for elment foucsed");
} else {
Assertion.test(expect, false, "Test for elment foucsed");
}
}
public void selected(){
boolean selected = selenWebElement.isSelected();
Assertion.test(expect, selected, "Selected > Having Value : " + selenWebElement.getAttribute("value"));
}
}