-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindElementDemo.java
More file actions
65 lines (53 loc) · 1.74 KB
/
FindElementDemo.java
File metadata and controls
65 lines (53 loc) · 1.74 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
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FindElementDemo {
private WebDriver driver;
@Before
public void setUp() {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver");
driver = new ChromeDriver();
}
@After
public void tearDown() {
driver.quit();
}
@Test
public void testFindElementById() {
driver.get("http://localhost:8080/workspace/helloworld.html");
WebElement h1 = driver.findElement(By.id("title"));
assertEquals("Hello World", h1.getText());
}
@Test
public void testFindElementByName() {
driver.get("http://localhost:8080/workspace/search.html");
WebElement input = driver.findElement(By.name("keyword"));
assertEquals("Type in your keyword", input.getAttribute("value"));
}
@Test
public void testFindElementByTagName() {
driver.get("http://localhost:8080/workspace/search.html");
WebElement label = driver.findElement(By.tagName("label"));
assertEquals("Search2", label.getText());
}
@Test
public void testFindElementsByTagName() {
driver.get("http://localhost:8080/workspace/search.html");
List<WebElement> labels = driver.findElements(By.tagName("label"));
assertEquals(2, labels.size());
String[] expected = {"Search2", "Search"};
List<String> actual = new ArrayList();
for (WebElement label: labels) {
actual.add(label.getText());
}
assertArrayEquals(expected, actual.toArray());
}
}