...

Source file src/github.com/playwright-community/playwright-go/examples/end-to-end-testing/main.go

Documentation: github.com/playwright-community/playwright-go/examples/end-to-end-testing

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"log"
     9  	"reflect"
    10  
    11  	"github.com/playwright-community/playwright-go"
    12  )
    13  
    14  func assertErrorToNilf(message string, err error) {
    15  	if err != nil {
    16  		log.Fatalf(message, err)
    17  	}
    18  }
    19  
    20  func assertEqual(expected, actual interface{}) {
    21  	if !reflect.DeepEqual(expected, actual) {
    22  		panic(fmt.Sprintf("%v does not equal %v", actual, expected))
    23  	}
    24  }
    25  
    26  const todoName = "Bake a cake"
    27  
    28  func main() {
    29  	pw, err := playwright.Run()
    30  	assertErrorToNilf("could not launch playwright: %w", err)
    31  	browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
    32  		Headless: playwright.Bool(false),
    33  	})
    34  	assertErrorToNilf("could not launch Chromium: %w", err)
    35  	context, err := browser.NewContext()
    36  	assertErrorToNilf("could not create context: %w", err)
    37  	page, err := context.NewPage()
    38  	assertErrorToNilf("could not create page: %w", err)
    39  	_, err = page.Goto("http://todomvc.com/examples/react/")
    40  	assertErrorToNilf("could not goto: %w", err)
    41  
    42  	// Helper function to get the amount of todos on the page
    43  	assertCountOfTodos := func(shouldBeCount int) {
    44  		count, err := page.EvalOnSelectorAll("ul.todo-list > li", "el => el.length")
    45  		assertErrorToNilf("could not determine todo list count: %w", err)
    46  		assertEqual(shouldBeCount, count)
    47  	}
    48  
    49  	// Initially there should be 0 entries
    50  	assertCountOfTodos(0)
    51  
    52  	// Adding a todo entry (click in the input, enter the todo title and press the Enter key)
    53  	assertErrorToNilf("could not click: %v", page.Click("input.new-todo"))
    54  	assertErrorToNilf("could not type: %v", page.Type("input.new-todo", todoName))
    55  	assertErrorToNilf("could not press: %v", page.Press("input.new-todo", "Enter"))
    56  
    57  	// After adding 1 there should be 1 entry in the list
    58  	assertCountOfTodos(1)
    59  
    60  	// Here we get the text in the first todo item to see if it"s the same which the user has entered
    61  	textContentOfFirstTodoEntry, err := page.EvalOnSelector("ul.todo-list > li:nth-child(1) label", "el => el.textContent")
    62  	assertErrorToNilf("could not get first todo entry: %w", err)
    63  	assertEqual(todoName, textContentOfFirstTodoEntry)
    64  
    65  	// The todo list should be persistent. Here we reload the page and see if the entry is still there
    66  	_, err = page.Reload()
    67  	assertErrorToNilf("could not reload: %w", err)
    68  	assertCountOfTodos(1)
    69  
    70  	// Set the entry to completed
    71  	assertErrorToNilf("could not click: %v", page.Click("input.toggle"))
    72  
    73  	// Filter for active entries. There should be 0, because we have completed the entry already
    74  	assertErrorToNilf("could not click: %v", page.Click("text=Active"))
    75  	assertCountOfTodos(0)
    76  
    77  	// If we filter now for completed entries, there should be 1
    78  	assertErrorToNilf("could not click: %v", page.Click("text=Completed"))
    79  	assertCountOfTodos(1)
    80  
    81  	// Clear the list of completed entries, then it should be again 0
    82  	assertErrorToNilf("could not click: %v", page.Click("text=Clear completed"))
    83  	assertCountOfTodos(0)
    84  
    85  	assertErrorToNilf("could not close browser: %w", browser.Close())
    86  	assertErrorToNilf("could not stop Playwright: %w", pw.Stop())
    87  }
    88  

View as plain text