switch between two windows in browser using Selenium java - java

I'm trying to switch between windows and put an accountant to find out if the Selenium found the second window , but does not find it , note that he sees only 01 window , which is the Father window.
how do I find It the second window and switch to It?
I use: selenium-server-standalone-2.48.2 + Eclipse + Java 1.8
I tried this:
//Get all window handles
Set<String> allHandles = driver.getWindowHandles();
//count the handles Here count is=2
System.out.println("Count of windows:"+allHandles.size());
//Get current handle or default handle
String currentWindowHandle = allHandles.iterator().next();
System.out.println("currentWindow Handle"+currentWindowHandle);
//Remove first/default Handle
allHandles.remove(allHandles.iterator().next());
//get the last Window Handle
String lastHandle = allHandles.iterator().next();
System.out.println("last window handle"+lastHandle);
//switch to second/last window, because we know there are only two windows 1-parent window 2-other window(ad window)
driver.switchTo().window(lastHandle);
System.out.println(driver.getTitle());
driver.findElement(By.tagName("body")).click();
message on the console:

I faced similar issue with IE 11. Solved it by checking enable protected mode settings for all zones in IE settings.

Related

How to switch focus between windows using WinAppDriver Java

I am new to windows automation using win app driver.
Our application is developed with chromium browser and we are using win app diver to automate since the main app we are trying to open is windows based.
When I click on Ok button opens another window(Window B). I have 2 windows opened window 1 and window 2. I need to perform actions on both the windows for that I need to shift the focus between two windows. When I use getwindowhandles() method I am getting number of windows opened as 1.
How can I switch between windows using winapp driver.
Appreciate your help.
Thanks
I am using in my code:
this.driver.SwitchTo().Window(this.driver.WindowHandles[0]);
However, I do not expect this to work in your case, as your number of open windows is 1, than means that there is no second window to switch to.
So in your case you can use root session in order to attach to your window:
AppiumOptions rootSessionOptions = new AppiumOptions();
rootSessionOptions.AddAdditionalCapability("app", "Root");
rootSessionOptions.AddAdditionalCapability("deviceName", "WindowsPC");
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), rootSessionOptions);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
var VSWindow = _driver.FindElementByName("Your project name without .csproj - Microsoft Visual Studio");
var VSTopLevelWindowHandle = VSWindow.GetAttribute("NativeWindowHandle");
VSTopLevelWindowHandle = (int.Parse(VSTopLevelWindowHandle)).ToString("x");
AppiumOptions VisualStudioSessionOptions = new AppiumOptions();
VisualStudioSessionOptions.AddAdditionalCapability("appTopLevelWindow", VSTopLevelWindowHandle);
_driver = new WindowsDriver<WindowsElement>(new Uri("http://127.0.0.1:4723"), VisualStudioSessionOptions);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
Reference:
https://github.com/microsoft/WinAppDriver/issues/978
OpenQA.Selenium.WebDriverException: [windowHandle] is not a top level window handle solution
This code works for me (windows automation using win app driver) with C#
//Switch to the next window in desktop application:
IList<string> toWindowHandles = new List<string>(_driver.WindowHandles);
Thread.Sleep(6000);
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
With Java:
Thread.sleep(5000);
//Switch to the next window in desktop application:
Set<String> windowHandles = driver.getWindowHandles();
driver.switchTo().window(windowHandles.iterator().next());

Selenium: How can I navigate back to parent window

I am running below java code for switching windows and getting error message, Kindly suggest something.
Driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
Set<String>set=Driver.getWindowHandles();
Iterator<String> It=set.iterator();
String PId=It.next();
String CId=It.next();
Driver.switchTo().window(CId);
Driver.get("https://www.facebook.com");/* Here again I want to come back to parent window and perform some action */
Please refer to this java documentation link for the list of available switchTo options:
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.TargetLocator.html
In your case, you may need to save the window handle before switching and then use it later to switch back to original window.
String originalWindow = Driver.getWindowHandle();
Driver.switchTo().window(CId);
//Operations on new window here
Driver.switchTo().window(originalWindow);
//Operations on original window here

Switching windows in Firefox 34 hangs

