-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaseStudy_m2.java
More file actions
178 lines (127 loc) · 5.08 KB
/
CaseStudy_m2.java
File metadata and controls
178 lines (127 loc) · 5.08 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
167
168
169
170
171
172
173
174
175
176
177
178
package module2;
import org.openqa.selenium.By;
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class CaseStudy_m2 {
public static void main(String[] args) {
chrome();
//firefox();
}
public static void chrome() {
System.out.println("Start testing using chrome browser");
Testing a = new Testing();
a.browser("chrome", "C:\\\\2018\\\\edureka\\\\selenium\\\\EXEs\\\\chromedriver.exe");
}
public static void firefox() {
System.out.println("Start testing using firefox browser");
Testing a = new Testing();
a.browser("firefox", "C:\\\\2018\\\\edureka\\\\selenium\\\\EXEs\\\\geckodriver.exe");
}
}
class Testing{
public static void browser(String browser, String DriverPath) {
String url = "https://www.edureka.co/";
WebDriver driver = openBrowser(browser, DriverPath, url);
// Find element using ID locator
String loc_id = "homeSearchBar";
WebElement elementById = FindElement(driver, By.id(loc_id), 4);
if(elementById != null) {
System.out.println("Element is present with the given id : " + loc_id);
} else {
System.out.println("Element is not present with the given id : " + loc_id);
}
// Find element using NAME locator
String loc_name = "user_v1[query]";
WebElement elementByName = FindElement(driver, By.name(loc_name), 4);
if(elementByName != null) {
System.out.println("Element is present with the given name : " + loc_name);
} else {
System.out.println("Element is not present with the given name : " + loc_name);
}
// Find element using XPATH locator
String loc_xpath = "//*[@id='homeSearchBar']";
WebElement elementByXpath = FindElement(driver, By.xpath(loc_xpath), 4);
if(elementByXpath != null) {
System.out.println("Element is present with the given xpath : " + loc_xpath);
} else {
System.out.println("Element is not present with the given xpath : " + loc_xpath);
}
// Find element using CSS locator
String loc_css = "#homeSearchBar";
WebElement elementByCss = FindElement(driver, By.cssSelector(loc_css), 4);
if(elementByCss != null) {
System.out.println("Element is present with the given css selector : " + loc_css);
} else {
System.out.println("Element is not present with the given css selector : " + loc_css);
}
// Find element using ClassName locator
String loc_class = "search_input";
WebElement elementByClass = FindElement(driver, By.className(loc_class), 4);
if(elementByClass != null) {
System.out.println("Element is present with the given class name : " + loc_class);
} else {
System.out.println("Element is not present with the given class name : " + loc_class);
}
// Find element using TagName locator
String loc_tag = "button";
WebElement elementByTag = FindElement(driver, By.tagName(loc_tag), 4);
if(elementByTag != null) {
System.out.println("Element is present with the given tag name : " + loc_tag);
} else {
System.out.println("Element is not present with the given tag name : " + loc_tag);
}
// Find element using LinkText locator
String loc_link = "Log In";
WebElement elementByLink = FindElement(driver, By.linkText(loc_link), 4);
if(elementByLink != null) {
System.out.println("Element is present with the given link text : " + loc_link);
} else {
System.out.println("Element is not present with the given link text : " + loc_link);
}
// Find element using Partial Link Text locator
String loc_partial = "Log";
WebElement elementByPartial = FindElement(driver, By.partialLinkText(loc_partial), 4);
if(elementByPartial != null) {
System.out.println("Element is present with the given partial link text : " + loc_partial);
} else {
System.out.println("Element is not present with the given partial link text : " + loc_partial);
}
closeBrowser(driver);
}
private static WebElement FindElement(WebDriver driver, By by, int timeoutInSeconds) {
try {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
// throws a timeout exception if element not present after waiting timeoutInSeconds
wait.until(ExpectedConditions.presenceOfElementLocated(by));
return driver.findElement(by);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static WebDriver openBrowser(String browser, String DriverPath, String url) {
WebDriver driver;
if(browser == "chrome") {
System.setProperty("webdriver.chrome.driver", DriverPath);
driver = new ChromeDriver();
} else {
System.setProperty("webdriver.gecko.driver", DriverPath);
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.navigate().to(url);
return driver;
}
private static void closeBrowser(WebDriver driver) {
try {
driver.quit();
System.out.println("Browser closed successfully!");
} catch(Exception e) {
e.printStackTrace();
}
}
}