I am trying to write automated tests using C# Selenium to cover a portion of an application. The scenario is that I reach a page (on the mobile web version of a site, using a safari browser on a real device via Browserstack) and there is a button on the page that will take me to another tab/ external site that I would like to go to. When I click on the button on the page, a safari alert popup shows that I have to Deny or Allow. In this case I want to allow it and move on to the next tab.
I have allowed safari popups for my WebDriver like this:
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("safariAllowPopups", "true");
The problem, is that when I click on the button, the alert popup shows which is expected, but then I get an error:
OpenQA.Selenium.UnhandledAlertException : A modal dialog was open, blocking this operation
The error is happening on the element I am trying to click, which doesn't make much sense. Because I just clicked that button in order to get the alert popup to show. Since it errors here, I can't select allow, or let the WebDriver handle it to allow the popup. It just fails here. This is the code I am trying to execute:
private readonly exampleButton = By.XPath("//div[@id='buttonContainer']//img[@id='buttonImage']");
//I was told by a browserstack support that I need to switch the WebDriver context to Native App in order to interact with the alert popup
driver.SwitchTo().WithContext("NATIVE_APP");
driver.FindElement(exampleButton).Click(); //The test fails here with the exception noted above
//I have tried this to allow the popup, but of course the code doesn't reach here
IAlert popup = driver.SwitchTo().Alert();
popup.Accept();
//I have also tried this to allow the popup, but of course the code doesn't reach here
IWebElement ll = driver.FindElement(By.Id("Allow"));
ll.Click();
//Switch back to normal context
driver.SwitchTo().WithContext("WEB_VIEW");
Image of popup I modal that is blocking button operation
So why in the world is the button that was clicked in order to show the alert popup, being blocked from operating by the very modal it opened?!