0

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?

  • What do you mean by getting it 'through code'? – trashpanda Aug 23 '18 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 '18 at 10:15
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.

  • 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 '18 at 12:49
0

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");
}
  • i have to validate whether current location is displaying correctly or not – QA Testing Aug 23 '18 at 11:31
  • edited the answer. Please check and let me know – Prasanna venkatesh Aug 23 '18 at 11:37
  • getAttribute("value") is worked for me to get the location value from homepage – QA Testing Aug 23 '18 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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