How to select a window through Selenium and C# - c#

I am looking to write a simple selenium test that will find a window with a title or URL and then select it, then select swap tabs once. I have tried using various things like
driver.SwitchTo().Window(handle);
I have managed to write this function in Powershell, but because I cannot identify the Chrome window with Powershell I read that Selenium was the best way to go. I am using Visual Studio and have installed all the Chrome Drivers and everything.

No, you can't locate a window with a specific title or URL and then use it to switch tabs.
Driver.SwitchTo().Window(windowHandle)
The current top-level browsing context is represented in the protocol by its associated window handle. A top-level browsing context can be selected using the Switch To Window command as follows :
driver.SwitchTo().Window(windowHandle);
You can find a detailed discussion in What is the difference between WebDriver.SwitchTo().Window() and WebDriver.SwitchTo().Frame()

Related

How to validate greyed out tab on windows app via selenium C#?

I'm working on MSTest framework where the window based app is automated using selenium with .net C#.
I have a scenario to automate where all the options (tab, text or button etc) present on windows app are greyed out. So, when a user try to click on any of those option nothing should happen.
I thought to take the locator of any these option and put a bool condition to see if an app refresh or not, when user try to click on greyed out option. But I am not able to think well how to do so as I'm not good in coding :(
I took the AccessibilityId (locator) of the option (eg: Tab) and observe that in debug mode that WindowsApplicationDrive is returning Element in id form to the variable. So, now I don't know how to use the bool condition to validate this.
Any example help would be appreciated which I can relate and try to test the above scenario.
You can verify the buttons etc attributes - for instance if button is set to Disabled. Inspect the button when 'greyed out' and see what is set.

Embed a Powershell Window inside a Winforms form

Is it possible to embed a Powershell screen in my application?
I want to see the screen itself, just like Visual Studio does. I cannot use a console redirect as that option would not allow me to interact with programs or view programs displaying colors and the like.
Capture both standard input and standard output is not a possible solution. I am using programs that use colors and progress bars.
It would be something similar to being able to implement a Windows Terminal, or https://github.com/dwmkerr/consolecontrol
I also tried this code, but it does not allow me to embed to Terminal server although I can use it with Notepad Run extended programs GUI on my own c# form
Looking for information, I found a project that apparently could be what I am looking for but it does not work with Winforms and I cannot find a simple example of use. https://github.com/PowerShell/PowerShellEditorServices

How to make an assertion or verification of a import PDF modal is displayed?

I want to verify how to make a assertion or verification if this screen is displayed.
I do not have any idea of how to approach this do to this "modal" is not from the webpage, but from the browser.
It is possible to create a assertion of how this can be verified?
Before your test you can get the current window count with following :
int windowCountBeforeTest = _driver.WindowHandles.Count;
After your dialog shows up you can check it with following :
Assert.True(windowCountBeforeTest < _driver.WindowHandles.Count)
While I cannot find a link which absolutely says so, the w3c webdriver specification does not appear to support interaction with browser based prompts and windows.
In windows this mean you cannot interact with basic authentication or download/upload window prompts from the browser. I am assuming this is true on Mac as well.
You will likely need a 3rd party library or tool to interact with it. Within IE11 on Windows I am forced to use InputSimulatorCore to interact and close download prompts from the browser as it does not support auto saving files. I have heard of others using AutoIT but I have never used it.

Selenium ChromeDriver - Run in background but not headless

