...

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

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

     1  package playwright_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestDownloadBasic(t *testing.T) {
    15  	BeforeEach(t)
    16  	defer AfterEach(t)
    17  	server.SetRoute("/downloadWithFilename", func(w http.ResponseWriter, r *http.Request) {
    18  		w.Header().Add("Content-Type", "application/octet-stream")
    19  		w.Header().Add("Content-Disposition", "attachment; filename=file.txt")
    20  		if _, err := w.Write([]byte("foobar")); err != nil {
    21  			log.Printf("could not write: %v", err)
    22  		}
    23  	})
    24  	require.NoError(t, page.SetContent(
    25  		fmt.Sprintf(`<a href="%s/downloadWithFilename">download</a>`, server.PREFIX),
    26  	))
    27  
    28  	download, err := page.ExpectDownload(func() error {
    29  		return page.Click("a")
    30  	})
    31  	require.NoError(t, err)
    32  	require.Equal(t, download.URL(), fmt.Sprintf("%s/downloadWithFilename", server.PREFIX))
    33  	require.Equal(t, download.SuggestedFilename(), "file.txt")
    34  	require.Equal(t, download.String(), "file.txt")
    35  	failure, err := download.Failure()
    36  	require.NoError(t, err)
    37  	require.Equal(t, failure, "")
    38  
    39  	file, err := download.Path()
    40  	require.NoError(t, err)
    41  	require.FileExists(t, file)
    42  
    43  	tmpFile := filepath.Join(t.TempDir(), download.SuggestedFilename())
    44  	require.NoFileExists(t, tmpFile)
    45  	require.NoError(t, download.SaveAs(tmpFile))
    46  	require.FileExists(t, tmpFile)
    47  
    48  	require.NoError(t, download.Delete())
    49  	require.NoFileExists(t, file)
    50  }
    51  
    52  func TestDownloadCancel(t *testing.T) {
    53  	BeforeEach(t)
    54  	defer AfterEach(t)
    55  	server.SetRoute("/downloadWithDelay", func(w http.ResponseWriter, r *http.Request) {
    56  		w.Header().Add("Content-Type", "application/octet-stream")
    57  		w.Header().Add("Content-Disposition", "attachment")
    58  		if _, err := w.Write([]byte(strings.Repeat("foobar", 8192))); err != nil {
    59  			log.Printf("could not write: %v", err)
    60  		}
    61  		if h, ok := w.(http.Hijacker); ok {
    62  			if _, _, err := h.Hijack(); err != nil {
    63  				log.Printf("could not hijack connection: %v", err)
    64  			}
    65  		}
    66  	})
    67  	require.NoError(t, page.SetContent(
    68  		fmt.Sprintf(`<a href="%s/downloadWithDelay">download</a>`, server.PREFIX),
    69  	))
    70  	download, err := page.ExpectDownload(func() error {
    71  		return page.Click("a")
    72  	})
    73  	require.NoError(t, err)
    74  	require.NoError(t, download.Cancel())
    75  	failure, err := download.Failure()
    76  	require.NoError(t, err)
    77  	require.Equal(t, "canceled", failure)
    78  }
    79  

View as plain text