Geolocation Testing With Selenium Using Examples

Image for post
Image for post

Note: the code samples may be displayed improperly because of markdown. I recommend continuing to read the original article on our blog to make sure all the examples are displayed properly.

Image for post
Image for post

Importance Of Geolocation Browser Testing

A lot of time, depending on the location, organization serves different content on the website. The websites being served in different countries can be in different languages or even the content or product showcased on the website might be different. To make sure that your users are being served the right content it is important to perform Gelocation testing.

Image for post
Image for post
Image for post
Image for post
  • Localization for adapting to a particular locale
  • Testing websites across different geographies with varying internet speeds
  • Geo-blocking content for adhering to the norms of the target country
  • Geotargeting for evaluating SERP (Search Engine Result Pages)

E-commerce Use Case Demonstrating Geolocation Browser Testing

To further explain the importance of Geolocation Testing with selenium. I’ll take the case of a popular e-commerce store and show you how it serves different content based on the location.

Image for post
Image for post
Amazon France HomePage
Image for post
Image for post
Amazon Singapore Homepage

Geolocation Testing With Selenium 4

Selenium is a popular test automation framework that provides a number of automated browser testing capabilities. The next major public release will be Selenium 4 but there is no official announcement on the official release of the same. If you are of an inquisitive nature (like me) and enjoy experimenting with the latest features of Selenium, you should download the Selenium 4 (Alpha) version. At the time of writing this blog, the latest Selenium 4 Alpha version was Selenium 4.0.0-alpha-6.

Environment Setup For Selenium 4

For installing Selenium 4.0.0-alpha-6 in your system, execute the following command on the terminal:

pip install selenium==4.0.0a6
$ python -c “import selenium; print(selenium.__version__)”
4.0.0a5

Geolocation Testing With Selenium 4 Using Pytest

For mocking (or faking) the geolocation, you first need to supply the latitude and longitude of the mock location. The same can be obtained using Google Maps where you have to search for the desired location.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
driver = webdriver.Chrome()
driver.get(“https://locations.dennys.com/search.html/")
location_icon = driver.find_element_by_css_selector(“.icon-geolocate”)
time.sleep(10)
location_icon.click()
pytest -v Mocking_geolocation_Selenium_4.py
Image for post
Image for post
Image for post
Image for post

Geolocation Testing With Selenium 4 Using TestNG

Using Maven, I’ll update the pom.xml for adding the following essential dependencies:

Image for post
Image for post
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GeoLocationTest {
protected static ChromeDriver driver;
String URL = “https://locations.dennys.com/search.html/";

@BeforeClass
public void testSetUp() {
/* System.setProperty(“webdriver.chrome.driver”, “C:\\EdgeDriver\\chromedriver.exe”); */
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
/* Command to emulate Geolocation */
String URL = “https://locations.dennys.com/search.html/";

driver.executeCdpCommand(“Emulation.setGeolocationOverride”, coordinates);
driver.navigate().to(URL);
driver.manage().window().maximize();
/* driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); */
/* Not a good programming practice, added for demonstration */
Thread.sleep(5000);

WebElement location_icon = driver.findElement(By.cssSelector(“.icon-geolocate”));
Thread.sleep(2000);
location_icon.click();
@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Geolocation Testing With Selenium 4 On Cloud-based Selenium Grid

LambdaTest provides cross browser testing on the Selenium Grid Using which you can perform automated browser testing across 2000+ combinations of real browsers and operating systems online.

Image for post
Image for post
capabilities = {
“user” : “user-name”,
“accessKey” : “access-key”,
“build” : “your build name”,
“name” : “your test name”,
“platformName” : “Windows 10”,
“browserName” : “Chrome”,
“browserVersion” : “83.0”,
“geoLocation” : “US”
}
Image for post
Image for post
Image for post
Image for post

It’s a Wrap!

Geolocation browser testing is important for testing product features that depend on the location from where the website (or web app) is accessed. Thorough geolocation browser testing will also help in improving your website’s ranking on popular search engines.

Written by

Product Growth at @lambdatesting (www.lambdatest.com)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store