Repository navigation
Expand file tree
/
Copy pathMainPageObject.java
More file actions
266 lines (217 loc) · 9.76 KB
/
Copy pathMainPageObject.java
File metadata and controls
266 lines (217 loc) · 9.76 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package lib.ui;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import lib.Platform;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import java.util.regex.Pattern;
public class MainPageObject {
protected RemoteWebDriver driver;
public MainPageObject(RemoteWebDriver driver) {
this.driver = driver;
}
public WebElement waitForElementPresent(String locator, String error_message, long timeoutInSeconds) {
By by = this.getLocatorByString(locator);
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.withMessage(error_message + "\n");
return wait.until(ExpectedConditions.presenceOfElementLocated(by));
}
public WebElement waitForElementPresent(String locator, String error_message) {
return waitForElementPresent(locator, error_message, 5);
}
public WebElement waitForElementAndClick(String locator, String error_message, long timeoutInSeconds) {
WebElement element = waitForElementPresent(locator, error_message, timeoutInSeconds);
element.click();
return element;
}
public WebElement waitForElementAndSendKeys(String locator, String value, String error_message, long timeoutInSeconds) {
WebElement element = waitForElementPresent(locator, error_message, timeoutInSeconds);
element.sendKeys(value);
return element;
}
public boolean waitForElementNotPresent(String locator, String error_message, long timeoutInSeconds) {
By by = this.getLocatorByString(locator);
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.withMessage(error_message + "\n");
return wait.until(ExpectedConditions.invisibilityOfElementLocated(by));
}
public WebElement waitForElementAndClear(String locator, String error_message, long timeoutInSeconds) {
WebElement element = waitForElementPresent(locator, error_message, timeoutInSeconds);
element.clear();
return element;
}
public void swipeUp(int timeOfSwipe) {
if (driver instanceof AppiumDriver) {
TouchAction action = new TouchAction((AppiumDriver) driver);
Dimension size = driver.manage().window().getSize();
int x = size.width / 2;
int startY = (int) (size.height * 0.8);
int endY = (int) (size.height * 0.2);
action
.press(x, startY)
.waitAction(timeOfSwipe)
.moveTo(x, endY)
.release()
.perform();
} else {
System.out.println("Method swipeUp() do nothing for platform " + Platform.getInstance().getPlatformVar());
}
}
public void swipeUpQuick() {
swipeUp(200);
}
public void scrollWebPageUp() {
if (Platform.getInstance().isMW()) {
JavascriptExecutor JSExecutor = (JavascriptExecutor) driver;
JSExecutor.executeScript("window.scrollBy(0, 250)");
} else {
System.out.println("Method scrollWebPageUp() does nothing for platform " + Platform.getInstance().getPlatformVar());
}
}
public void scrollWebPageTillElementNotVisible(String locator, String error_message, int max_swipes) {
int already_swiped = 0;
WebElement element = this.waitForElementPresent(locator, error_message);
while (!this.isElementLocatedOnTheScreen(locator)) {
scrollWebPageUp();
++already_swiped;
if (already_swiped > max_swipes) {
Assert.assertTrue(error_message, element.isDisplayed());
}
}
}
public void swipeUpToFindElement(String locator, String error_message, int max_swipes) {
By by = this.getLocatorByString(locator);
int already_swiped = 0;
while (driver.findElements(by).size() == 0) {
if (already_swiped > max_swipes) {
waitForElementPresent(locator, "Cannot find element by swiping up." + error_message, 0);
return;
}
swipeUpQuick();
++already_swiped;
}
}
public void swipeUpTitleElementAppear(String locator, String error_message, int max_swipes) {
int already_swiped = 0;
while (!this.isElementLocatedOnTheScreen(locator)) {
if (already_swiped > max_swipes) {
Assert.assertTrue(error_message, this.isElementLocatedOnTheScreen(locator));
}
swipeUpQuick();
++already_swiped;
}
}
public boolean isElementLocatedOnTheScreen(String locator) {
int element_location_on_y = this.waitForElementPresent(locator, "Cannot find element by locator", 1)
.getLocation()
.getY();
if (Platform.getInstance().isMW()) {
JavascriptExecutor JSExecutor = (JavascriptExecutor) driver;
Object js_result = JSExecutor.executeScript("return window.pageYOffset");
element_location_on_y -= Integer.parseInt(js_result.toString());
}
int screen_size_by_y = driver
.manage()
.window()
.getSize()
.getHeight();
return element_location_on_y < screen_size_by_y;
}
public void clickElementToTheRightCorner(String locator, String error_message) {
if (driver instanceof AppiumDriver) {
WebElement element = this.waitForElementPresent(locator + "/..", error_message);
int left_x = element.getLocation().getX();
int upper_y = element.getLocation().getY();
int lower_y = upper_y + element.getSize().getHeight();
int middle_y = (upper_y + lower_y) / 2;
int width = element.getSize().getWidth();
int point_to_click_x = (left_x + width) - 3;
int point_to_click_y = middle_y;
TouchAction action = new TouchAction((AppiumDriver) driver);
action.tap(point_to_click_x, point_to_click_y).perform();
} else {
System.out.println("Method clickElementToTheRightCorner() do nothing for platform " + Platform.getInstance().getPlatformVar());
}
}
public void swipeElementToLeft(String locator, String error_message) {
if (driver instanceof AppiumDriver) {
WebElement element = waitForElementPresent(
locator,
error_message,
10);
int left_x = element.getLocation().getX();
int right_x = left_x + element.getSize().getWidth();
int upper_y = element.getLocation().getY();
int lower_y = upper_y + element.getSize().getHeight();
int middle_y = (upper_y + lower_y) / 2;
TouchAction action = new TouchAction((AppiumDriver) driver);
action.press(right_x, middle_y);
action.waitAction(300);
if (Platform.getInstance().isAndroid()) {
action.moveTo(left_x, middle_y);
} else {
int offset_x = (-1 * element.getSize().getWidth());
action.moveTo(offset_x, 0);
}
action.release();
action.perform();
} else {
System.out.println("Method swipeElementToLeft() do nothing for platform " + Platform.getInstance().getPlatformVar());
}
}
public int getAmountOfElements(String locator) {
By by = this.getLocatorByString(locator);
List elements = driver.findElements(by);
return elements.size();
}
public boolean isElementPresent(String locator) {
return getAmountOfElements(locator) > 0;
}
public void tryClickElementWithFewAttempts(String locator, String error_message, int amount_of_attempts) {
int current_attempts = 0;
boolean need_more_attempts = true;
while (need_more_attempts) {
try {
this.waitForElementAndClick(locator, error_message, 1);
need_more_attempts = false;
} catch (Exception e) {
if (current_attempts > amount_of_attempts) {
this.waitForElementAndClick(locator, error_message, 1);
}
}
++current_attempts;
}
}
public void assertElementNotPresent(String locator, String error_message) {
int amount_of_elements = getAmountOfElements(locator);
if (amount_of_elements > 0) {
String default_message = "An element '" + locator + "' supposed to be not present";
throw new AssertionError(default_message + " " + error_message);
}
}
public String waitForElementAndGetAttribute(String locator, String attribute, String error_message, long timeoutInSeconds) {
WebElement element = waitForElementPresent(locator, error_message, timeoutInSeconds);
return element.getAttribute(attribute);
}
private By getLocatorByString(String locator_with_type) {
String[] exploded_locator = locator_with_type.split(Pattern.quote(":"), 2);
String by_type = exploded_locator[0];
String locator = exploded_locator[1];
if (by_type.equals("xpath")) {
return By.xpath(locator);
} else if (by_type.equals("id")) {
return By.id(locator);
} else if (by_type.equals("css")) {
return By.cssSelector(locator);
} else {
throw new IllegalArgumentException("Cannot get type of locator. Locator: " + locator_with_type);
}
}
}