Page 1 of 1

Block browser device location requests

Posted: Thu Aug 19, 2021 4:48 am
by tiffin.filion
The site I am creating tests for, triggers a browser request asking for the location from the mobile device.

If I shrink desktop chrome, I'm able to find the pop-up as part of chrome, but when I run in BrowserStack on an actual device, it doesn't find it.

I found a BrowserStack article specifically for this case, but I have idea how to translate into ranorex. https://www.browserstack.com/docs/autom ... on-pop-ups

Our site sets a cookie when the pop-up has been opened (whether the user allows or blocks), so that could be an option. But no idea how to do that for chrome and safari.

Any suggestions would be greatly appreciated.

Re: Block browser device location requests

Posted: Thu Aug 19, 2021 9:19 am
by odklizec
Hi,

The problem is, that the Selenium WD cannot (by default) handle native browser dialogs. Therefore you have to implement your own solution, using the code suggested in the link you posted. Unfortunately, I'm not using Ranorex WD integration, so I can't help you much with this. But if you search this forum, you will find a lot of WD-related examples, which may help you to understand how to work with Ranorex endpoints via code.

This post may help with obtaining actually used endpoint:
viewtopic.php?f=20&t=14194&p=55275&hili ... int#p55275
And then you need to use this information together with the sample code from your link.

Re: Block browser device location requests

Posted: Fri Aug 20, 2021 6:25 am
by tiffin.filion
So that post helped me a bit, and we found a different way to handle the popups that works much better.
It works on both Android and iOS and is only a single line of code. :)

https://www.browserstack.com/guide/aler ... n-selenium

I'm going to utilize the same thing to be able to log in to our dev environments on mobile devices since it's the exact same type of notification.

Thanks for getting me started in the right direction odklizec!

Re: Block browser device location requests

Posted: Mon Aug 23, 2021 7:37 am
by odklizec
Hi Tiffin,

Could you please share your solution? I mean not just the link, but how exactly you implemented it? I'm sure it would help someone in future. Thanks.

Re: Block browser device location requests

Posted: Mon Aug 23, 2021 5:50 pm
by tiffin.filion
odklizec wrote:
Mon Aug 23, 2021 7:37 am
Hi Tiffin,

Could you please share your solution? I mean not just the link, but how exactly you implemented it? I'm sure it would help someone in future. Thanks.
Yes! I totally meant to and then forgot to actually do it. :D

So, need to add the following at the top of the code module:

Code: Select all

using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.Interactions;
The in the Run() module, it's as simple as:

Code: Select all

var webDriverEndpoint = Host.Current.TryGetAsWebDriverEndpoint();
OpenQA.Selenium.IWebDriver driver = webDriverEndpoint.WebDrivers.FirstOrDefault();

driver.SwitchTo().Alert().Dismiss();

driver.Quit(); 
The first part finds the webdriver (from your endpoint).
Then it switches to the OS alert, either the location request or a login popup, and then cancels or blocks it. If you want it to allow, change Dismiss to Accept. Then it closes access to the driver so it's not constantly running.

Re: Block browser device location requests

Posted: Fri Sep 10, 2021 5:54 pm
by tiffin.filion
:!: An update to this, this code ONLY works for iOS. I haven't found a solution for Android. :!:

Re: Block browser device location requests

Posted: Tue Dec 21, 2021 2:01 am
by tiffin.filion
Final Solutions:

Android:
Create a usercode action called SetCookies (or whatever you'd like to call it). Replace Repository Name and Application Under Test with the names from your solution:

Code: Select all

public void SetCookies()
        {
           // set cookies to avoid Location Request popup on mobile
            var repo = (RepositoryName).Instance;
            
            WebDocument webDocument = repo.(ApplicationUnderTest).Self;
            webDocument.ExecuteScript("document.cookie = 'geocalled=true';");
            webDocument.ExecuteScript("document.cookie = 'mobileGeoCalled=true';");
        }
iOS:
Setting cookies doesn't work for iOS, even after allowing cookies (it refuses to sendKeys). So the alert dismiss solution is what will work here.

Code: Select all

var webDriverEndpoint = Host.Current.TryGetAsWebDriverEndpoint();
OpenQA.Selenium.IWebDriver driver = webDriverEndpoint.WebDrivers.FirstOrDefault();

driver.SwitchTo().Alert().Dismiss();

Re: Block browser device location requests

Posted: Tue Dec 21, 2021 9:11 am
by odklizec
Hi Tiffin,

Thanks for sharing your valuable solutions!