-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePage.java
More file actions
166 lines (146 loc) · 6.7 KB
/
BasePage.java
File metadata and controls
166 lines (146 loc) · 6.7 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
package pages;
import managers.FileReaderManager;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.MoveTargetOutOfBoundsException;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import java.util.List;
public class BasePage {
protected WebDriver driver;
private static final String ENVIRONMENT = FileReaderManager.getInstance().getConfigReader().getEnvironment();
protected WebDriverWait wait;
protected JavascriptExecutor javascriptExecutor;
public BasePage(WebDriver driver) {
this.driver = driver;
javascriptExecutor = (JavascriptExecutor) driver;
wait = new WebDriverWait(driver, 15);
}
protected void waitAndClick(By by) {
wait.until(ExpectedConditions.visibilityOfElementLocated(by)).click();
}
protected void selectByText(By by, String text){
try {
waitForElementClikable(by);
Select select = new Select(driver.findElement(by));
select.selectByVisibleText(text);
} catch (ElementClickInterceptedException e){
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(by)).click().build().perform();
shortSleep();
driver.findElement(By.xpath("//a[text()='"+text+"']")).click();
}
catch (NoSuchElementException e){
System.out.println("No Such element found : " + e.toString());
}
catch (WebDriverException e){
Actions actions = new Actions(driver);
try {
actions.moveToElement(driver.findElement(by)).click().build().perform();
shortSleep();
driver.findElement(By.xpath("//a[text()='" + text + "']")).click();
}catch (MoveTargetOutOfBoundsException exception){
driver.navigate().refresh();
}
}
}
protected void shortSleep() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
protected void VerifyElementDisplayedAndClick(By locator, String message) {
if (ENVIRONMENT.equalsIgnoreCase("local")) {
try {
if (wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).isDisplayed()) {
shortSleep();
wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
}
} catch (Exception e) {
System.out.println("Element not clicked");
}
}else {
try {
if (wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).isDisplayed()) {
shortSleep();
wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).click();
javascriptExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"+" + message + "+\"}}");
}
} catch (Exception e) {
javascriptExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"Element Not clicked!\"}}");
System.out.println("Element not clicked");
}
}
}
protected void VerifyElementDisplayed(By locator, String message) {
if (ENVIRONMENT.equalsIgnoreCase("local")) {
try {
if (wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).isDisplayed()) {
boolean isDisplayed = driver.findElement(locator).isDisplayed();
Assert.assertTrue(isDisplayed);
}
} catch (Exception e) {
Assert.assertFalse(false);
}
}else {
try {
if (wait.until(ExpectedConditions.visibilityOfElementLocated(locator)).isDisplayed()) {
boolean isDisplayed = driver.findElement(locator).isDisplayed();
Assert.assertTrue(isDisplayed);
javascriptExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"+" + message + "+\"}}");
}
} catch (Exception e) {
javascriptExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"Element Not clicked!\"}}");
System.out.println("Element not clicked");
}
}
}
protected void VerifyText(String expected, String actual, String message) {
if (ENVIRONMENT.equalsIgnoreCase("local")) {
Assert.assertEquals(expected, actual, message);
} else {
if (expected.equals(actual)) {
Assert.assertEquals(expected, actual);
javascriptExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"+" + message + "+\"}}");
System.out.println("Page Title is verified");
} else {
javascriptExecutor.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Text is Different\"}}");
}
}
}
protected void click(By by) {
driver.findElement(by).click();
}
protected void hideKeyboard() {
driver.navigate().back();
}
protected List<WebElement> waitAndFindElements(By by) {
return wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(by));
}
protected WebElement waitAndFindElement(By by) {
return wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
protected WebElement waitForElementClikable(By by) {
return wait.until(ExpectedConditions.elementToBeClickable(by));
}
protected String getText(By by) {
return waitAndFindElement(by).getText();
}
protected void sendKey(By by, String text) {
waitAndFindElement(by).sendKeys(text);
}
public List<WebElement> getElements(By elementLocator) {
try {
List<WebElement> elements = driver.findElements(elementLocator);
return elements;
} catch (WebDriverException exception) {
throw new WebDriverException(
"Element with locator : " + elementLocator + " was not displayed and unable to get the count",
exception);
}
}
}