-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseTest.java
More file actions
121 lines (94 loc) · 3.66 KB
/
BaseTest.java
File metadata and controls
121 lines (94 loc) · 3.66 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
package com.demo.tests;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
public class BaseTest {
String Url = "https://www.emirates.com/ae/english/";
private WebDriver driver;
private Actions builder;
// will launch the browser and the url
public WebDriver launchBrowser() {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\User\\Desktop\\drivers\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setVersion("59.0.3");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("marionette", true);
driver = new FirefoxDriver(capabilities);
driver.manage().deleteAllCookies();
driver.get(Url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
return driver;
}
public WebDriver launchBrowserChrome(){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Desktop\\drivers\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
driver = new ChromeDriver(capabilities);
driver.get(Url);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
// getting the driver object
public WebDriver getDriver() {
return driver;
}
// getting actions object
public Actions getActions() {
builder = new Actions(getDriver());
return builder;
}
public void elementPresence(By by, long seconds) {
WebDriverWait wait = new WebDriverWait(driver, seconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
// this method will quite the browser instance after executing the script.
@AfterTest
public void tearDown() {
getDriver().quit();
}
public void safeType(By by, String Data) {
final WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
getDriver().findElement(by).clear();
getDriver().findElement(by).sendKeys(Data);
}
public void safeClick(By by) {
final WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
getDriver().findElement(by).click();
}
public String getCurrentDate(){
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); //2016/11/16
String currentDate = dtf.format(localDate);
String [] temp = currentDate.split("/");
String currentDate1 = temp[temp.length-1];
return currentDate1;
}
public void scrollIntoElement(By id){
WebElement element = driver.findElement(id);
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("arguments[0].scrollIntoView(true);",element);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}