Skip to content

Commit 8653e7c

Browse files
Test the ability to mark todo items as completed
1 parent 612c2f9 commit 8653e7c

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/test/java/todomvc/acceptancetests/WhenCompletingATask.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
11
package todomvc.acceptancetests;
22

3+
import org.assertj.core.api.Assertions;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeEach;
36
import org.junit.jupiter.api.Test;
7+
import org.openqa.selenium.WebDriver;
8+
import org.openqa.selenium.chrome.ChromeDriver;
9+
import todomvc.pageactions.ToDoPage;
410

511
public class WhenCompletingATask {
12+
private ToDoPage toDoPage;
13+
private WebDriver driver;
14+
15+
@BeforeEach
16+
public void setUp(){
17+
driver = new ChromeDriver();
18+
toDoPage = new ToDoPage(driver);
19+
}
20+
21+
@AfterEach
22+
public void tearDown(){
23+
driver.quit();
24+
}
25+
626
// TODO: Exercise 3
727
@Test
828
public void activeTasksShouldNotShowCompletedTasks() {
929
// Add "Feed the cat" and "Walk the dog" to the list
1030
// Complete "Feed the cat"
1131
// Filter by "Active"
1232
// Check that only "Walk the dog" appears
33+
34+
toDoPage.openToDoApp();
35+
toDoPage.addItemsCalled("Feed the cat", "Walk the dog");
36+
toDoPage.completeItemWith("Feed the cat");
37+
toDoPage.filterBy("Active");
38+
39+
Assertions.assertThat(toDoPage.listOfTextsOfToDoItems()).containsExactly("Walk the dog");
1340
}
1441

1542
// TODO: Exercise 4
@@ -19,5 +46,12 @@ public void completedTasksShouldNotShowActiveTasks() {
1946
// Complete "Feed the cat"
2047
// Filter by "Completed"
2148
// Check that only "Feed the cat" appears
49+
50+
toDoPage.openToDoApp();
51+
toDoPage.addItemsCalled("Feed the cat", "Walk the dog");
52+
toDoPage.completeItemWith("Feed the cat");
53+
toDoPage.filterBy("Completed");
54+
55+
Assertions.assertThat(toDoPage.listOfTextsOfToDoItems()).containsExactly("Feed the cat");
2256
}
2357
}

src/test/java/todomvc/pageactions/ToDoPage.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void openToDoApp() {
1919
openPage("https://todomvc.com/examples/angular/dist/browser/#/all");
2020
}
2121

22-
public void addItem(String toDoText) {
22+
public void addItemCalled(String toDoText) {
2323
//With call to a static method, that returns a By Object
2424
getWebElementFor(ToDoPageSelectors.TODO_INPUTBOX()).sendKeys(toDoText);
2525
getWebElementFor(ToDoPageSelectors.TODO_INPUTBOX()).sendKeys(Keys.RETURN);
@@ -31,9 +31,19 @@ public List<String> listOfTextsOfToDoItems() {
3131
.collect(Collectors.toList());
3232
}
3333

34-
public void addItems(String... texts) {
34+
public void addItemsCalled(String... texts) {
3535
for(String text: texts){
36-
addItem(text);
36+
addItemCalled(text);
3737
}
3838
}
39+
40+
public void completeItemWith(String text) {
41+
//click on the checkbox associated the todo item to complete
42+
getWebElementFor(ToDoPageSelectors.TODO_ITEM_CHECKBOX(text)).click();
43+
}
44+
45+
public void filterBy(String linkText) {
46+
//click on the filter
47+
getWebElementFor(ToDoPageSelectors.FILTER_LINK(linkText)).click();
48+
}
3949
}

src/test/java/todomvc/pageselectors/ToDoPageSelectors.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ public static By TODO_INPUTBOX(){
2020
public static By LIST_OF_TODO_ITEMS() {
2121
return By.cssSelector("ul.todo-list li");
2222
}
23+
24+
public static By TODO_ITEM_CHECKBOX(String text) {
25+
return By.xpath("//ul[@class='todo-list']//label[text()='"+text+"']/preceding-sibling::input[@type='checkbox']");
26+
}
27+
28+
public static By FILTER_LINK(String linkText) {
29+
return By.xpath("//a[text()='"+linkText+"']");
30+
}
2331
}

0 commit comments

Comments
 (0)