npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

playwright-geolocation-mock

v1.0.0

Published

A comprehensive geolocation mocking utility for Playwright testing

Downloads

70

Readme

Playwright Geolocation Mock

A comprehensive geolocation mocking utility for Playwright testing. This package allows you to easily mock geolocation data in your Playwright tests, including single locations, routes, and error scenarios.

Features

  • Mock single location with customizable accuracy
  • Simulate movement along routes
  • Mock geolocation errors
  • Control geolocation permissions
  • 35+ predefined locations across continents and landmarks
  • TypeScript support
  • Cross-browser support (Chrome, Firefox, Safari)
  • Mobile browser support

Installation

npm install --save-dev playwright-geolocation-mock

Usage

Basic Location Mocking

import { test } from '@playwright/test';
import { GeoMocker, PREDEFINED_LOCATIONS } from 'playwright-geolocation-mock';

test('mock user location', async ({ context, page }) => {
  const geo = new GeoMocker(context);
  
  // Use predefined locations
  await geo.mockLocation(PREDEFINED_LOCATIONS.LONDON);          // Cities
  await geo.mockLocation(PREDEFINED_LOCATIONS.EIFFEL_TOWER);    // Landmarks
  await geo.mockLocation(PREDEFINED_LOCATIONS.SILICON_VALLEY);  // Tech Hubs
  
  // Or specify custom location
  await geo.mockLocation({
    latitude: 51.5074,
    longitude: -0.1278,
    accuracy: 10 // optional, in meters
  });
});

Route Simulation

test('simulate movement', async ({ context, page }) => {
  const geo = new GeoMocker(context);
  
  // Simulate a European tour
  await geo.mockRoute([
    PREDEFINED_LOCATIONS.LONDON,
    PREDEFINED_LOCATIONS.PARIS,
    PREDEFINED_LOCATIONS.BERLIN,
    PREDEFINED_LOCATIONS.ROME,
    PREDEFINED_LOCATIONS.MADRID
  ], {
    interval: 3000, // Update every 3 seconds
    loop: false     // Stop at the last position
  });

  // Or simulate a world tech hub tour
  await geo.mockRoute([
    PREDEFINED_LOCATIONS.SILICON_VALLEY,
    PREDEFINED_LOCATIONS.BANGALORE,
    PREDEFINED_LOCATIONS.SHENZHEN,
    PREDEFINED_LOCATIONS.TEL_AVIV
  ]);

  // Stop route simulation if needed
  geo.stopRoute();
});

Error Simulation

test('handle location errors', async ({ context, page }) => {
  const geo = new GeoMocker(context);
  
  // Simulate permission denied error
  await geo.mockError(1); // PERMISSION_DENIED = 1
  
  // Error codes:
  // 1 = PERMISSION_DENIED
  // 2 = POSITION_UNAVAILABLE
  // 3 = TIMEOUT
});

Permission Control

test('manage permissions', async ({ context, page }) => {
  const geo = new GeoMocker(context);
  
  // Grant geolocation permission
  await geo.mockPermission('granted');
  
  // Deny permission
  await geo.mockPermission('denied');
  
  // Note: When permission is granted, the last mocked location will be restored
});

High Accuracy Mode

test('use high accuracy mode', async ({ context, page }) => {
  const geo = new GeoMocker(context);
  
  await geo.mockLocation(PREDEFINED_LOCATIONS.LONDON, {
    enableHighAccuracy: true // Reduces accuracy value by half
  });
});

Testing Setup

Test Fixtures

import { test as base } from '@playwright/test';
import { GeoMocker } from 'playwright-geolocation-mock';

type TestFixtures = {
  geoMocker: GeoMocker;
};

const test = base.extend<TestFixtures>({
  geoMocker: async ({ context }, use) => {
    const geoMocker = new GeoMocker(context);
    await use(geoMocker);
    geoMocker.dispose();
  },
});

API Reference

GeoMocker

Constructor

constructor(context: BrowserContext)

Methods

mockLocation(position: GeoPosition, options?: GeoMockOptions): Promise<void>

Mock a single location with optional configuration.

mockRoute(positions: GeoPosition[], options?: RouteOptions): Promise<void>

Simulate movement between multiple locations.

mockError(code: GeoLocationErrorCode): Promise<void>

Simulate a geolocation error.

mockPermission(permission: GeolocationPermission): Promise<void>

Control geolocation permissions.

stopRoute(): void

Stop an ongoing route simulation.

dispose(): void

Clean up resources (automatically stops any active route).

Types

interface GeoPosition {
  latitude: number;
  longitude: number;
  accuracy?: number;
  altitude?: number | null;
  altitudeAccuracy?: number | null;
  heading?: number | null;
  speed?: number | null;
}

interface GeoMockOptions {
  enableHighAccuracy?: boolean;
}

interface RouteOptions {
  interval?: number;  // Default: 1000ms
  loop?: boolean;     // Default: false
}

type GeolocationPermission = 'granted' | 'denied';

type GeoLocationErrorCode = 1 | 2 | 3;  // PERMISSION_DENIED | POSITION_UNAVAILABLE | TIMEOUT

Browser Support

  • Chromium (Desktop & Mobile)
  • Firefox (Desktop)
  • WebKit/Safari (Desktop & Mobile)

Note: Some features like high accuracy mode and error handling may behave differently across browsers.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Releases

Release Title Format: v{major}.{minor}.{patch} Example: v1.0.0

Release Description Template:

## What's Changed
- Brief summary of major changes
- New features added
- Bug fixes
- Breaking changes (if any)

## New Features
- Detailed description of new feature 1
- Detailed description of new feature 2

## Bug Fixes
- Fixed issue with X
- Resolved problem with Y

## Breaking Changes
- List any breaking changes
- Migration steps if needed

## Dependencies
- Updated dependencies
- New dependencies added

## Documentation
- Documentation updates
- New examples added

## Contributors
@username - Feature/Fix description