- Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSample2.java
More file actions
Latest commit
81 lines (71 loc) · 3.31 KB
/
Copy pathSample2.java
File metadata and controls
81 lines (71 loc) · 3.31 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
packageselenium.sample;
importorg.junit.After;
importorg.junit.Before;
importorg.junit.Test;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.chrome.ChromeDriver;
importjava.io.File;
importjava.util.List;
publicclassSample2 {
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.quit();
}
@Test
publicvoidfindElementByID() throwsException {
// works:
System.out.println(driver.findElement(By.id("heading_1")).getText());
// will fail with error NoSuchElementException
// no such element:
// Unable to locate element: {"method":"id","selector":"headingasdga"}
// System.out.println(driver.findElement(By.id("headingasdga")).getText());
}
@Test
publicvoidfindElementByName() throwsException {
// From page:
// <input type="button" value="This is a button" name="randomButton" />
System.out.println(driver.findElement(By.name("randomButton1")).getAttribute("value")); // "This is a button"
System.out.println(driver.findElement(By.name("randomButton1")).getAttribute("type")); // "button"
System.out.println(driver.findElement(By.name("randomButton1")).getAttribute("name")); // "randomButton"
System.out.println(driver.findElement(By.name("randomButton1")).getAttribute("id")); // empty
// Unable to locate element: {"method":"name","selector":"randomButton123"}:
// System.out.println(driver.findElement(By.name("randomButton123")).getAttribute("value"));
}
@Test
publicvoidfindElementByClassFirst() throwsException {
System.out.println(driver.findElement(By.className("text")).getText()); // "sample text 1"
}
@Test
publicvoidfindElementByClassAll() throwsException {
System.out.println(driver.findElements(By.id("headingasdga")).size()); // 0
System.out.println(driver.findElements(By.className("text")).size()); // 5
List<WebElement> allElementsWithClass = driver.findElements(By.className("text"));
//
for (WebElementelementWithClass : allElementsWithClass) {
System.out.println(elementWithClass.getText());
// sample text 1
// sample text 2
// unbelievable sample text
// amazing sample text
// dummy text
}
System.out.println("-----------------------");
System.out.println(driver.findElements(By.className("text")).get(0).getText());
System.out.println(driver.findElements(By.className("text")).get(2).getText()); // "unbelievable sample text"
}
}