How to get Selenium to respond to Chrome's site init popup? - testing

I am trying out Selenium with ChromeDriver to automate some audio/video tests.
But When I fireup the Chrome Browser with my app it asks me the question http:... wants to use your camera and microphone Allow Deny Options I want to click on Allow and proceed with the scripting on the site. But I cannot proceed without selecting Allow. Unfortunately Chrome pops up this question in a sort of Non-DOM format that I am not able to do a driver.findElement the obvious way and respond with a "click" on the "Allow" option. Has anyone of you encountered this situation and what is the best way to deal with this ?
Cheers !
-- Brian

See this answer (print dialog) or this answer ("Run As..." dialog).
Different dialogs, but the reason (in short, WebDriver can't handle these dialogs) and possible solutions are absolutely the same:
The Robot class, it allows you to "press" programatically anything on the keyboard (or clicking blindly) and therefore getting rid of the dialog by, say, pressing Enter or Esc. However, as told above, any advanced interaction is dependant on OS / language / printer.
// press Escape programatically - the print dialog must have focus, obviously
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
You can, of course, type anything via this class, too.
AutoIt. It's a Windows program useful for handling any system-level automation. Same dependancy as above.
Note that (as far as I know) you can't really check whether the dialog showed up or not, so you won't be able to catch a possible error if it runs on a computer without a camera...

If you're using a ChromeDriver you can get to any 'native' popups using
Alert popup = webDriver.switchTo().alert();
// here you can examine the text within the alert using popup.getText();
popup.accept();

Related

Why doesn't Robot dismiss the Firefox print modal? [Automated testing]

When opening the print page in Firefox a print dialog is triggered. As is mentioned in the answer linked here (How to handle print dialog in Selenium?), Selenium's WebDriver cant handle these sorts of browser (or OS) dialogs.
Im using Selenium with Java to test a particular website. So I included the following code (as was suggested by the answer linked above) which employed the use of Java Robot to get rid of the print dialog.:
// press Escape programatically - the print dialog must have focus, obviously.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
This answer made sense to me because when I manually pressed the escape key, the print dialog was dismissed.
Unfortunately, the code mentioned above did not work for me i.e. the print dialog remained there. As suggested by some other posts, I made sure to wait until the print dialog appeared.
While running the automated test code I'm not entirely sure whether "the print dialog was focused" (as is mentioned in the comment in the code given above). I tried multiple methods to get it "in focus" such as switching to the last web handle, switching to the browser alert etc and all of those threw errors. This further confirmed to me the idea that selenium was unable to handle this dialog. I do suspect that the lack of "focus" on the print dialog is where I'm running into the issue.
I want to be able to dismiss this modal because I open the print page multiple times during my testing run which means that multiple print dialogs open. Once the testing code is completed, the browser quits and only one of these dialogs is closed. Could you please help me figure out what I'm doing wrong?
Im using the following:
Geckodriver version: geckodriver-v0.26.0-macos
Java: 1.8.0_191
MacOS: Version 10.15.5 (19F101)
Firefox version: 78.0.2
Please do let me know what other information I could include to help you answer this. Thanks! I would have commented on the thread I mentioned, but Im new to StackOverflow and you need 50 reputation points to make a comment :(

How to handle window open prompt(native) in webdriver.io

I am using webdriver.io framework. At my application, I have a button that opens a Windows Open prompt (Native), and there I need to choose a folder/file and click Open.
Then returning to my application and continue.
Is there a way to move focus on to this window and control it (Select a path and click Open)?
If not- Is there any other solution someone can offer?
The short answer is probably not, you can't control native dialogs from webdriver.
If it's a file input type for which you want to set a file, you might possibly use webdriver to evaluate a piece of javascript to set the HTMLInputElement.files value of the input element.
This works in modern browsers as mentioned in the mozilla docs

Is the Selenium click() recognized as human or automation?

My scenario which produces the question goes something like below:
I enter a webpage via normal means, next I press on a button, to start a HTML5 application on this webpage, this application is inside an iFrame. On application start I'm being prompted to either turn the sound on or off. At this point there are two possible outcomes:
1. When I answer this prompt manually, new buttons appear in the application window, as expected.
2. When I answer this prompt through automation via Appium, new buttons do not appear.
Now to the question:
To answer the prompt I use the click() method from Selenium. Is it possible that this click() is not considered to be executed by a human and therefore doesn't trigger necessary things? And since I don't have access to the source of the application can I force the Selenium click() to look exactly like a human click?
Here is the code I use to execute the mentioned click:
//Application loading up, hence the sleep
Thread.sleep(5000);
AppiumTestBase.getDriver().switchTo().frame("e_iframe");
Thread.sleep(5000);
WebElement soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
AppiumTestBase.getStandardWaitTime().until(elementToBeClickable(soundOff));
soundOff.click();
The program is able to find and switch in to the iFrame, there are no cross-origin issues either. The AppiumTestBase is just there for initializing the driver, setup capabilities etc. I also tried clicking the element via Actions and JavaScript, but there was no change in behavior.
In C# a workaround I've found to actually take control of the mouse and move it/click with it is to use "Microsoft.VisualStudio.TestTools.UITesting" for the Keyboard/Mouse libraries. From there, you can tell it "Mouse.Click(new Point(X, Y));"and it will move your mouse to that location and click.
Sample Code:
using Microsoft.VisualStudio.TestTools.UITesting;
var soundOff = AppiumTestBase.getDriver().findElement(By.id("soundOff"));
Mouse.Click(new Point(soundOff.Bounds.X, soundOff.Bounds.Y));

Have selenium interact with safari "store password" popup?

I have a selenium test that tests the login of a site through a browser instance available on a selenium-grid. This works fine when using a firefox browser instance but when using safari the test cannot get through the entire test because safari pops up a window with the question "Would you like me to save this password".
It doesn't seem to appear as a window_handle nor as an alert. How can I have selenium dismiss this safari popup and continue its test?
You can't. WebDriver cannot interact with browser and/or OS specific dialogs.
You probably have two choices:
Save/dismiss the password manually the first time you run through the test, then reuse the profile every time.
Press Enter or Esc programatically via your programming language. You didn't specify which one you're using, so here's a Java example:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
It's a hack, but it's reliable and it's what I've been doing to overcome similar issues.
Try this code, it uses JavaScript to change the password to text, and this stops the pop-up:
driver.executeScript("Array.prototype.slice.call(document.getElementsByTagName('input')).forEach(function(e){e.type=e.type=='password'?'text':e.type;});");
Alternatively, you can selectively disable password capture for specific domains. You can also disable password capture altogether by clearing the auto-fill option for username/password.

Automation of DOH Robot tests interrumped by Pop-up message

In order to automate DOH tests during our build process, I use Selenium RC to launch different browsers (IE and Firefox) on a server placed on a different domain than the build machine. Each browser is directed to our runTests.html in order to start DOH.
Sometimes, when a test that uses doh.robot starts, the following message is shown:
"DOH has detected that the current web page is attempting to access DOH, but belongs to a different domain than the one you agreed to let DOH automate. If you did not intend to start a new DOH test by visiting this Web page, press Cancel now and leave the Web page"
but since these tests are unattended it just sits there waiting for someone to click OK, and Selenium times out (in IE 8 it seems like the pop-up disappears automatically but the robot does nothing afterward).
As I said, it doesn't always happen. After you click OK on the Pop-up, the message will stop showing, and the message can go away for several sessions, but then it will show again in which seems to be an arbitrary way.
Does anyone knows a way to prevent this pop-up from showing?
This is probably not the correct way to do it, but in util/doh/robot/DOHRobot.java, you may be able to modify the code to not check that or always simulate pressing "OK". I haven't tried it myself, but I may also need to do that for some of our automated testing.
When the DOH robot is initialized, it first tries to click in the upper left corner of the page you are trying to test. If you obscure this div (you can see it with firebug), then the message will pop up. I think the problem is that your page isn't always loading up quick enough.
It is somewhat of a challenge to fix this. I haven't used DOH in awhile, but I don't think there is any way you can use a setTimeout to fix this. (You can try using setTimeout on the doh.run command, but it might be the case that the DOH robot clicks that div before parsing any doh commands.)
Another thing you might be able to do is add a sort of "wait" command to Selenium, or whatever shell command you are using to fire up the system.