forked from yuriy-logosha/selenium_java_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample6Task.java
More file actions
67 lines (60 loc) · 3.05 KB
/
Sample6Task.java
File metadata and controls
67 lines (60 loc) · 3.05 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
package selenium.sample;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.assertEquals;
public class Sample6Task {
WebDriver driver;
// method which is being run before each test
@Before
public void startingTests() throws Exception {
// from Sample 1:
String libWithDriversLocation = System.getProperty("user.dir") + "\\lib\\";
System.setProperty("webdriver.chrome.driver", libWithDriversLocation + "chromedriver.exe");
// declaration above:
driver = new ChromeDriver();
//open page:
driver.get("https://kristinek.github.io/site/examples/locators");
}
// method which is being run after each test
@After
public void endingTests() throws Exception {
driver.quit();
}
@Test
public void findElementByXPath() throws Exception {
// 2 ways to find text: "Heading 2 text":
assertEquals("Heading 2 text", driver.findElement(By.xpath("//*[@id='heading_2']")).getText());
assertEquals("Heading 2 text", driver.findElement(By.xpath("//h2[@id='heading_2']")).getText());
// 1-2 ways to find text: "Test Text 1"
assertEquals("Test Text 1", driver.findElement(By.xpath("//*[@id='test1']/p[1]")).getText());
// 1-2 ways to find text: "Test Text 2"
assertEquals("Test Text 2", driver.findElement(By.xpath("//*[@id='test1']/p[2]")).getText());
// 1-2 ways to find text: "Test Text 3"
assertEquals("Test Text 3", driver.findElement(By.xpath("//*[@id='test3']/p[1]")).getText());
// 1-2 ways to find text: "Test Text 4"
assertEquals("Test Text 4", driver.findElement(By.xpath("//*[@id='test3']/p[2]")).getText());
// 1-2 ways to find text: "Test Text 5"
assertEquals("Test Text 5", driver.findElement(By.xpath("//*[@id='test2']/p[1]")).getText());
// 1-2 ways to find text: "This is also a button"
assertEquals("This is also a button",
driver.findElement(By.xpath("//input[@name='randomButton2']")).getAttribute("value"));
}
@Test
public void findElementByCssName() throws Exception {
// 1-2 ways to find text: "Heading 2 text"
assertEquals("Heading 2 text", driver.findElement(By.cssSelector("h2#heading_2")).getText());
// 1-2 ways to find text: "Test Text 1"
assertEquals("Test Text 1", driver.findElement(By.cssSelector("#test1>p:first-child")).getText());
// 1-2 ways to find text: "Test Text 2"
assertEquals("Test Text 2", driver.findElement(By.cssSelector("#test1>p:nth-child(2)")).getText());
// 1-2 ways to find text: "Test Text 3"
assertEquals("Test Text 3", driver.findElement(By.cssSelector("#test3>p:nth-child(1)")).getText());
// 1-2 ways to find text: "This is also a button"
assertEquals("This is also a button",
driver.findElement(By.cssSelector("input[name='randomButton2']")).getAttribute("value"));
}
}