
Geolocation testing ensures that web applications function as intended for users in diverse locations. Many application features rely on user location, such as when ordering food or booking flights.
In these scenarios, geolocation testing is an integral component of Functional Testing to guarantee that the application performs seamlessly across various locations.
Let’s examine the details of performing geolocation testing using Cypress with a demonstration for better understanding.
What is geolocation testing?
Geolocation is the geographic location identification of a user/GPS-enabled device.
If we are searching for a restaurant in a nearby or specific location, geolocation comes into the picture to identify where we live and which is the nearest restaurant. Geolocation identification typically uses a network routing address or the internal GPS.
Geolocation is device-specific, which means the device should support the geolocation, and the browser should have sufficient permission to access the location services.
In geolocation testing, we set various latitudes and longitudes in the browser and check if our application gives results based on the location(using latitude and longitude).
Industries where geolocation testing can be useful
Geolocation testing proves beneficial across sectors such as e-commerce, where location-based services impact user experiences in areas like online shopping and delivery tracking. Additionally, in the travel industry, testing geolocation ensures accurate functionality for services like hotel bookings and navigation assistance.
- Maps and Navigation Systems: Geolocation is used in Map applications and Navigation systems where the precise location of a user or device is required. Applications like Google Maps and GPS devices rely on geolocation to provide real-time location-based services.
- Location-Based Services: Geolocation is a critical component of location-based services, which provide customized information and features based on the user’s location. Examples include finding nearby restaurants, hotels, ATMs, gas stations, or weather forecasts specific to your location.
- Social Media and Networking: Many social media platforms utilize geolocation to enhance user experiences. It enables users to share their location, tag posts or photos with a specific place, and find friends or events nearby. Geolocation can also target advertisements based on the user’s location.
- E-commerce and Local Search: Geolocation plays a significant role in e-commerce and local search applications. It enables users to find products or services in their area, search for nearby stores, and receive personalized recommendations based on location.
- Emergency Services: Geolocation is crucial to locating and assisting distressed individuals. When someone makes an emergency call from their mobile device, their geolocation information is often transmitted to emergency response centers, helping them dispatch assistance more effectively.
- Asset Tracking and Fleet Management: Geolocation is employed in asset tracking systems to monitor the location and movement of vehicles, shipments, or valuable assets. It allows businesses to optimize logistics, improve fleet management, and track the status of their assets in real-time.
- Geotagging and Geofencing: Geolocation is used in geotagging, which involves adding location metadata to various types of content like photos, videos, or social media posts. On the other hand, geofencing uses geolocation to define virtual boundaries or zones and trigger specific actions or notifications when a device enters or exits those areas.
- Weather Forecasting: Geolocation data gathers real-time weather conditions from various locations. This information helps meteorologists analyze weather patterns, predict storms, and issue accurate forecasts for specific regions.
Performing geolocation testing manually
Manual geolocation testing is a process that involves carefully examining the precision of location-based features in a web application. Testers manually simulate different geographical scenarios to ensure the application operates appropriately and delivers precise results, such as location-specific services or content. It’s all about meticulously verifying and validating the accuracy of these features through hands-on testing.
For instance, If we consider the food delivery application, we need to test if the application shows restaurants based on the location and the nearest one. Should we travel to various locations physically to test this? The answer is No.
Making use of the Geolocation API, this API enables users to share their location with web applications voluntarily. Users are prompted to grant permission before providing location information to uphold privacy. It allows the browser to retrieve the user’s location(if permission is granted) and send the location information to the web application running inside the browser.
Below are the interfaces that Geolocation API provides.
- Geolocation: The main class of this API, which contains methods to retrieve the user’s current position, coordinates also watches for any change in the location coordinates
- GeolocationPosition: This interface returns the GeolocationCoordinate object and timestamp
- GeolocationCoordinates: This interface provides the user’s Position, and using this interface, we can retrieve the user’s latitude, longitude, altitude, accuracy, altitude accuracy, and speed
- GeolocationPositionError: This interface returns an error code and message if the call made to Geolocation is unsuccessful
- geolocation: The read-only property returns the Geolocation object.
Geolocation testing using Cypress
In Cypress, we can perform Geolocation testing using the following two approaches.
- Stubbing
- Network Interception
Stubbing is useful when the web application uses Geolocation API functions to determine the user’s location coordinates.
Network interception applies when a web application uses an API call to the server, which returns the user’s location coordinates.
Let’s go through each of the approaches in detail.
Using Stubbing
Cypress leverages Stubbing as a powerful technique within browser automation testing. In the context of a web application utilizing Geolocation API, Cypress allows replacing real Geolocation API calls with predefined behavior using Stubs. It enables testers to control and manipulate the geolocation data, setting specific latitude and longitude values within the browser for comprehensive automation testing scenarios.
Cypress uses the external library sinon.js for stubbing. This library provides the stubbing feature and other useful features, making testing these features during development much easier and quicker; below is an example showing how to use the stub in Cypress.
cy.stub(navigator.geolocation, “getCurrentPosition”).callsArgWith(0, { coords: { latitude, longitude }, }); |
Using stub whenever the application calls the getCurrentPosition() function, we can return the latitude and longitude of the geographic location we want to test.
To understand the working stub using Cypress, let’s take an example. Before writing the code, we must identify the coordinates (latitude and longitude) of the location we want to test.
Test Scenario
The provided Cypress test scenario is to test the functionality when performing geolocation testing in California.
1. Set desired latitude (36.7783) and longitude (-119.417931) for California.
2. Use cy.visit()to load the HTML page at “cypress/e2e/geo-location/index.html”. 3. Employ cy.stub() within onBeforeLoad to simulate navigator.geolocation.getCurrentPosition with predefined California coordinates. 4. Simulate a button click with cy.contains(“button”, “Get Location Details”).click(). 5. Verify the presence of a message containing the stubbed latitude and longitude for California. |
Below is the code implementation for the above test scenario.
it(“Geolocation testing california”, () => { const latitude = 36.7783; const longitude = -119.417931; cy.visit(“cypress/e2e/geo-location/index.html”, { onBeforeLoad({ navigator }) { cy.stub(navigator.geolocation, “getCurrentPosition”).callsArgWith(0, { coords: { latitude, longitude }, }); }, }); cy.contains(“button”, “Get Location Details”).click(); cy. contains( `Your current location is (Latitude: ${latitude}, Longitude: ${longitude})` ); }); |
Result: Running the above tests will be completed successfully.
Coordinating precise and varied geolocation tests in a local setting can pose challenges because of the limited range of real-world locations, however, by utilizing cloud-based testing platforms like LambdaTest.
LambdaTest is an AI-powered test orchestration and execution platform that lets you run manual and automated tests at scale with over 3000+ real devices, browsers, and OS combinations. This platform allows you to access various real devices located in different places, guaranteeing thorough coverage and precise outcomes.
Using Network Interception
A Web application uses API calls to create or retrieve data and show it in the browser. Network interception is a mechanism where we can listen to the API Calls that our web application triggers and modify its request and response data based on our requirements.
Network Interception can be used when we want to
- Wait for a particular request to be sent
- Wait for a specific request response
- Manipulate request and response data
Let’s consider LambdaTest Playground for the use case where the GeoLocation is set based on the API response in the website.
Unlike the other websites, LambdaTest Playground does not use the geolocation API to show the geolocation details. Now, let’s enable developer tools and navigate to the Network Tab. Post this, and we will refresh the page.
As we can see, the API https://geolocation-db.com/json/ is called, and the response the API is returning is shown on the website.
With Geolocation API and Cypress, we can effortlessly test various Geolocation-specific testing of our website without connecting to a VPN or physically moving to a specific location. It enables us to develop and test the website effectively, which provides data based on location
Disclaimer
The information contained in South Florida Reporter is for general information purposes only.
The South Florida Reporter assumes no responsibility for errors or omissions in the contents of the Service.
In no event shall the South Florida Reporter be liable for any special, direct, indirect, consequential, or incidental damages or any damages whatsoever, whether in an action of contract, negligence or other tort, arising out of or in connection with the use of the Service or the contents of the Service. The Company reserves the right to make additions, deletions, or modifications to the contents of the Service at any time without prior notice.
The Company does not warrant that the Service is free of viruses or other harmful components