Selenium Webdriver Handling Popup window - javascript

I am trying to Automate a login scenario of my project.
After Hitting the URL a POPUP Authentication window comes even before the page loads.
If we not pass that window we can't see the home page.
the problem is once the pop-up comes i can't inspect the element using firebug,its not letting me to click anywhere else.
i tries to handle the window but still the control is not going to the username and password text box.
i tried windowhandle,robot class but not working.
Please anybody can help??
here is the piece of code:-
Set<String> handles = driver.getWindowHandles(); // get all window handles
System.out.println(handles.size());
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);
also the robot class:-
Robot rb = new Robot();
//Enter user name by ctrl-v
StringSelection username = new StringSelection("myusername");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
Thread.sleep(2000);
river.switchTo().window(mainHandle);
rb.keyPress(KeyEvent.VK_A);
rb.keyRelease(KeyEvent.VK_A);
rb.keyPress(KeyEvent.VK_M);
rb.keyRelease(KeyEvent.VK_M);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//tab to password entry field
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(2000);
//Enter password by ctrl-v
StringSelection pass = new StringSelection("password");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pass, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//press enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
//wait
Thread.sleep(5000);

You can do one thing. you can try it.
If we press ESC button then application stop processing many times.
after clicking on event use this below code:-
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
above code will press ESC for you. Now you can get the time to find the element.
Hope it will help you :)

Its not possible to automate the browser based authentication using Selenium.
If your authentication is http based then try opening the url using format - http://username:password#example.com/ instead of example.com.
If at all you still want to automate it then use third party plugins like AutoIt to accomplish it which works along with Selenium. AutoIt is used to automate windows based desktop applications including browsers. Here are few links that you can refer to -
Login to chrome using AutoIt
Handling Authentication using AutoIt
Use AutoIt with Selenium
Also you can use plugins for firefox which authenticates the popups automatically, when you open the url. But this is a manual setup and you have to perform authentication once so that the plugin remembers it. Here's one of them.
Hope this helps.

Related

Bringing a browser to focus in parallel execution selenium

Can we make sure one particular browser session to bring to front/focus in parallel execution of selenium?
I have a requirement to do keyboard action by pressing enter key , using robot class to handle google payment option in chrome.
And robot key actions are working only when the browser is in focus/front side.
Tried with ((JavascriptExecutor) driver).executeScript("window.focus();"); but no effect.
Also , tried with sikuli , and having the same issue with sikuli too, as it also needs browser to be in focus.
NOT going for an alternative to use Autoit , since our project needs to work on both Windows and MAC.
Your help is much appreciated.
You can send keys (Alt + Tab)to switch to the current session of browser since it was just the window in focus and then moved to state of being not focused on.
Here is the code sample in Java
Actions action = new Actions(driver);
action.sendKeys(Keys.chord(Keys.ALT, Keys.ENTER)).build().perform();

Handle Cancel button on Safari/Chrome for Open in App Store pop up

Is there a way to handle Cancel button on the Open in App pop up when I redirect from Safari/Chrome to AppStore?
The scenario is something like this: A user receives a message to download the app. It is a browser link. On that browser link check if it is an android or ios? If iOS redirect to App Store or otherwise.
This works seamlessly on android. But iOS on the other hand displays a pop-up to the user, weather to switch and open in another app.
Now my client requires me to handle the Cancel button.
So, could there be a way to handle this via JavaScript?
Code looks something like this:
if($rootScope.$device.Android()) {
window.location = appConfig.PLAYSTORE_LINK;
return;
}
if($rootScope.$device.iOS()) {
window.location = appConfig.APPSTORE_LINK;
return;
}
P.S.: Though, I believe it is an OS level pop-up. It might not be a possibility or a feasible solution to handle this pop-up.
You are correct: this is a user interface element controlled by Apple. You cannot programatically manipulate this alert.

How to handle Geo location popup using Selenium chromedriver?

