-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathVerifyElements.java
More file actions
95 lines (79 loc) · 3.24 KB
/
VerifyElements.java
File metadata and controls
95 lines (79 loc) · 3.24 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
package pages;
import org.codacy.BasePage;
import org.codacy.BaseTest;
import org.codacy.Environment;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.asserts.SoftAssert;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class VerifyElements extends BasePage {
public VerifyElements(RemoteWebDriver driver, Environment env) {
super(driver, env);
}
public void main(String urlLink) {
SoftAssert assertion = new SoftAssert();
try {
//Use URL Class - Create object of the URL Class and pass the urlLink as parameter
URL link = new URL(urlLink);
// Create a connection using URL object (i.e., link)
HttpURLConnection httpConn = (HttpURLConnection) link.openConnection();
//Set the timeout for 2 seconds
httpConn.setConnectTimeout(2000);
//connect using connect method
httpConn.connect();
//use getResponseCode() to get the response code.
if (httpConn.getResponseCode() == 200) {
System.out.println(urlLink + " - " + httpConn.getResponseMessage());
}
if (httpConn.getResponseCode() == 404) {
System.out.println(urlLink + " - " + httpConn.getResponseMessage());
// Assert.fail();
assertion.assertTrue(false);
}
}
//getResponseCode method returns = IOException - if an error occurred connecting to the server.
catch (Exception e) {
//e.printStackTrace();
}
// assertion.assertAll();
}
public void verifyImg()
{
List<WebElement> img = driver.findElements(By.tagName("img"));
//To print the total number of links
System.out.println("Total imgs are " + img.size());
//used for loop to
for (int i = 0; i < img.size(); i++) {
WebElement element = img.get(i);
//By using "href" attribute, we could get the url of the requried link
String url = element.getAttribute("src");
//calling verifyLink() method here. Passing the parameter as url which we collected in the above link
//See the detailed functionality of the verifyLink(url) method below
main(url);
}
}
public void verifyLink()
{
List<WebElement> links = driver.findElements(By.tagName("a"));
//To print the total number of links
System.out.println("Total links are " + links.size());
//used for loop to
for (int i = 0; i < links.size(); i++) {
WebElement element = links.get(i);
//By using "href" attribute, we could get the url of the requried link
String url = element.getAttribute("href");
//calling verifyLink() method here. Passing the parameter as url which we collected in the above link
//See the detailed functionality of the verifyLink(url) method below
main(url);
}
}
public void testsoft(){
SoftAssert assertion = new SoftAssert();
assertion.assertAll();
}
}