2
Answers

Go Lang API testing in Playwright

Photo of Kumar AU

Kumar AU

Jun 11
552
1

Hi,

I am new to Go language, can you please let me know , how do we test API’s written in Go language using playwright. 

Any sample code or good article about Go lang API testing using playwright.

Note :- I am unable to find any article in google testing Go lang API’s using playwright

Any help will be much appreciated.

Answers (2)

3
Photo of Naimish Makwana
132 13.8k 203.1k Jun 11

Testing APIs written in Go using Playwright involves a few steps since Playwright is primarily a Node.js library. However, there is a Go library for Playwright maintained by the community that you can use. Here’s a general approach to testing your Go APIs with Playwright:

  1. Install Playwright for Go: You can install the Playwright Go library using the following command:

    go get -u github.com/playwright-community/playwright-go
    
  2. Write Test Cases: You’ll write test cases using the Playwright API to send requests to your Go server and verify the responses. Below is a sample code snippet that demonstrates how you might write a test case to check the response of an API endpoint:

    package main
    
    import (
        "fmt"
        "github.com/playwright-community/playwright-go"
    )
    
    func main() {
        pw, _ := playwright.Run()
        browser, _ := pw.Chromium.Launch()
        page, _ := browser.NewPage()
    
        // Replace with your API endpoint
        response, _ := page.Goto("http://localhost:8080/your-api-endpoint")
    
        // Check if the response status is 200 (OK)
        if response.Status() == 200 {
            fmt.Println("API endpoint is working as expected.")
        } else {
            fmt.Println("API endpoint is not working as expected.")
        }
    
        _ = browser.Close()
        _ = pw.Stop()
    }
    
  3. Run Your Tests: Execute your tests using the Go command line tool. If you have set up your tests in a file named api_test.go, you can run them with:

    go test api_test.go
    

For more detailed information and examples, you can refer to the Playwright for Go GitHub repository and the Playwright API testing guide. These resources should give you a good starting point for writing and running your API tests using Playwright in Go.

https://github.com/playwright-community/playwright-go

https://playwright.dev/docs/api-testing

Remember, since Playwright is a Node.js library, the community-driven Go version may not have all the features of the official Playwright library, and you might need to handle some of the inter-language interoperability yourself. Keep this in mind as you develop your tests.

Thanks

2
Photo of Kumar AU
1k 322 67.6k Jun 11

Thank you Naimish for your response with the detailed explanation.

When I run the program (File name – api_test.go) , I get this message, do you have any idea ?

My local Project Folder - C:\Users\XXXXXX\source\repos\GoLang_Training_1\Playwright_Go>

Note :- API end point is running in my local, i am able to access the same endpoint in postman