- Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSample7.java
More file actions
Latest commit
143 lines (118 loc) · 5.05 KB
/
Copy pathSample7.java
File metadata and controls
143 lines (118 loc) · 5.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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;
importorg.openqa.selenium.support.ui.Select;
importjava.io.File;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.List;
importstaticorg.junit.Assert.*;
publicclassSample7 {
WebDriverdriver;
Stringbase_url = "https://kristinek.github.io/site/examples/actions";
// 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(base_url);
}
// method which is being run after each test
@After
publicvoidendingTests() throwsException {
driver.close();
}
@Test
publicvoidselectCheckBox() throwsException {
List<WebElement> checkBoxes = driver.findElements(By.cssSelector(".w3-check[type='checkbox']"));
for (WebElementcheckBox : checkBoxes) {
assertFalse(checkBox.isSelected()); // checkboxes are NOT selected
checkBox.click();
assertTrue(checkBox.isSelected()); // checkboxes are selected
checkBox.click();
assertFalse(checkBox.isSelected()); // checkboxes are NOT selected
}
WebElementoption3 = driver.findElement(By.cssSelector(".w3-check[value='Option 3'][type='checkbox']"));
assertFalse(option3.isSelected());
option3.click();
assertTrue(option3.isSelected());
}
@Test
publicvoidselectRadioButton() throwsException {
List<WebElement> radioButtons = driver.findElements(By.cssSelector(".w3-check[type='radio']"));
for (WebElementradioButton : radioButtons) {
assertFalse(radioButton.isSelected()); // radio are NOT selected
radioButton.click();
assertTrue(radioButton.isSelected()); // radio are selected
}
WebElementoption2 = driver.findElement(By.cssSelector(".w3-check[value='Option 2'][type='radio'"));
assertFalse(option2.isSelected());
option2.click();
assertTrue(option2.isSelected());
}
@Test
publicvoidselectOptionByText() throwsException {
Selectdropdown = newSelect(driver.findElement(By.id("vfb-12")));
assertEquals("Choose your option", dropdown.getFirstSelectedOption().getText());
dropdown.selectByVisibleText("Option 2");
assertEquals("Option 2", dropdown.getFirstSelectedOption().getText());
}
@Test
publicvoidselectOptionByIndex() throwsException {
Selectdropdown = newSelect(driver.findElement(By.id("vfb-12")));
assertEquals("Choose your option", dropdown.getFirstSelectedOption().getText());
dropdown.selectByIndex(1);
assertEquals("Option 1", dropdown.getFirstSelectedOption().getText());
}
@Test
publicvoidselectOptionByValue() throwsException {
Selectdropdown = newSelect(driver.findElement(By.id("vfb-12")));
assertEquals("Choose your option", dropdown.getFirstSelectedOption().getText());
dropdown.selectByValue("value3");
assertEquals("Option 3", dropdown.getFirstSelectedOption().getText());
}
@Test
publicvoidtestDragAndDrop() throwsException {
StringdragElement = "#black_box";
StringtoTarget = "#drag_box2";
Sample7DragAndDropMagic.dragAndDropMagic(driver, dragElement, toTarget);
}
@Test
publicvoidchooseDateViaCalendar() throwsException {
// get today date
Calendarcal = Calendar.getInstance();
// go back 10 month
cal.add(Calendar.MONTH, -10);
Stringresult = newSimpleDateFormat("MM/15/yyyy").format(cal.getTime());
WebElementdateBox = driver.findElement(By.id("vfb-8"));
assertEquals("", dateBox.getAttribute("value"));
dateBox.click();
WebElementdateWidget = driver.findElement(By.id("ui-datepicker-div"));
// go back 10 month in calendar on page
for (inti = 0; i < 10; i++) {
dateWidget.findElement(By.className("ui-datepicker-prev")).click();
}
// select date 15
dateWidget.findElement(By.xpath("//a[text()='15']")).click();
assertEquals(result, dateBox.getAttribute("value"));
dateBox.clear();
}
@Test
publicvoidchooseDateViaTextBox() throwsException {
StringdateToEnter = "12/15/2014";
WebElementdateBox = driver.findElement(By.id("vfb-8"));
assertEquals("", dateBox.getAttribute("value"));
dateBox.clear();
dateBox.sendKeys(dateToEnter);
assertEquals(dateToEnter, dateBox.getAttribute("value"));
}
}