I have a PWA (which means essentially a webpage + serviceworker) that asks for the user's camera and location via getUserMedia
and getCurrentPosition
calls. These create an OS level dialog box with options to Allow
| Deny
. It appears that Selenium driver cannot manipulate these dialogs. Is there any way to get past the dialog's using Selenium's RemoteWebDriver? The objective is to run the webpage on mobile devices using a service like BrowserStack.
-
Is your app a native or web app?– João FariasCommented Sep 7, 2019 at 5:22
-
Its a web app, a webpage with a serviceworker basically– patrickCommented Sep 8, 2019 at 1:11
Add a comment
|
1 Answer
The trick is to flip the driver into NATIVE_APP
mode (this seemed a bit dubious since I have a normal webpage, but it worked)
Here's a test console app for dot net framework 4.
On some phones 'Allow' might be 'ALLOW' and the XPath
matching is case sensitive
Note: if you are using BrowserStack and run out of minutes you have to choose the 'Automate Mobile' plan, their pricing page is highly confusing
static void Main(string[] args)
{
DesiredCapabilities capability = new DesiredCapabilities();
capability.SetCapability("device", "Samsung Galaxy S9");
capability.SetCapability("real_mobile", "true");
capability.SetCapability("browserstack.user", username);
capability.SetCapability("browserstack.key", password);
capability.SetCapability("autoAcceptAlerts", "true");
capability.SetCapability("autoGrantPermissions", "true");
AndroidDriver<IWebElement> driver =
new AndroidDriver<IWebElement>(
new Uri("http://hub-cloud.browserstack.com/wd/hub"), capability);
driver.Navigate().GoToUrl("https://the-internet.herokuapp.com/download");
driver.Context = ("NATIVE_APP"); // this does the trick <<<<
IWebElement ll = driver.FindElement(By.XPath("//*[text()='some-file.txt']"));
ll.Click();
System.Threading.Thread.Sleep(2000);
// To accept/block the popup,
//you need to switch the context to “NATIVE_APP“
//and click on the Allow/Block button.
//driver.Context = ("NATIVE_APP");
ll = driver.FindElement(By.XPath(".//android.widget.Button[@text='Continue']"));
ll.Click();
System.Threading.Thread.Sleep(2000);
ll = driver.FindElement(By.XPath(".//android.widget.Button[@text='ALLOW']"));
ll.Click();
System.Threading.Thread.Sleep(2000);
driver.Quit();
}