...

Source file src/github.com/playwright-community/playwright-go/examples/mobile-and-geolocation/main.go

Documentation: github.com/playwright-community/playwright-go/examples/mobile-and-geolocation

     1  //go:build ignore
     2  // +build ignore
     3  
     4  package main
     5  
     6  import (
     7  	"log"
     8  
     9  	"github.com/playwright-community/playwright-go"
    10  )
    11  
    12  func main() {
    13  	pw, err := playwright.Run()
    14  	if err != nil {
    15  		log.Fatalf("could not start playwright: %v", err)
    16  	}
    17  	browser, err := pw.Chromium.Launch()
    18  	if err != nil {
    19  		log.Fatalf("could not launch browser: %v", err)
    20  	}
    21  	device := pw.Devices["Pixel 5"]
    22  	context, err := browser.NewContext(playwright.BrowserNewContextOptions{
    23  		Geolocation: &playwright.BrowserNewContextOptionsGeolocation{
    24  			Longitude: playwright.Float(12.492507),
    25  			Latitude:  playwright.Float(41.889938),
    26  		},
    27  		Permissions:       []string{"geolocation"},
    28  		Viewport:          device.Viewport,
    29  		UserAgent:         playwright.String(device.UserAgent),
    30  		DeviceScaleFactor: playwright.Float(device.DeviceScaleFactor),
    31  		IsMobile:          playwright.Bool(device.IsMobile),
    32  		HasTouch:          playwright.Bool(device.HasTouch),
    33  	})
    34  	if err != nil {
    35  		log.Fatalf("could not create context: %v", err)
    36  	}
    37  	page, err := context.NewPage()
    38  	if err != nil {
    39  		log.Fatalf("could not create page: %v", err)
    40  	}
    41  	if _, err = page.Goto("https://www.openstreetmap.org"); err != nil {
    42  		log.Fatalf("could not goto: %v", err)
    43  	}
    44  	if err = page.Click("a[data-original-title='Show My Location']"); err != nil {
    45  		log.Fatalf("could not click on location: %v", err)
    46  	}
    47  	if _, err = page.Screenshot(playwright.PageScreenshotOptions{
    48  		Path: playwright.String("colosseum-iphone.png"),
    49  	}); err != nil {
    50  		log.Fatalf("could not make screenshot: %v", err)
    51  	}
    52  	if err = browser.Close(); err != nil {
    53  		log.Fatalf("could not close browser: %v", err)
    54  	}
    55  	if err = pw.Stop(); err != nil {
    56  		log.Fatalf("could not stop Playwright: %v", err)
    57  	}
    58  }
    59  

View as plain text