1

In one of my requirements the current geolocation of the customer will be displayed in a text box by default while loading site. I need to get the geolocation value displayed in the text box, and the actual geolocation value of the user so I can compare them.

Using below code I got the location displayed in the text box

String currentLoc=driver.findElement(By.id("location")).getAttribute("value");
System.out.println("Current location value:"+currentLoc); 

How do I get the actual geolocation value to compare with the value I read from the text box?

2
  • What do you mean by getting it 'through code'?
    – dvniel
    Aug 23, 2018 at 9:59
  • Using below code got the location displayed in the text box String currentLoc=driver.findElement(By.id("location")).getAttribute("value"); System.out.println("Current location value:"+currentLoc); But i need to get the current location of the customer through selenium webdriver code and need to compare the values whether location value what is dispaying in home page and value got from code is same or not
    – QA Testing
    Aug 23, 2018 at 10:15

3 Answers 3

0

You can get the value to compare against several ways, and use it several ways. None of them use Selenium, which is just a library that interacts with web pages.

Your tests will always be run from the same location and you are using IP address geolocation

This is the simplest situation. You do an internet search and find your location, then use that as a comparison constant for your test.

Your tests will always be run from the same location and your application is using something other than IP address

This is a little more complex, because the most common online geolocation sites I found in a quick search use your IP address to get a location. You might have to run a few searches to find the geolocation if your application uses something different - such as address, postal code, address plus postal code, etc. You will still end up with a geolocation you can use as a constant for your tests.

Your tests use a different location each time

This is the complicated scenario. To get the location that's being used, you're going to need to use one of the geolocation APIs to get the actual location. Most of them come with instructions on how to use them, but if you're not familiar with programming you'll find this challenging.

If this is what you need to do, I'd recommend creating a separate class to handle the API interaction so that all your tests need to do is use whatever the location is based on in your app - let's say it's IP address - and call the geolocation class.

1
  • yes my tests uses a different location some times not every time. Thanks for ur guidence i will try geolocation API and will create a separate class to handle
    – QA Testing
    Aug 23, 2018 at 12:49
0

To get the actual geolocation value of the user, you can use the Selenium WebDriver's built-in support for geolocation. Here's how you can do it:

  1. Enable geolocation in your browser:

Before you can get the geolocation, you need to enable it in your browser. The exact steps to do this vary depending on the browser you're using. Here are some instructions for common browsers:

  1. Create a geolocation object:

Once geolocation is enabled in your browser, you can create a geolocation object in your WebDriver instance:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("navigator.geolocation.getCurrentPosition = function(success) { var position = {" +
"\"coords\": {" +
"\"latitude\": \"37.7749\"," +
"\"longitude\": \"-122.4194\"" +
"}" +
"};" +
"success(position); }");

This code creates a geolocation object with a fixed latitude and longitude for testing purposes. In production, you would want to use the actual geolocation values provided by the browser.

  1. Get the geolocation:

Now that you have a geolocation object, you can use it to get the actual latitude and longitude:

JavascriptExecutor js = (JavascriptExecutor) driver;
Object latitude = js.executeScript("return navigator.geolocation.getCurrentPosition().coords.latitude;");
Object longitude = js.executeScript("return navigator.geolocation.getCurrentPosition().coords.longitude;");
System.out.println("Actual latitude: " + latitude);
System.out.println("Actual longitude: " + longitude);

This code uses the JavaScriptExecutor to execute the getCurrentPosition() method of the geolocation object and retrieves the latitude and longitude values from the returned object.

Once you have both the current location value from the text box and the actual geolocation value, you can compare them and perform further actions as needed.

-1

If the geolocation is present in a textbox then locate the text box with the locators and then use getText() method. Then print the value.

WebElement location = driver.findElementByID("Locator value");
System.Out.println(location);
if(location.equals("Actual geolocation"))
{
System.out.println("location matches");
}
else
{
 System.out.println("location is not matched");
}
3
  • i have to validate whether current location is displaying correctly or not
    – QA Testing
    Aug 23, 2018 at 11:31
  • edited the answer. Please check and let me know Aug 23, 2018 at 11:37
  • getAttribute("value") is worked for me to get the location value from homepage
    – QA Testing
    Aug 23, 2018 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.