-
Notifications
You must be signed in to change notification settings - Fork 0
/
prac.java
335 lines (276 loc) · 12.6 KB
/
prac.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import java.time.Duration;
//import java.util.List;
import java.util.Set;
//import java.util.concurrent.TimeUnit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.FileReader;
import java.io.BufferedReader;
//import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
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.print.PrintOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
//import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Pdf;
import org.openqa.selenium.TakesScreenshot;
//Parallel execution
public class prac implements Runnable{
WebDriver driver;
int num = 0;
int length;
String fileName;
Thread agent;
prac() {}
prac(String fileName) {
this.fileName = fileName;
agent = new Thread(this, "Testing Agent for "+fileName);
}
// Data driven
public void run() {
System.out.println(agent.getName());
BufferedReader buff;
try {
buff = new BufferedReader(new FileReader(fileName));
String record;
while ((record = buff.readLine()) != null) {
String source = record.split(",")[0];
String destination = record.split(",")[1];
String travelClass = record.split(",")[2];
try {
launchBrowser(source, destination, travelClass);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Window handle
private void windowOperations() throws InterruptedException {
// Get the WebDriver.Window interface
WebDriver.Window window = driver.manage().window();
// Maximize the browser window
window.maximize();
Thread.sleep(1000);
// Set the browser window size
window.setSize(new org.openqa.selenium.Dimension(800, 600));
Thread.sleep(1000);
// Get the browser window position
System.out.println("Window position: " + window.getPosition());
Thread.sleep(1000);
window.maximize();
Thread.sleep(2000);
}
// cookies
private void cookiesOperations() throws InterruptedException {
//Options
WebDriver.Options options = driver.manage();
// Perform operations using WebDriver.Options
// Retrieve and print cookies
Set<Cookie> cookies = options.getCookies();
System.out.println("Number of cookies: " + cookies.size());
for (Cookie cookie : cookies) {
System.out.println("Cookie: " + cookie.getName() + " = " + cookie.getValue());
}
options.deleteAllCookies();
options.timeouts().implicitlyWait(Duration.ofSeconds(10));
System.err.println("Deleted all Cookies");
}
//ACTUAL TESTING
private void enterCity(String cityName) throws InterruptedException {
driver.findElement(By.xpath("//*[@id=\"top-banner\"]/div[2]/div/div/div/div[2]/div/div[1]/label")).click();
Thread.sleep(2000);
driver.findElement(
By.xpath("//*[@id='root']/div/div[2]/div/div/div/div[2]/div/div[1]/div[1]/div/div/div/input"))
.sendKeys(cityName);
// wait until element is not visible
Thread.sleep(2000);
int length = driver.findElements(By.cssSelector("ul.react-autosuggest__suggestions-list > li")).size();
System.out.println("Length of the list is: " + length);
Thread.sleep(1000);
// define a loop to iterate through the list
for (int i = 0; i < length; i++) {
// find the element using index
String element = driver.findElements(By.cssSelector(
"ul.react-autosuggest__suggestions-list > li > div > div > p.searchedResult.font14.darkText > span"))
.get(i)
.getText();
System.out.println("Element is: " + element);
// check if the element is equal to the search term
if (element.equals(cityName)) {
// click on the element
driver.findElements(By.cssSelector(
"ul.react-autosuggest__suggestions-list > li > div > div > p.searchedResult.font14.darkText > span"))
.get(i).click();
break;
}
}
}
private void selectCity(String destination) throws InterruptedException {
driver.findElement(By.xpath("//*[@id='root']/div/div[2]/div/div/div/div[2]/div/div[2]/label/span")).click();
Thread.sleep(2000);
driver.findElement(
By.xpath("//*[@id='root']/div/div[2]/div/div/div/div[2]/div/div[2]/div[1]/div/div/div/input"))
.sendKeys(destination);
// wait until element is not visible
Thread.sleep(2000);
length = driver.findElements(By.cssSelector("ul.react-autosuggest__suggestions-list > li")).size();
System.out.println("Length of the list is: " + length);
Thread.sleep(1000);
for (int i = 0; i < length; i++) {
// find the element using index
String element = driver.findElements(By.cssSelector(
"ul.react-autosuggest__suggestions-list > li > div > div > p.searchedResult.font14.darkText > span"))
.get(i)
.getText();
System.out.println("Element is: " + element);
// check if the element is equal to the search term
if (element.equals(destination)) {
// click on the element
driver.findElements(By.cssSelector(
"ul.react-autosuggest__suggestions-list > li > div > div > p.searchedResult.font14.darkText > span"))
.get(i).click();
break;
}
}
}
private void selectDate() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='root']/div/div[2]/div/div/div/div[2]/div/div[3]")).click();
Thread.sleep(1000);
driver.findElement(By.xpath(
"//*[@id='root']/div/div[2]/div/div/div/div[2]/div/div[3]/div[1]/div/div/div/div[2]/div/div[2]/div[1]/div[3]/div[3]/div[6]"))
.click();
}
private void selectTravelClass(String travelClass) throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='root']/div/div[2]/div/div/div/div[2]/div/div[4]")).click();
length = driver.findElements(By.cssSelector("ul.travelForPopup > li")).size();
System.out.println("Length of the list is: " + length);
for (int i = 0; i < length; i++) {
String element = driver.findElements(By.cssSelector("ul.travelForPopup > li")).get(i).getText();
System.out.println("Element is: " + element);
if (element.equals(travelClass)) {
driver.findElements(By.cssSelector("ul.travelForPopup > li")).get(i).click();
break;
}
}
}
private void clickSearchButton() throws InterruptedException {
Thread.sleep(4000);
driver.findElement(By.xpath("//*[@id=\"top-banner\"]/div[2]/div/div/div/div[2]/p/a")).click();
Thread.sleep(4000);
}
private void navigateBack() throws InterruptedException {
driver.navigate().to("https://www.makemytrip.com/railways/");
Thread.sleep(4000);
}
//SCREENSHOT
synchronized public void takeScreenshot(String fileName) throws IOException {
try {
TakesScreenshot screenshot = (TakesScreenshot) driver;
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
File destinationFile = new File(fileName);
Files.copy(sourceFile.toPath(), destinationFile.toPath());
System.out.println("Screenshot saved: " + destinationFile.getAbsolutePath());
} catch (IOException e) {
System.err.println("Error taking screenshot: " + e.getMessage());
throw e; // Re-throw the exception after handling it
}
}
public void quitDriver() {
// Quit the driver when needed
if (driver != null) {
driver.quit();
}
}
public void launchBrowser(String source, String destination, String travelClass) throws InterruptedException {
driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/railways/");
// driver.manage().window().maximize();
// driver.navigate().refresh();
// Thread.sleep(3000);
// Check if you have landed in the correct page
// Print the URL and Title of the Page
String title = driver.getTitle();
System.out.println("Title of the page is: " + title);
String url = driver.getCurrentUrl();
System.out.println("URL of the page is: " + url);
windowOperations();
Thread.sleep(2000);
enterCity(source);
selectCity(destination);
selectDate();
selectTravelClass(travelClass);
clickSearchButton();
Thread.sleep(2000);
cookiesOperations();
Thread.sleep(2000);
navigateBack();
try {
takeScreenshot("screenshot_"+fileName.substring(4, 9)+"_"+num+".png");
num++;
} catch (IOException e) {
System.err.println("Error taking screenshot: " + e.getMessage());
}
driver.quit();
}
//HIGHLIGHT
private void highlightItems(WebDriver driver) throws InterruptedException, IOException {
// driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/railways/");
// driver.manage().window().maximize();
Thread.sleep(20000);
WebElement offers = driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/main/main/div[2]/div[1]/h2"));
WebElement ticket = driver.findElement((By.xpath("/html/body/div[1]/div/div[2]/div/div/div/div[1]/div/h1")));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'background: orange; border: 2px solid red;');", ticket);
js.executeScript("arguments[0].setAttribute('style', 'background: orange; border: 2px solid red;');", offers);
System.out.println("Elemeaqwnts highlighted");
Thread.sleep(20000);
downloadPage(driver);
System.out.println("Page downloaded");
// throw new UnsupportedOperationException("Unimplemented method 'highlight'");
}
//DOWNLOAD PDF
private void downloadPage(WebDriver driver) throws IOException {
Path printPage = Paths.get("MMT_homepage.pdf");
// driver.get("https://www.makemytrip.com/railways/");
Pdf print = ((RemoteWebDriver) driver).print(new PrintOptions());
Files.write(printPage, OutputType.BYTES.convertFromBase64Png(print.getContent()));
}
public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
prac testerOne = new prac("src/test1.csv");
prac testerTwo = new prac("src/test2.csv");
prac testerThree = new prac("src/test3.csv");
prac obj = new prac();
System.setProperty("webdriver.chrome.driver", "chromedriver1.exe");
testerOne.agent.start();
testerTwo.agent.start();
testerThree.agent.start();
testerOne.agent.join();
testerTwo.agent.join();
testerThree.agent.join();
System.out.println("Parallel Testing Execution completed, printing the homepage...");
obj.driver = new ChromeDriver();
Thread.sleep(2000);
obj.highlightItems(obj.driver);
Thread.sleep(2000);
obj.downloadPage(obj.driver);
Thread.sleep(2000);
obj.quitDriver();
}
}