I am using Selenium webdriver 2.44 with firefox 34 on windows 7 machine. I have a script where I hover over an 'open page' icon and click it. The click opens the a new tab manually and in chrome driver 2.15. The scenario opens a new window instead of tab which I handle in firefox-34. below is the code.
public void switchWindow(){
//try {
String winHandleBefore = driver.getWindowHandle();
System.out.println("before "+winHandleBefore);
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
driver.switchTo().window(window);
if (driver.getTitle().contains(winHandleBefore)) {
return;
}
}
The problem I am facing is one or two tests run for more than 10,000 seconds as they look like they hang at switching the windows from parent to child. This is issue is reproducible each and every time.Has anybody seen this issue?. Is there a workaround?.Kindly let me know if additional information needs to be provided.
I have fixed this issue by closing the first window and shifting the focus back on the newly opened window. I didn't expect the fix would be this simple.
The fix works for FF-35 as well.

How to handle overlay/popup window using selenium webdriver

I am trying to automate initial configuration(of my server) through webpage. After hitting my server ip https:/localhost:4443 and entering my credentials ,i get a window to change password(overlay/popup window).
Problem:- if i browse the same ip from another m/c or from another browser , i get a window over window i.e one more window over change password window(Please click the link to see the screenshot).
What i tried is to get the handle of the window but its not working, its providing one handle only.
**Its not frame also.
HTML code -- https://dl.dropboxusercontent.com/u/91420517/Html_Code.JPG
Here's my code
WebDriver driver=new FirefoxDriver();
driver.get("https://localhost:4443/ControlPoint/");
driver.findElement(By.xpath("//*[#id='name']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='pass']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='loginForm123']/div[6]/div[1]/div")).click();
Set<String> winIds = driver.getWindowHandles();
System.out.println("Total Windows --- " + winIds.size()); // its resulting the size as 1 which is not correct.
Iterator<String> it = winIds.iterator();
String mainWin=it.next();
String changeWin=it.next();
String shareWin =it.next();
driver.switchTo().window(shareWin);
String warning = driver.findElement(By.xpath("html/body/div[4234]/div[1]/span")).getText(); // to get the text on 3 window
System.out.println(warning);
How to resolve this issue .Please help. Any other way to click on buttons on window 3.
If the additional window is opened asynchronously, then possibly you are checking for it (with getWindowHandles()) too early, before it has been created - this is a common issue with Selenium tests and asynchronous page updates.
If this is the issue, it can be solved by trying a few times with a wait in between, checking each time whether a new window has appeared.

WebDriver + Internet Explorer, switchTo.window issue

I use Selenium 2 + Java for testing the application on IE 9.
After clicking on the link, the pop-up window is opened. I use switchTo.window method for going to pop-up window. But when I try to go back, my test is delayed on this operation and doesn't go on.
Some code:
link.click(); //Open pop-up window
Object[] windows = driverIE.getWindowHandles().toArray();
driverIE.switchTo().defaultContent();
driverIE.switchTo().window(windows[1].toString()); //Focus on pop-up window
.....
mainWindowHandle = driverIE.getWindowHandles().iterator().next(); //Handle of main window
driverIE.switchTo().window(mainWindowHandle); //Fail!
Please help me to solve the problem.
Windows handles returned by getWindowHandles() are not guaranteed to be in any order. In other words, you cannot depend on windows[1] in your code sample above to contain the window handle of the opened window. Rather, you need code that looks something like the following (NOTE: Completely untested code ahead!):
String mainHandle = driver.getWindowHandle();
// Do whatever you need to do to open a new window,
// and properly wait for the new window to appear...
Set<String> allHandles = driver.getWindowHandles();
for(String currentHandle : allHandles) {
// Note that this is cheating a bit. It will only
// work with a total of two windows. If you have
// more than two windows total, your logic here
// will have to be a little more sophisticated.
if (!currentHandle.equals(mainHandle)) {
driver.switchTo().window(currentHandle);
break;
}
}
// Work with popup window...
// Close the popup window and switch context back
// to the main window.
driver.close();
driver.switchTo().window(mainHandle);
As JimEvans stated, driver.getWindowHandles() sometimes puts windows in incorrect order, thus the for loop does not always work.
Similar to above worked for me (I only have two windows to handle):
String winHandleBefore = driver.getWindowHandle();
driver.findElement(By.cssSelector("a")).click();
Set<String> winHandle = driver.getWindowHandles();
winHandle.remove(winHandleBefore);
String winHandleNew = winHandle.toString();
String winHandleFinal = winHandleNew.replaceAll("\\[", "").replaceAll("\\]","");
driver.switchTo().window(winHandleFinal);

Categories