Selenium WebDriver new tab and Navigate - c#

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!

Related

How to close Window in WinUI3?

I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.

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.

Window shown as dialogue losts its focus - wpf

I have a main window Say home and from there i am calling another window named addItem as
var item = new addItem();
item.ShowDialog();
Its working fine.
But when we navigate to other applications like chrome, notepad by alt + tab and come back to the WPF application both the windows are separated meaning as like in winforms it wont stick together
So user got confused in that behavior.
Requirement is unless until the dialogue window is opened it should always
be on top and main window on the back and when we click the icon in task bar both window together should come.
try this
var item = new addItem();
item.Owner = this;
item.ShowDialog();

Open a dialog in the same window and wait for user input

Is there a way to open a new window instance within the primary application window and still wait for a user response before continuing with the main program? I just have a 3 option query with a picture to be offered to the user and a whole new window opening up seems like overkill, I would much prefer it opening in a frame within the current window.
Do something like this sans the separate window:
OptionWindow optionDialog = new OptionWindow();
optionDialog.Owner = this;
optionDialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
optionDialog.ShowDialog();
if (optionDialog.DialogResult == true)
{
something;
{
Seems like you could just have your "OptionWindow" be a frame that is on the WPF form, and when you want to show it, just disable all other panels and show the frame with your dialog, and after the user completes the dialog, enable all other panels and hide your dialog frame.
After I posted my answer, I saw that Mark Hall made an edit to his comment with pretty much the same suggestion. Sorry Mark.
I guess you are looking for modal window which can be achieved using Window.Showdialog.
Opens a window and returns only when the newly opened window is
closed.

WebDriver + swtichTo another browser

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.

Categories

Resources