-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebElements_Actions.java
422 lines (344 loc) · 13.7 KB
/
WebElements_Actions.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package seleniumPackage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.support.Color;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.io.Files;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.comparison.ImageDiff;
import ru.yandex.qatools.ashot.comparison.ImageDiffer;
import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class WebElements_Actions extends SeleniumLaunchBrowser
{
public void createNewTabOne(String url)
{
this.url=url;
instantiateBrowser();
// First way of handling creation of new tab, closing the same and shifting to the initially opened tab.
String originalwin= driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.close(); // Closes currently focused tab.
driver.switchTo().window(originalwin); //Shifting to the initially opened tab.
driver.navigate().to(url);
System.out.println(driver.getCurrentUrl());
driver.quit();
}
public void createNewTabTwo(String url) throws InterruptedException
{
this.url=url;
instantiateBrowser();
driver.switchTo().newWindow(WindowType.TAB);
// Navigate through opened tabs
ArrayList<String> tab= new ArrayList<String>(driver.getWindowHandles());
System.out.println(tab.size()); // Gets the number of opened tabs.
driver.switchTo().window(tab.get(1));
System.out.println(driver.getTitle());
driver.close();
driver.switchTo().window(tab.get(0));
driver.get(url);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.println(driver.getCurrentUrl());
driver.quit();
}
public void tableAction()
{
instantiateBrowser();
driver.get("C:/Users/baner/Downloads/newsample.html");
int csize;
// To locate table
WebElement table= driver.findElement(By.id("users-list"));
// To locate table rows
List<WebElement> rows= table.findElements(By.tagName("tr"));
// To calculate number of rows in the table
int rsize=rows.size();
System.out.println("Total Row Count: "+rsize);
/* To get data(cell value) from each cell in the table
* NOTE: To get data(cell value) from each cell accurately, use <tbody> tag(in line 89) while locating the table.
*/
// Loop will execute till the last row
for(int i=0; i<rsize; i++)
{
// To locate cells of that specific row
List<WebElement> cells= rows.get(i).findElements(By.tagName("td"));
// To calculate number of cells in that specific row
csize=cells.size();
System.out.println("Number of cells In Row " + i + " are " + csize);
// Loop will execute till the last cell of that specific row
for(int j=0; j<csize; j++)
{
// To retrieve data(value) from that specific cell
String cellValue= cells.get(j).getText();
System.out.println("Cell Value of row number " + i + " and column number " + j + " Is " + cellValue);
}
System.out.println("-------------------------------------------------- ");
}
// To locate all cells of the table
List<WebElement> cells= table.findElements(By.tagName("td"));
// To calculate total number of cells of the table
csize=cells.size();
System.out.println("Total Cell Count: "+csize);
// To locate cells head or column head of the table
List<WebElement> cellheads= table.findElements(By.tagName("th"));
// To calculate number of cells head or column head of the table
int chsize=cellheads.size();
System.out.println("Total Cell_Heads Count: "+chsize);
// To calculate number of columns of the table
int colsize=(csize+chsize)/rsize;
System.out.println("Total Column Count: "+colsize);
driver.quit();
}
@SuppressWarnings("deprecation")
public void screenShotAction() throws IOException, InterruptedException
{
instantiateBrowser();
driver.navigate().to("https://www.ebizindia.com/");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
// 1) Capture screen_shot for current opened window
File imgcapture= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try
{
FileHandler.copy(imgcapture, new File("C:/Users/baner/Downloads/ss.png")); // copies and saves the screen_shot image
// to mentioned location
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
// 2) Capture screen_shot of entire web page
Screenshot imgss= new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1500)).takeScreenshot(driver); // Capturing the Screenshot with the help of Ashot()
File entireimg = new File("C:/Users/baner/Downloads/ss1.png");
try
{
ImageIO.write(imgss.getImage(), "png", entireimg); //The screenshot to be captured will be in .png image
// format and would be saved at above mentioned path.
// NOTE: We can use BufferedImage actualimage= imgss.getImage() method to get the captured screen_shot image.
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
// 3) Capturing screenshot of a specific web element
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
WebElement textlink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("3s-services")));
File eleimage= textlink.getScreenshotAs(OutputType.FILE);
try
{
File eximage= new File("C:/Users/baner/Downloads/ss2.png");
FileHandler.copy(eleimage,eximage);
//textlink.sendKeys(Keys.PAGE_DOWN);
/* 1) For scrolling by defining number of pixels(normal scroll) use the following:
* JavascriptExecutor js= (JavascriptExecutor)driver;
* js.executeScript("window.scrollBy(0,350)", "");
* NOTE: (+ve)x & y denotes right & down scrolling and (-ve)x & y denotes left & up scrolling, respectively.
*
* 2) For scrolling to the bottom of the web page:
* js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
*/
//Screenshot webeleimage= new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver, textlink);
//File eleimage = new File("C:/Users/baner/Downloads/ss2.png");
//ImageIO.write(webeleimage.getImage(), "PNG", eleimage);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
// 4) Comparing web-element image with initial image.
try
{
// Reading the image for comparison
BufferedImage actualimage= ImageIO.read(imgcapture);
BufferedImage expectedimage= ImageIO.read(eleimage);
ImageDiffer img_diff= new ImageDiffer();
ImageDiff difference= img_diff.makeDiff(expectedimage, actualimage);
if(difference.hasDiff())
{
System.out.println("Both images didn't match");
}
else
{
System.out.println("Both images matched");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
finally
{
driver.quit();
}
}
public void mouseHover()
{
// Get tool_tip text in mouse-hover action.
instantiateBrowser();
driver.navigate().to("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement tooltipelem= driver.findElement(By.id("APjFqb"));
String toolTipMsg= tooltipelem.getAttribute("title");
System.out.println("Tool-tip message: "+toolTipMsg); // Get the desired text through getAttribute() method
// if the element has title/aria-label attribute
// OR
// Use Action class to perform the desired action
Actions action= new Actions(driver);
action.moveToElement(tooltipelem).perform(); //Performing the mouse hover action
String ttMessage=null;
if(tooltipelem.equals(driver.switchTo().activeElement())) // Check if driver focus is currently in desired element
{
ttMessage= tooltipelem.getAttribute("title");
System.out.println("Tool-tip message using 'Actions' class: "+ttMessage);
}
// Comparing the two strings returned
if(toolTipMsg.equals(ttMessage))
{
System.out.println("Tool-tip messsages matched in both ways");
}
else
{
System.out.println("Tool-tip messsages didn't match in both ways");
}
driver.quit();
}
public void alertPopUpError() throws InterruptedException
{
/* 1) Getting error message for blank validation.
* 2) Getting error message for blank validation from alert window.
*/
// 1) Getting error message for blank validation.
instantiateBrowser();
driver.navigate().to("https://demo8.ebizindia.com/memdir/login.php");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id=\'login-box\']/div/form/div[4]/div/button")).click();
System.out.println("Erro Message: "+driver.findElement(By.id("alert-error-message")).getText());
driver.findElement(By.id("login_username")).sendKeys("[email protected]");
driver.findElement(By.id("login_password")).sendKeys("xyz123");
driver.findElement(By.xpath("//*[@id=\'login-box\']/div/form/div[4]/div/button")).click();
Thread.sleep(1000);
driver.navigate().to("https://demo8.ebizindia.com/memdir/users.php#mode=addUser");
JavascriptExecutor js= (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
driver.findElement(By.id("record-save-button")).click();
// 2) Getting error message for blank validation from alert window
Alert alert= driver.switchTo().alert();
System.out.println("Alert Message: "+alert.getText());
alert.accept();
driver.quit();
}
public void dropDownActions() throws InterruptedException
{
/* 1) Selection of item from drop-down
* 2) Getting all items as List
* 3) Getting size of the drop-down
* 4) Getting the selected item
* 5) Printing all items of drop-down
*/
instantiateBrowser();
driver.navigate().to("https://demo8.ebizindia.com/memdir/login.php");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.id("login_username")).sendKeys("[email protected]");
driver.findElement(By.id("login_password")).sendKeys("xyz123");
driver.findElement(By.xpath("//*[@id=\'login-box\']/div/form/div[4]/div/button")).click();
Thread.sleep(1000);
driver.navigate().to("https://demo8.ebizindia.com/memdir/users.php#mode=addUser");
JavascriptExecutor js= (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
driver.findElement(By.id("record-save-button")).click();
Alert alert= driver.switchTo().alert();
alert.accept();
// Verifying the border color on error
try
{
String orgColorHex= "#ff0000";
WebElement dDown= driver.findElement(By.id("add_form_field_title"));
String rgbCol= dDown.getCssValue("border-color"); // Getting color property
System.out.println("Color Is: "+rgbCol);
/*String hexCode= Color.fromString(rgbCol).asHex(); // Converting to HEX Code
System.out.println("HEX Code Of Color: "+hexCode);
if(orgColorHex.equals(hexCode))
{
System.out.println("Color matched");
}
else
{
System.out.println("Color didn't match");
}*/
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
driver.quit();
/*Select drop= new Select(driver.findElement(By.id("add_form_field_title")));
drop.selectByIndex(1); // Selection of item from drop-down
List<WebElement> options= drop.getOptions(); // Getting all items as List
System.out.println("Size of drop-down: "+options.size()); // Getting size of the drop-down
System.out.println("Selected Item: "+drop.getFirstSelectedOption().getText()); // Getting the selected item
for(WebElement items: options) // Printing all items of drop-down
{
System.out.println(items.getText());
}*/
}
public void brokenLinkActions() throws InterruptedException
{
/* 1) Fetch all links present on the web page
* 2) Send HTTP request for the link
* 3) Verify the HTTP response code for the link
* 4) Determine if the link is valid or it is broken based on the HTTP response code
* 5) Repeat the process for all links captured with the first step
*/
instantiateBrowser();
driver.navigate().to("https://www.memdir.in/index.php");
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
List<WebElement> links= driver.findElements(By.tagName("a"));
System.out.println("Number Of Links: "+links.size());
String url=null;
for(WebElement fetchedLinks: links)
{
url= fetchedLinks.getAttribute("href");
try
{
URL link= new URL(url);
HttpURLConnection setConnection= (HttpURLConnection) link.openConnection();
setConnection.setConnectTimeout(3000);
setConnection.connect();
if(setConnection.getResponseCode()== 200)
{
System.out.println(url+": "+setConnection.getResponseMessage());
}
else
{
System.out.println(url+": "+setConnection.getResponseMessage()+"- is broken");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
driver.close();
}
}