In HTML5, the new feature geolocation is used to locate user location information.
Because the user location information is sensitive information, you need to be allowed by the user before the program can get the current user information through the API. Each time the Webdriver program is re-allowed to be a new session process, the user's location information has been accessed manually by the browser in a timely manner, but the previous user settings are still not available in the current running environment. The workaround is to let the browser execute the Webdriver test program every time, and the user settings will still be loaded.
In Firefox, for example, on a Mac OS platform, you can open the user Profile Manager with the following command
$/applications/firefox.app/contents/macos/firefox-bin-profilemanager
Other platform Open Way query official developer Documentation:
Https://developer.mozilla.org/en-US/docs/Mozilla/Multiple_Firefox_Profiles
After creating the Geolocation profile successfully, click Start Firefox to start Firefox browser.
Take http://www.weschools.com/html/html5_geolocation.asp as an example. To demonstrate the complete sample code, you also need to create a JSON file containing geolocation information, named Location.json, which reads as follows:
{
"Status": "OK",
"Accuracy": 10.0,
"Location": {"lat": 52.1771129, "LNG": 5.4}
}
Example:
Package COM.LEARNINGSELENIUM.HTML5;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.firefox.FirefoxDriver;
Import Org.openqa.selenium.firefox.FirefoxProfile;
Import Org.openqa.selenium.firefox.internal.Profileslni;
Import org.testng.annotations.*;
public class testhtml5geolocation{
private static Webdriver driver;
@BeforeClass
public void SetUp () throws exception{
Get geolocation profile
Firefoxprofile profile = new Profileslni (). GetProfile ("geolocation");
Configuring geolocation Information
Profile.setpreference ("Geo.wifi.uri", "/selenium 2/mydoc/codes/4/location.json");
Launch Browser via custom profile
Driver = new Firefoxdriver (profile);
Driver.get ("http://www.weschools.com/html/html5_geolocation.asp");
}
@Test
public void Testgetlocation () throws exception{
Driver.findelement (By.cssselector ("P#demo button"). Click ();
}
@AfterClass
public void TearDown () throws exception{
Driver.quit ();
}
}
The geolocation in HTML5