Transcripted Summary

A good use case for mocking a geolocation is when an application shows different features, depending on the location.

In this chapter, we will imitate a geographic location. For example, I live in Dallas, Texas, but can change my location to be anywhere such as India, Singapore, South Africa, Germany, United Kingdom, Israel, even Canada.

In the IDE let's start by writing ChromeDriver driver.

The @BeforeMethod will start our public void setupUp method — WebDriverManager.chromedriver().setup().

At this point we can write driver = new ChromeDriver() and then maximize the window.


  @BeforeMethod
  public void setUp(){
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
  }

For now, we are not going to load the AUT, but we'll load it as the last step.

# Mock Geolocation with executeCDPCommand

Now for the Test method, we start with:


  @Test
  public void mockGeoLocation_executeCDPCommand(){

  }

There are 2 ways to mock a geolocation — we can use the DevTools class or the executeCdpCommand.

I want to show you both ways, starting with driver.executeCdpCommand.



Notice the parameters for executeCdpCommand. They are String_commandName and Map.

We can see the String_commandName by going to the Chrome DevTools Protocol site, search for “geo”, and we see 4 methods.



Our test script will set the geolocation, so look at the second method, setGeolocationOverride. The description shows “Overrides the Geolocation Position”.

This will be the String_commandName.

Click the method and now we see the parameters: latitude, longitude, and accuracy.



These are the coordinates for the map.

Now let's go to DuckDuckGo and get the coordinates. Search for “Dallas Cowboys Stadium” and get the address.

The address is 1 AT&T Way in Arlington, Texas. I'm going to copy it.

Then go to LatLong.net, then paste the value. It says I'm a bot, but I'm not a bot. So, let's try it again.

This time it shows the latitude and longitude.



The latitude is always first and the direction is North or South. Longitude is next and the direction is East or West.

We are going to copy and paste them. But for now, let's see the location that we are going to mock in this AUT.

Where-Am-I.org will be the AUT.



Let's go to our IDE and we're going to write the first parameter, starting with "Emulation.setGeolocationOverride". And we're going to come back and add the coordinates.

The second parameter was a map, so let's create a map by writing Map coordinates = new HashMap(). There it is.

  • Now I'm going to skip a line and within the line, I'm going to put the latitude first by writing put(""). In the parenthesis, I'm going to write “latitude” then a comma. Let's go back to the application for the latitude, copy, then paste.
  • Next is the longitude, so let's write put("") and in the parenthesis, in double quotes we put “longitude” and a comma. Then go and copy the longitude and paste.
  • Next is the accuracy, so let's write put("") and in the parenthesis we're going to write “accuracy” followed by a comma. And the accuracy is 1.

Now we have a map with coordinates.

Let's pass in the coordinates in the executeCdpCommand and let's import Map. Bingo.



Now that we have passed in coordinates, the final step is to load the AUT, application under test.

We're going to write driver.get(""). We go back and get the URL for our AUT and paste.


  @Test
  public void mockGeoLocation_executeCDPCommand(){
    Map coordinates = new HashMap()
    {{
      put("latitude", 32.746940);
      put("longitude", -97.092400);
      put("accuracy", 1);
    }};
    driver.executeCdpCommand(
            "Emulation.setGeolocationOverride", coordinates);
    driver.get("https://where-am-i.org/");
  }

Now let's run.

It looks like my location is the Dallas Cowboy Stadium because we see AT&T for your location. The latitude and longitude, and map match the location.



Yes, so it worked.

# Mock Geolocation with DevTools

Now on the Selenium site, you will see how we can look at a way to use the DevTools class and it's emulate geolocation.



It shows how to perform the same command using devTools.

  • The second line uses getDevTools then assigns it to the DevTools class.
  • Next, a session is created using devTools.createSession().
  • After creating a session, a command is sent to emulate the geolocation by overriding the values with a latitude, longitude, and accuracy.
  • The last step, it's to load the AUT.

I will copy this information starting at DevTools and not copying the last line for driver.quit.

Now go back to the IDE and start our new test with:


  @Test
  public void mockGeoLocation_DevTools(){

  }

So, it does not have the same name as the first test, let's put “mockGeoLocation_DevTools”.

I'm going to also go back up here to our first test. Just in case if you go to GitHub to use this information, I'm going to show you the difference by putting “mockGeoLocation_executeCDPCommand” up here as the test name. That way you'll know which one is for DevTools and which one is for executeCdpCommand.

Now all we do is paste the information.


  @Test
  public void mockGeoLocation_DevTools(){
    DevTools devTools = driver.getDevTools();
    devTools.createSession();
    devTools.send(Emulation.setGeolocationOverride(Optional.of(52.5043),
            Optional.of(13.4501),
            Optional.of(1)));
    driver.get("https://my-location.org/");
  } 

Then import the DevTools class, Emulation, and Optional; and run.

The location shows Germany. We see the latitude and longitude. And it shows North and East.



That's it for mocking a geolocation.

Next, we're going to emulate a network condition.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR