- Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSample6.java
More file actions
Latest commit
193 lines (170 loc) · 10.9 KB
/
Copy pathSample6.java
File metadata and controls
193 lines (170 loc) · 10.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
packageselenium.sample;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.chrome.ChromeDriver;
importjava.io.File;
publicclassSample6 {
WebDriverdriver;
// method which is being run before each test
@Before
publicvoidstartingTests() throwsException {
// from Sample 1:
StringlibWithDriversLocation = System.getProperty("user.dir") + File.separator + "lib" + File.separator;
System.setProperty("webdriver.chrome.driver", libWithDriversLocation + "chromedriver" + newselenium.ChangeToFileExtension().extension());
// declaration above:
driver = newChromeDriver();
//open page:
driver.get("https://kristinek.github.io/site/examples/locators");
}
// method which is being run after each test
@After
publicvoidendingTests() throwsException {
driver.close();
}
@Test
publicvoidfindElementById() throwsException {
// find element by id using xPath
System.out.println("Find element by id using xPath:");
System.out.println("\t text of element with id 'heading_1' is '" +
driver.findElement(By.xpath("//*[@id='heading_1']")).getText() + "'");
System.out.println("\t text of element with id 'standartText' is '" +
driver.findElement(By.xpath("//*[@id='standartText']")).getText() + "'");
System.out.println("\t text of element with id 'nonStandartText' is '" +
driver.findElement(By.xpath("//*[@id='nonStandartText']")).getText() + "'");
// find element by id using CSS
System.out.println("Find element by id using CSS:");
System.out.println("\t text of element with id 'heading_1' is '" +
driver.findElement(By.cssSelector("#heading_1")).getText() + "'");
System.out.println("\t text of element with id 'standartText' is '" +
driver.findElement(By.cssSelector("#standartText")).getText() + "'");
System.out.println("\t text of element with id 'nonStandartText' is '" +
driver.findElement(By.cssSelector("#nonStandartText")).getText() + "'");
}
@Test
publicvoidfindElementByAllClasses() throwsException {
// find element by all classes using xPath
System.out.println("Find element by all classes using xPath:");
System.out.println("\t text of element with class 'text' is '" +
driver.findElement(By.xpath("//*[@class='text']")).getText() + "'");
System.out.println("\t text of element with class 'text' and 'unbelievable' is '" +
driver.findElement(By.xpath("//*[@class=\"text unbelievable\"]")).getText() + "'");
// find element by all classes using CSS
System.out.println("Find element by all classes using CSS:");
System.out.println("\t text of element with class 'text' is '" +
driver.findElement(By.cssSelector(".text")).getText() + "'");
System.out.println("\t text of element with classes 'text' and 'unbelievable' is '" +
driver.findElement(By.cssSelector(".text.unbelievable")).getText() + "'");
System.out.println("\t text of element with classes 'text' and 'unbelievable' is '" +
driver.findElement(By.cssSelector(".unbelievable.text")).getText() + "'");
}
@Test
publicvoidfindElementBy1stClass() throwsException {
// find element by 1 of nth class using CSS
System.out.println("Find element by 1 of nth class using xPath:");
System.out.println("\t text of element with class 'unbelievable' is '" +
driver.findElement(By.xpath("//*[contains(@class, 'unbelievable')]")).getText() + "'");
// find element by 1 of nth class using CSS
System.out.println("Find element by 1 of nth class using CSS:");
System.out.println("\t text of element with class 'unbelievable' is '" +
driver.findElement(By.cssSelector(".unbelievable")).getText() + "'");
}
@Test
publicvoidfindElementByIdAndClass() throwsException {
// find element by id and class using xPath
System.out.println("Find element by id and classes using xPath:");
System.out.println("\t text of element with class 'text' and id 'dummy' is '" +
driver.findElement(By.xpath("//*[contains(@class, 'text') and @id='dummy']")).getText() + "'");
System.out.println("\t text of element with class 'text' and id 'dummy' v2 is '" +
driver.findElement(By.xpath("//*[@class='text' and @id='dummy']")).getText() + "'");
// find element by id and class using CSS
System.out.println("Find element by id and classes using CSS:");
System.out.println("\t text of element with class 'text' and id 'dummy' is '" +
driver.findElement(By.cssSelector(".text#dummy")).getText() + "'");
System.out.println("\t text of element with class 'text' and id 'dummy' v2 is '" +
driver.findElement(By.cssSelector("#dummy.text")).getText() + "'");
}
@Test
publicvoidfindElementByText() throwsException {
// find element by Text using xPath
System.out.println("Find element by Text using xPath:");
System.out.println("\t class of element with text 'sample text 1' is '" +
driver.findElement(By.xpath("//*[text()='sample text 1']")).getAttribute("class") + "'");
System.out.println("\t text of element which contains text 'dummy' is '" +
driver.findElement(By.xpath("//*[contains(text(), 'dummy')]")).getText() + "'");
}
@Test
publicvoidfindElementByTagName() throwsException {
// find element by tag name using xPath
System.out.println("Find element by tag name using xPath:");
System.out.println("\t text of element with tag name 'h2' is '" +
driver.findElement(By.xpath("//h2")).getText() + "'");
System.out.println("\t value of element with tag name 'input' is '" +
driver.findElement(By.xpath("//input")).getAttribute("value") + "'");
// find element by tag name using CSS
System.out.println("Find element by tag name using CSS:");
System.out.println("\t text of element with tag name 'h2' is '" +
driver.findElement(By.cssSelector("h2")).getText() + "'");
System.out.println("\t value of element with tag name 'input' is '" +
driver.findElement(By.cssSelector("input")).getAttribute("value") + "'");
}
@Test
publicvoidfindNthElement() throwsException {
// find nth element using xPath
System.out.println("Find element nth using xPath:");
System.out.println("\t text of 1-st element with tag name 'p' is '" +
driver.findElement(By.xpath("//p[1]")).getText() + "'");
System.out.println("\t text of 3-rd element with tag name 'p' is '" +
driver.findElement(By.xpath("//p[3]")).getText() + "'");
// find nth element using CSS
System.out.println("Find element nth using CSS:");
System.out.println("\t text of 1-st element with tag name 'p' is '" +
driver.findElement(By.cssSelector("p:nth-of-type(1)")).getText() + "'");
System.out.println("\t text of 3-rd element with tag name 'p' is '" +
driver.findElement(By.cssSelector("p:nth-of-type(3)")).getText() + "'");
}
@Test
publicvoidfindElementByOtherAttributes() throwsException {
// find element by other attributes using xPath (e.g. name)
System.out.println("Find element by other attributes using xPath:");
System.out.println("\t value of element with name 'randomButton1' is '" +
driver.findElement(By.xpath("//*[@name='randomButton1']")).getAttribute("value") + "'");
System.out.println("\t name of element with value 'This is a button' is '" +
driver.findElement(By.xpath("//*[@value='This is a button']")).getAttribute("name") + "'");
System.out.println("\t value of element with type 'button' is '" +
driver.findElement(By.xpath("//*[@type='button']")).getAttribute("value") + "'");
// find child element using CSS
System.out.println("Find element by other attributes using CSS:");
System.out.println("\t value of element with name 'randomButton1' is '" +
driver.findElement(By.cssSelector("[name='randomButton1']")).getAttribute("value") + "'");
System.out.println("\t name of element with value 'This is a button' is '" +
driver.findElement(By.cssSelector("[value='This is also a button']")).getAttribute("name") + "'");
System.out.println("\t value of element with type 'button' is '" +
driver.findElement(By.cssSelector("[type='button']")).getAttribute("value") + "'");
}
@Test
publicvoidfindChildOrDescendantElement() throwsException {
// find child element using xPath
System.out.println("Find child element using xPath:");
System.out.println("\t text of element with tag name 'p' which is a direct child of element with tag name 'div' is '" +
driver.findElement(By.xpath("//div/p")).getText() + "'");
System.out.println("\t text of element with tag name 'p' which 3-rd a direct child of element with tag name 'div' is '" +
driver.findElement(By.xpath("//div/p[3]")).getText() + "'");
System.out.println("\t text of element with tag name 'p' which is a descendant child of element with tag name 'body' is '" +
driver.findElement(By.xpath("//body//p")).getText() + "'");
System.out.println("\t text of element with class 'amazing' which is a direct child of element with id 'nonStandartText' is '" +
driver.findElement(By.xpath("//*[@id='nonStandartText']/p[contains(@class, 'amazing')]")).getText() + "'");
// find element by other attributes using CSS (e.g. name)
System.out.println("Find child element using CSS:");
System.out.println("\t text of element with tag name 'p' which is a direct child of element with tag name 'div' is '" +
driver.findElement(By.cssSelector("div > p")).getText() + "'");
System.out.println("\t text of element with tag name 'p' which 3-rd a direct child of element with tag name 'div' is '" +
driver.findElement(By.cssSelector("div > p:nth-child(3)")).getText() + "'");
System.out.println("\t text of element with tag name 'p' which is a descendant child of element with tag name 'body' is '" +
driver.findElement(By.cssSelector("body p")).getText() + "'");
System.out.println("\t text of element with class 'amazing' which is a direct child of element with id 'nonStandartText' is '" +
driver.findElement(By.cssSelector("#nonStandartText .amazing")).getText() + "'");
}
}