-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSample2Task.java
72 lines (60 loc) · 2.41 KB
/
Sample2Task.java
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
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Sample2Task {
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 findElementByID() throws Exception {
// get text "Heading 2 text" using id
System.out.println(driver.findElement(By.id("heading_2")).getText());
}
@Test
public void findElementByName() throws Exception {
// TODO:
// get attribute "id" and "value" of button "This is also a button" using name
System.out.println(driver.findElement(By.name("randomButton2")).getAttribute("id"));
System.out.println(driver.findElement(By.name("randomButton2")).getAttribute("value"));
}
@Test
public void findElementByClassFirst() throws Exception {
System.out.println(driver.findElement(By.className("test")).getText());
// TODO:
// get first text of class "test" (should be "Test Text 1")
}
@Test
public void findElementByClassAll() throws Exception {
// get size text of class "test" (should be 5)
System.out.println(driver.findElements(By.className("test")).size());
// get text of class "test"
List<WebElement> elements = driver.findElements(By.className("test"));
for (WebElement element : elements) {
System.out.println(element.getText());
System.out.println(element.getAttribute("class"));
}
// get third text of class "test" (should be "Test Text 4")
System.out.println(driver.findElements(By.className("test")).get(2).getText());
System.out.println(elements.get(2).getText());
}
}