How to disable firefox shared location alert in selenium webdirver
I am unable to dismiss the Firefox location warning. When I click on the storage locator, Firefox will open a location warning. I want to click "Don't Allow". I know how to write a way to handle alert boxes in the selenium web driver. But here I cannot click on the alerts.
package toysrus;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class Toysrus {
public WebDriver driver;
@Test(priority=1)
public void firefox() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:/Users/naveenkumar.d/Downloads/geckodriver-v0.17.0-win64/geckodriver.exe");
driver=new FirefoxDriver();
}
@Test(priority=2)
public void urlaccess()
{
String URL="http://m.toysrus.com/?TRU=1";
driver.get(URL);
}
@Test(priority=3)
public void menucontainner()
{
driver.findElement(By.id("sk_menu_icon_container")).click();
}
@Test(priority=3)
public void storeloocator() throws InterruptedException
{
driver.findElement(By.name("storelocator")).click();
Thread.sleep(5000);
Alert alert = driver.switchTo().alert();
alert.dismiss();
}
source to share
Co-hosting permission is not a warning (at least not for webdriver). This is an opportunity. Therefore, the best way is to disable this feature. Refer to my answer to this geolocation post . Basically you need to set DesiredCapabilities for your driver:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("firefox");
caps.setCapability("locationContextEnabled", false);
driver = new RemoteWebDriver(new URL(URL),caps);
//continue the rest of your test and remove the code on Alert
This will prevent the message from showing even.
Side note: I would recommend putting this code in driver initialization in a method @Before
instead of@Test.
source to share