WebDriver + swtichTo another browser - c#

I have seen umpteen posts talking about working with pop window using window handle.
But how to work with a browser which is opened when clicked on a button -
<button class="power_buy_now_button" type="button">
</button>
I tried to get window handle but each time I encounter a changing string, some thing like - "8c5f028e-e7cc-4d0f-afe4-983bb119391e"
There is not even title associated with new browser. More over I am not sure how I would use title to bring control to new browser. And then at some point I would have to bring control back to first browser.

Your best bet is to do something like the following:
// This code assumes you start with only one browser window in your test.
// If you have more than one browser window, your code will be more complex.
string originalHandle = driver.GetWindowHandle();
driver.FindElement(By.ClassName("power_buy_now_button")).Click();
// May need to wait for window handles collection to have a .Count of 2,
// as clicks are asynchronous.
string popupHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.GetWindowHandles();
foreach (string handle in windowHandles)
{
if (handle != originalHandle)
{
popupHandle = handle;
break;
}
}
driver.SwitchTo().Window(popupHandle);
// Do stuff in the popup window, eventually closing it with driver.Close()
driver.SwitchTo().Window(originalHandle);

I ended up in picking second handle and click on that. This approach works and I hope I would be able to get control back to main window also.

Related

How could you check if a window popup exists using Selenium w/ C#?

I'm currently writing a program that has a 'Submit' button that displays a list of providers within a certain area. However, a separate screen may popup if the provider has offices in more than one location (ie. different states, different offices around the same city, etc). If he's located in a single area, a dropdown occurs instead without the need to use window handling for the popup.
So far, my code fails 50% of the time since half of the providers operate in only one location and consequently, no popup appears. While it passes the other 50% since a popup appears and the code continues with no further issues.
My question is, how can I tell Selenium to search for a separate screen once I click the 'Submit' button. And if it doesn't appear, to continue on instead?
My code so far:
string currentHandle = driver.CurrentWindowHandle;
PopupWindowFinder finder = new PopupWindowFinder(driver);
string popupWindowHandle = finder.Click(BtnAccept);
if(popupWindowHandle != currentHandle)
{
driver.SwitchTo().Window(popupWindowHandle);
BtnSelectPopUpProvider.Click();
driver.SwitchTo().Window(currentHandle);
}
I don't know if it will be of any help, but what about using this construction ?
string currentHandle = driver.CurrentWindowHandle;
PopupWindowFinder finder = new PopupWindowFinder(driver);
try
{
string popupWindowHandle = finder.Click(BtnAccept);
if (popupWindowHandle != currentHandle)
{
driver.SwitchTo().Window(popupWindowHandle);
BtnSelectPopUpProvider.Click();
driver.SwitchTo().Window(currentHandle);
}
}
catch { }
On timeout the code below the try will continue to execute.

Why does a folder dialog immediately close unless a window is displayed in WPF?

I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.

Selenium WebDriver new tab and Navigate

Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.
I saw that I should use this:
driver.switchTo().window(windowName);
but what is windowName?
You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.
Here is a sample working code in java:
String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
Hope this helps!

Get WebBrowser.Document from another HTMLpage

My application opens up a HTML page as a popup and it is getting opened in a different window. (Browser : IE8)
I need to read the HTML content of this popup and close it afterwards.
I have been looking at different alternative of using FindWindow functions etc but no luck yet.
Any help would be appreciated. Thanks.
I'm not sure how you're opening the popup, but this article should help: http://www.quirksmode.org/js/popup.html
Notice how you have to declare a new variable, newwindow, that holds a reference to the popup window. From there, it's a simple `$(window).html().
You might add this to the onclick() of a link.
var newwindow = window.open(url,'name','height=200,width=150');
if (window.focus) {
newwindow.focus();
}
var html = $(newwindow).html();
return false; // if it's a link, you have to prevent it from opening the link's
//address in the original window
Edit: if you're not the one opening the popup, things are more iffy. You can get a reference to the topmost window using window.top, but that's about all I know, especially because the situation isn't quite clear.

new form problem c#

I'm aware that this title doesn't say much but it's really hart to explain what I want in few words.
I have two forms (main & help). Once I press button on main form help form pop ups. What I would like to implement is function to block user from doing anything on main form till he close help form.
I would not like to play with visible controls but I would like to have an effect you might have seen on some program that when user tries to click on main form help form "blinks" along with error sound playing. Once user close help form program works as usual
Hope you understand what I meant
This is called a modal dialog, and luckily, the answer is simple; show the child Form with the ShowDialog method instead of using Show. This is a blocking call that will not return until the child form/dialog is closed, so it means that you can check the return value and any properties if needed right after that line of code (probably not useful for a help window, but in most circumstances it is useful to check the user's action).
using( var dlg = new MyHelpDialog() )
{
if( dlg.ShowDialog() == DialogResult.OK )
{
// user chose "OK", do something (?)
// you can also access properties of the form after the fact
string whatever = dlg.SomeStringProperty;
}
}
You're looking for modal forms:
How to: Display Modal and Modeless Windows Forms
The thing you're talking about is called a "Modal Window".
See
How to: Display Modal and Modeless Windows Forms

Categories

Resources