Handeling popup window in selenium c# - selenium-webdriver

My program includes many windows.
Whenever I want to change the first window (I named "bigwindow") and the new second window
I have been using the following code in order to switch betwing the windows:
Ireadonlycollection <string> openwindows1=driver.windowhandels;
Foreach (string current in openwindows1)
{
If (current!= bigwindow)
{
Driver.switchto().window(current):
Break
}
}
I trying to using this code when a popup window was opened from the second window and it is not working..
The focus is still on the second window and not on the third..
Please help me

Related

Switch to other site from main application

In my current application, manually when i click on a Button say 'Buy' button it takes me to a different site within the same browser (in another tab).
Usually I can switch to the Tab using
driver.switchTo().defaultContent();
But while am doing it in automation the second site is opening in a different browser. How can i handle this. I want this to open within a the same browser like how its happening when i do it manually. Please help me out. Thanks in advance.
You don't define the programming language, but in Java it is as follows:
// Store the current window handle
String winHandleBefore = driver.getWindowHandle();
// Perform the click operation that opens new window
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Perform the actions on new window
// Close the new window, if that window no more required
driver.close();
// Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
// Continue with original browser (first window)
Source: How to switch to the new browser window, which opens after click on the button?

Opening of new tab when user click on a button provided on popup using selenium webdriver

There is a question regarding Selenium WebDriver given is a scenario
I login to the application
Click on some link
It opens a new popup which has iframe
There is a preview button on this popup on which if I click manually it opens a new tab in the previous parent window but if I click on this button using selenium webdriver it opens new window
I want to open new tab using selenium webdriver by clicking on this preview button any solution?
These are steps you should take then.
Log into application
Get current window handles
Find a list of window handles EXCEPT currentWindow handles
switchTo() newly opened window handle, switchTo() iframe
Click the button inside an iframe
Get the window handles again
switchTo() newly opened window
perform some action, close all window except parent.
A sample can look like the following. Note: C# code
public void PopupWindowHandle()
{
//logged into application
//get current window handle
string prentWindowHandle = Driver.CurrentWindowHandle;
//click should generate a new tab
Driver.FindElement(By.Id("id")).Click();
//get window handle counts. In your case should be 2
List<string> windowHandles = Driver.WindowHandles.ToList();
//switchTo newly opened window handle
foreach (string handle in windowHandles)
{
if (handle != prentWindowHandle)
{
Driver.SwitchTo().Window(handle);
//switch focus into iframe
Driver.SwitchTo().Frame(Driver.FindElement(By.CssSelector("Iframe Selector")));
//click should generate another window
Driver.FindElement(By.Id("id")).Click();
//should be 2 in total
List<string> newHandles = Driver.WindowHandles.ToList();
foreach (string newHandle in newHandles)
{
if (newHandle != handle)
{
Driver.SwitchTo().Window(newHandle);
//do some work
Driver.Close();
Driver.SwitchTo().Window(handle);
break;
}
}
Driver.Close();
Driver.SwitchTo().Window(prentWindowHandle);
break;
}
}
}
I guess you are not using chrome.:)
Usually, webdriver forces a browser to open a new window instead of a tab, it's easier to dealing with through switch window. Like IE and Firefox, but chrome will open a new tab instead of a new window and it still supports switch window. This how I found, try it out.

Popup windows that automatically close in Selenium Webdriver

My Selenium Webdriver test is something like:
Click button -> this opens popup window
Iterate through all window handles and switch to popup window
Fill out form on popup window and submit.
At this point, the popup window might automatically close if the input was valid or it will stay open if there is an error on the form.
My question is, if the window automatically closes and that was the window handle I had focus on, what happens exactly? Is there a way for me to detect that the window has closed?
Thanks
If the focused window closed, then the Web Driver instance had no focus on any window and it throw an exception if you are trying to find any element. It throws NoSuchElementException when you trying to find an element. You can check the window presence by counting the number of windows.
The windows handles keeps the record of pop of window even if it closes automatically. So you have to switch to the parent window. And then use the print statement for Windows.title to check which window is opened.

Detect if any dialog is opened

I have created single instance application. It accepts command line arguments and process them. If application is already running and some dialog ( open file or message box ) is opened. Now if i try to pass command line argument i need to check if dialog is shown or not. So I added this code.
if (!Application.Current.MainWindow.IsActive)
{
Application.Current.MainWindow.Activate();
}
if (Keyboard.FocusedElement != null)
{
// If focused element is not null it means no other dialog is shown.
// safe to go.
}
Ideal was like , if focused element is not null then it means focus is inside window and no other dialog is shown.
In normal scenarios when window is not minimized this code works fine. but if window is minimized then condition fails as keyboard focus is not in window.
Do u find any solution which will be generic ? I can achieve this by adding flags before each dialog box. but I have more than 10 dialog boxes. In future i may add more dialog boxes.
Thanks
Very old question but I've recently faced a similar issue.
Here is how I solved it:
public static bool IsAnyModalOpened()
{
return Application.Current.Windows.OfType<Window>().Any(IsModal);
}
public static bool IsModal(this Window window)
{
var fieldInfo = typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic);
return fieldInfo != null && (bool)fieldInfo.GetValue(window);
}

How can I show a FolderBrowserDialog more than once?

In my Windows Form's Form_Load event, I want to show a FolderBrowserDialog to let the user select a directory, and if the directory they've selected is not valid (meaning it lacks certain files that the application needs), I want to show it again. However, when I create a new FolderBrowserDialog, it does not appear when I call ShowDialog.
while (ValidDirectorySelected() == false && tryAgain == true)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
tryAgain = false;
}
}
}
When I step into it, the dialog.ShowDialog() line is reached on the second time, and then nothing happens. The dialog does not appear, and the debugger doesn't move on. It just stops. It works perfectly the first time, but not the second. I've even tried just copying that entire using block and pasting it right after the first one, and the same thing happens. The dialog shows only once.
What do I need to do to show a FolderBrowserDialog more than once?
Solution:
Passing 'this' to ShowDialog fixed my issue. I also moved the using to outside of the while loop, to avoid needlessly re-creating the dialog.
Minimize Visual Studio, you'll find the dialog back.
This is a focus issue, triggered because you display the dialog in the Load event. When the dialog closes, there is no window left in your app that can receive the focus. Your Load event hasn't finished running so the app's main window isn't yet visible. Windows has to find a window to give the focus to and will select one from another program. Like Visual Studio.
When you display the dialog again, it cannot steal the focus back because Visual Studio has acquired it. So the dialog appears behind Visual Studio's main window, out of view.
You'll have to fix this by allowing your main window to become visible. And call dialog.ShowDialog(this) to be completely sure. You could use the Shown event, for example.
Try this:
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
while (ValidDirectorySelected() == false && tryAgain == true)
{
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
tryAgain = false;
}
}
}
...move your using outside the while loop to keep from destroying the folder browser every time. You don't have to do that. You can reuse FolderBrowserDialog.

Resources