Skip to content

Commit

Permalink
page objects: break out displayed list related functionality to its o…
Browse files Browse the repository at this point in the history
…wn page class
  • Loading branch information
ashwiniraokarai committed Apr 23, 2024
1 parent 574709a commit 57650ff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
12 changes: 10 additions & 2 deletions tests/adding-items.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { test, expect, Locator } from "@playwright/test";
import { TodoForm } from "./page-classes/todo-form.ts";
import { TodoList } from "./page-classes/todo-list.ts";

let todoForm: TodoForm;
let newTodoField: Locator;
let displayedTodoItems: Locator;

let todoList: TodoList;
let countOfRemainingToDos: Locator;

let displayedTodoItems: Locator;


test.beforeEach(
async({ page })=>{
await page.goto("https://todomvc.com/examples/emberjs/todomvc/dist/");

//Invoke the page object to grab locator(s)
todoForm = new TodoForm(page);
newTodoField = todoForm.newToDoField();
countOfRemainingToDos = todoForm.countOfRemainingToDos();

todoList = new TodoList(page);
countOfRemainingToDos = todoList.countOfRemainingToDos();

//Instead of the li, you could even target the label inside the li directly which embeds the text, like so:
//".todo-list label"
Expand Down Expand Up @@ -64,6 +71,7 @@ test.describe("when adding multiple todo items",
)
test("should be shown all the added items",
async( {page} ) => {
await expect(displayedTodoItems).toHaveCount(2);
await expect(displayedTodoItems).toHaveText(["feed the dog", "snuggle with the cat"]);
}
);
Expand Down
4 changes: 0 additions & 4 deletions tests/page-classes/todo-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ export class TodoForm {
newToDoField(){
return this.page.locator(".new-todo");
}

countOfRemainingToDos(){
return this.page.locator(".todo-count");
}

/*
********************************************************************************
Expand Down
28 changes: 28 additions & 0 deletions tests/page-classes/todo-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Page } from "@playwright/test"

export class TodoList{
page: Page;

constructor(page: Page){
this.page = page;
}

/*
********************************************************************************
Functions that return page locators.
Each function returns a Type called "Locator" which in turn resolves to the elements
right before performing an action.
********************************************************************************
*/

countOfRemainingToDos(){
return this.page.locator(".todo-count");
}

/*
********************************************************************************
Functions that act on page locators (locators
resolve to elements right before they're acted on)
********************************************************************************
*/
}

0 comments on commit 57650ff

Please sign in to comment.