Block browser device location requests

Technology specific object identification, supported applications, web technologies, and 3rd party controls.
User avatar
tiffin.filion
Posts: 46
Joined: Thu Oct 29, 2020 12:47 am
Contact:

Block browser device location requests

Post by tiffin.filion » Thu Aug 19, 2021 4:48 am

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.
-- Tiffin --
Senior QA Analyst

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7174
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Block browser device location requests

Post by odklizec » Thu Aug 19, 2021 9:19 am

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.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
tiffin.filion
Posts: 46
Joined: Thu Oct 29, 2020 12:47 am
Contact:

Re: Block browser device location requests

Post by tiffin.filion » Fri Aug 20, 2021 6:25 am

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!
-- Tiffin --
Senior QA Analyst

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7174
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Block browser device location requests

Post by odklizec » 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.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
tiffin.filion
Posts: 46
Joined: Thu Oct 29, 2020 12:47 am
Contact:

Re: Block browser device location requests

Post by tiffin.filion » Mon Aug 23, 2021 5:50 pm

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.
-- Tiffin --
Senior QA Analyst

User avatar
tiffin.filion
Posts: 46
Joined: Thu Oct 29, 2020 12:47 am
Contact:

Re: Block browser device location requests

Post by tiffin.filion » Fri Sep 10, 2021 5:54 pm

:!: An update to this, this code ONLY works for iOS. I haven't found a solution for Android. :!:
-- Tiffin --
Senior QA Analyst

User avatar
tiffin.filion
Posts: 46
Joined: Thu Oct 29, 2020 12:47 am
Contact:

Re: Block browser device location requests

Post by tiffin.filion » Tue Dec 21, 2021 2:01 am

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();
-- Tiffin --
Senior QA Analyst

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7174
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Block browser device location requests

Post by odklizec » Tue Dec 21, 2021 9:11 am

Hi Tiffin,

Thanks for sharing your valuable solutions!
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration