-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSample4.java
123 lines (95 loc) · 5.25 KB
/
Sample4.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
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
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 static org.junit.Assert.*;
public class Sample4 {
WebDriver driver;
String base_url = "https://kristinek.github.io/site/examples/actions";
// 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(base_url);
}
// method which is being run after each test
@After
public void endingTests() throws Exception {
driver.quit();
}
@Test
public void clickLink() throws Exception {
assertEquals(base_url, driver.getCurrentUrl());
driver.findElement(By.id("link1")).click();
assertEquals("Link Page 1", driver.findElement(By.id("h1")).getText());
assertFalse(driver.getCurrentUrl().equals(base_url));
assertEquals("https://kristinek.github.io/site/examples/link1", driver.getCurrentUrl());
}
@Test
public void clickButtonAndSeeOrHideText() throws Exception {
WebElement text = driver.findElement(By.id("show_me"));
WebElement showButton = driver.findElement(By.id("show_text"));
WebElement hideButton = driver.findElement(By.name("hide_text"));
assertFalse(text.isDisplayed()); // "I am here!" is NOT seen
assertTrue(showButton.isEnabled()); // "Show" button is enabled (clickable)
assertFalse(hideButton.isEnabled()); // "Hide" button is NOT enabled (clickable)
showButton.click(); // clicking on "Show" button
assertTrue(text.isDisplayed()); // "I am here!" is seen
assertFalse(showButton.isEnabled()); // "Show" button is NOT enabled (clickable)
assertTrue(hideButton.isEnabled()); // "Hide" button is enabled (clickable)
hideButton.click(); // clicking on "Hide" button
assertFalse(text.isDisplayed()); // "I am here!" is NOT seen
assertTrue(showButton.isEnabled()); // "Show" button is enabled (clickable)
assertFalse(hideButton.isEnabled()); // "Hide" button is NOT enabled (clickable)
}
@Test
public void enterTextInTextArea() throws Exception {
WebElement textArea = driver.findElement(By.id("text_area"));
String originalTextInTextArea = "This is a text area";
String newTextOne = " sending some keys";
String newTextTwo = "New text in text area\nOn two lines";
assertEquals(textArea.getText(), originalTextInTextArea); // checking that getText is "This is a text area"
assertEquals(textArea.getAttribute("value"), originalTextInTextArea); // checking that value is "This is a text area"
textArea.sendKeys(newTextOne); // sending keys " sending some keys"
assertEquals(textArea.getText(), originalTextInTextArea); // checking that getText is "This is a text area"
assertEquals(textArea.getAttribute("value"), originalTextInTextArea + newTextOne);
// checking that value is "This is a text area" + " sending some keys"
textArea.clear();
assertEquals(textArea.getText(), originalTextInTextArea); // checking that getText is "This is a text area"
assertEquals(textArea.getAttribute("value"), ""); // checking that value is empty or ""
textArea.sendKeys(newTextTwo);
assertEquals(textArea.getText(), originalTextInTextArea); // checking that getText is "This is a text area"
assertEquals(textArea.getAttribute("value"), newTextTwo); // checking that value is "New text in text area"
assertFalse(textArea.getAttribute("value").contains(originalTextInTextArea));
// checking that value is NOT "This is a text area"
}
@Test
public void enterTextInTextBox() throws Exception {
WebElement textInput = driver.findElement(By.id("text"));
String originalText = "This is a text box";
String sendKeyOne = " bla-bla";
assertEquals(textInput.getText(), ""); // checking that getText is empty
assertEquals(textInput.getAttribute("value"), originalText); // checking that value is "This is a text box"
textInput.sendKeys(sendKeyOne);
assertEquals(textInput.getText(), ""); // checking that getText is empty
assertEquals(textInput.getAttribute("value"), originalText + sendKeyOne);
// checking that value is "This is a text box" + " bla-bla"
textInput.clear();
assertEquals(textInput.getText(), ""); // checking that getText is empty
assertEquals(textInput.getAttribute("value"), ""); // checking that value is also empty
textInput.sendKeys(sendKeyOne);
assertEquals(textInput.getText(), ""); // checking that getText is empty
assertEquals(textInput.getAttribute("value"), sendKeyOne);
// checking that value is "This is a text box" + " bla-bla"
assertFalse(textInput.getAttribute("value").contains(originalText));
}
}