...

Source file src/github.com/playwright-community/playwright-go/tests/remote_server_test.go

Documentation: github.com/playwright-community/playwright-go/tests

     1  package playwright_test
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"runtime"
    10  	"strings"
    11  
    12  	"github.com/playwright-community/playwright-go"
    13  )
    14  
    15  type remoteServer struct {
    16  	url string
    17  	cmd *exec.Cmd
    18  }
    19  
    20  func newRemoteServer() (*remoteServer, error) {
    21  	driver, err := playwright.NewDriver(&playwright.RunOptions{})
    22  	if err != nil {
    23  		return nil, fmt.Errorf("could not start Playwright: %v", err)
    24  	}
    25  	node := "node"
    26  	if runtime.GOOS == "windows" {
    27  		node = "node.exe"
    28  	}
    29  	cliJs := filepath.Join(driver.DriverDirectory, "package", "lib", "cli", "cli.js")
    30  	cmd := exec.Command(filepath.Join(driver.DriverDirectory, node), cliJs, "launch-server", "--browser", browserName)
    31  	cmd.Stderr = os.Stderr
    32  	stdout, err := cmd.StdoutPipe()
    33  	if err != nil {
    34  		return nil, fmt.Errorf("could not get stdout pipe: %v", err)
    35  	}
    36  	err = cmd.Start()
    37  	if err != nil {
    38  		return nil, fmt.Errorf("could not start server: %v", err)
    39  	}
    40  	scanner := bufio.NewReader(stdout)
    41  	url, err := scanner.ReadString('\n')
    42  	url = strings.TrimRight(url, "\n")
    43  	if err != nil {
    44  		return nil, fmt.Errorf("could not read url: %v", err)
    45  	}
    46  	return &remoteServer{
    47  		url: url,
    48  		cmd: cmd,
    49  	}, nil
    50  }
    51  
    52  func (s *remoteServer) Close() {
    53  	_ = s.cmd.Process.Kill()
    54  	_ = s.cmd.Wait()
    55  }
    56  

View as plain text