I am looking for a way to run selenium tests with a chrome driver, in the background. With the background I mean as in, not the foreground focussed window. I can actually do this, but as soon as actions like opening a new tab or switching between windows (so basically switching window handles) happen in the chrome driver, the browser window gets pushed to the foreground.
So my question is, how can I prevent this from happening without running the test headless?
Any suggestions are appreciated, open for discussion.
EDIT
As a somewhat temporary solution I came up with the following.
Using the Windows 10 Virtual Desktops feature, I run the test and thus the chrome browser window in a seperate virual desktop.
I then switch back to my main virtual desktop to continue with other tasks.
This prevents the chrome browser window from being forced to the foreground.
Note that this still makes a flashing chrome icon appear in the taskbar when any of the actions described above appear.
Still looking for a more solid solution, so any suggestions are still appreaciated.
To hopefully open new perspectives and discussion points, and ultimately a solution, I will provide some more detailed info of what my code is doing.
I have 5 chrome webdrivers, and each of these webdrivers contains 6 tabs (WindowHandles).
The idea is that a certain process has to be repeated continuously on each tab. So we loop over each webdriver, and within that webdriver over each tab and set this tab as the webdrivers current WindowHandle. This makes the chrome window visibly switch to the assigned tab.
After that switch has taken place, so basic selenium automation is performed on the content of the tab, after wich we repeat the whole process.
The actual issue seems to take place when a chrome webdriver switches to a new tab (WindowHandle), at this moment the chrome window containing the tab is pushed to the foreground and steals focus. Note that this does not always takes place, often it can switch tabs without any issues. So it is unclear wether there is another factor which would cause the window to steal focus.
EDIT 2
After doing the following:
I overloaded the selenium method which is used to switch between tabs (WindowHandles), and called SetWindoPos each time. Unfortunately this did not solve this issue either. I will try to look deeper into what might be causing this and will report back. – S. Van den Wyngaert
I went out for a few hours while running the tests, and came back to see that surprisingly I was still on my main Virtual Desktop (win10 feature). This means that the issue didn't occur during the time I was gone. I started working again, opened a chrome window and noticed that shortly after I did this, focus was stolen by one of the chrome driver's windows again.
What I conclude from this is that the issue only occurs when another chrome window (not opened by a chrome driver from code) is opened.
I will keep investigating and will report back with updates.
After investigating this behavior for a few more hours I noticed that when another chrome window is open, as long as this has focus, the issue doesn't take place. So to quickly summarize this:
The issue doesn't take place when:
No other chrome windows (not selenium driven) are opened or minimised
Another chrome window (not selenium driven) is opened and has focus
Another application running fullscreen mode has focus
The issue does take place when:
Another chrome window (not selenium driven) is opened and has no focus
Note that when I talk about another chrome window I specificly mean a chrome window that is not driven by selenium, so a regular chrome window opened by the user.
Easiest way would be to run a local selenium grid. Start your node(s) as a windows service. This way the test will run in the background, without being headless.
Another good option to scale your solution, and if your machine has the capacity to run docker, is to use
zalenium
It's a docker based, auto scaling, selenium-grid solution that works pretty quick out of the box.
You can watch your tests live via the management pages, watch a recording after the fact, pause/debug with live interaction via VNC. Also something to be said for not having the worry about changing browser versions.
Last time i used it, it had the odd bug and throws an end of stream error every now and then - but that was a good year ago.

Selenium driver open in new tab does not work as community shared knowledge describes

I know this is a contradictory topic. "Officially" the driver does not support tabs, however many places state that newly opened tabs will be available via window handles, and we can use the handles to switch between tabs.
(code samples are C# but I hope there is nothing special to C# in the question)
driver.SwitchTo.Window(myHandle);
I am trying to open a link in a new tab. I have success, the browser shows the new tab, however the driver's windowhandles does not contain the newly opened tab, only the one original window handle. This seems to be logical, the tab is not a window, however again, in many places it is described that it should work, and driver treats tabs as windows. What am I missing?
Open in new tab:
// Performing Ctrl + Click on my link:
new Actions(driver)
.KeyDown(Keys.Control)
.Click(myLink)
.KeyUp(Keys.Control).Perform();
// driver.WindowHandles did **not** change, still contains one handle
// The newly opened tab can not be reached, because we can not even switch
// the driver to it.
Open in new window:
// Performing context menu and "Open new Window" on my link
new Actions(driver)
.ContextClick(myLink)
.SendKeys("w")
.Perform();
// driver.WindowHandles **changed**, contains 2 handles
// Switch to the newly opened window works:
driver.SwitchTo().Window(driver.WindowHandles.Last());
Misc information:
Using Firefox v43.0.4
Using Official Selenium C# bindings v2.48.2 (nuget)
OS Windows 7 64 bit
One of the zillon places where tabs described as working: here (see all answer and comments too)
There is a difference between the browsers, for example in Chrome the driver does recognize two window handles. In FireFox I also have only one window handle but the focus is on the new tab.
To switch between the tabs you can use Actions
action.KeyDown(Keys.Control).SendKeys("2").Perform(); //to switch to the second tab

Categories