When ever launching the web-app in chrome browser using selenium webdriver, Geo-location(Geo Location) Pop up is coming on the top of the browser.
Due to which some of my location based Test Cases are getting failed.
How to handle these kind of scenarios using chromedriver?
"When ever launching the web-app in chrome browser using selenium webdriver,
Geo-location(Geo Location) Pop up is coming on the top of the browser."
in this case, u may try something like this:
#BeforeMethod
public void clearPopup() {
driver.navigate().refresh();
Thread.sleep(2000);//wait until the page is loaded.
//try to use explicit wait
try {
Alert alert = driver.switchTo().alert();
alert.dismiss();
} catch (NoAlertPresentException ex) {
//System.out.println("No alert for this test case.");
}
}
this method will run before all ur testcase and if any popup shows it will dismiss the alert.
I also have go through ur url. when i click on call geolocation api, a pop is shown. U can stop this kind of pop up by doing the followings:
-go to settings of chrome and click on advanced settings.
-than click on content settings under privacy.
-than u will see pop ups and location options.
form there choose what option u want. Choose
"Allow all sites to track ur physical location"
if u don't want to see the popup.

Never Show Window On Page Launch

I am using C# and asp.net to launch a webpage that I am passing parameters to. That works well! I come from a Windows.Forms background so please forgive me if I am trying to achieve the impossible. What I would like is set the Visibility property of the program (either IE or chrome) to false so the user never sees that a webpage is being launched. I have been using this JS function to close the page, but it seems that the page must completely load before closing which sometimes can take a few seconds.
Does asp.net have the capability to achieve such? And this is my JS code I have been using
string close = #"<script type = 'text/javascript'>
window.returnValue = true;
window.close();
</script>";
base.Response.Write(close);
If you don't want the User to see the page, I assume you just want to post some information to the page. In that case, make an HTTP request via c# code, instead of opening the webpage up in a browser.
On the Project Properties page, Web tab, Start Action section, click the radio button for "Don't open a page. Wait for a request from an external application".

How to open a new window or tab via javascript in windows phone 7 browser

Is it possible to open a new window or tab via javascript in Windows Phone 7 browser?
window.open does not appear to be supported nor does target='_blank'.
Am I missing something here? This basic feature works just fine on iphone and android.
Any ideas on how I can get this working in Windows Phone 7?
On Windows Phone 7 this is not possible programmatically. It's all in the users hand.
To cite a Microsoft employee:
"A user can open a link in a new Tab by tapping and holding the link to
get the context menu but an anchor or scripts request to target a new
window is ignored.
There are several reasons for this:
Cross-window communications are not supported.
Windows Phone only has one instance of the
browser so new "windows" have to be opened as Tab's.
The browser experience is full screen so the user has no good visual cue that they
have moved to a new Tab unless they explicity request it.
Navigating "back" in a new Tab exits the browser which would be
confusing to the user if they did not know a new Tab was created."
If you are trying to add this feature for in-ap browser control, I can suggest you one way.
You have to inject a java-script on every webpage the browser control is able to load the page successfully. In the java-script use window.extern.notify to invoke the ScriptNotify function in your code behind. On the detection of the appropriate message create a new instance of browser control and add it to an array or list. Thereby you can emulate the new tab feature for in-app browser control.
the JS code to be injected may be like this String NEW_TAB_FUNCTION = "window.open = function(__msg){window.external.notify('addnewtab');};";
Which can be injected using browser.InvokeScript("eval", NEW_TAB_FUNCTION);
In ScriptNotify check for addnewtab (keep IsScriptEnabled = True)
void WebBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == "addnewtab")
{
//do work here
}
}
Note that I have overridden the window.open function in the JS with a function which will be injected every time on a new webpage in order to get notified of user input.
Also note this works only for WebBrowser Control and not external browser.
My workaround this issue is simple:
window.open
returns null if failed, so in that case I open it in the same window:
var win = window.open(href, '_blank');
if(!win || win==null){
window.location.href = href;
}else{
win.focus();
}
which is a good practice to have a fallback anyway...

Categories