C# IE11 selenium webdriver, No new window handles with new tab - c#

Selenium Webdriver for Internet Explorer 11 is not creating a new window handle when my app opens a new tab.
The problem is similar to this , which was a problem with the window handles disappearing in a fraction of a second, but in my case a new window handle isn't even created in the first place.
I used the test code from that link to check for disappearing window handles, but it never created another window handle when a new tab was opened, so the test only displayed the first console output and never the second part.
I tried using:
driver.SwitchTo().Window(driver.WindowHandles.Last());
but the Selenium Webdriver focus stays on the first tab, because I believe it isn't creating a new handle for the new tab.
Also, tried this:
driver.SwitchTo().Window(driver.WindowHandles[1]);
and that results in an "out of range" exception and "Index was out of range. Must be non-negative and less than the size of the collection."
If I change the [1] to a [0], then obviously I do not get the exception because the focus is just set to the first tab.
Based on my testing with the test code in the link I provided and my own attempts, I believe that new window handles are not being created.
I tried resetting IE11 and then only changing the settings as described on Selenium's website including adding the registry key, which I used Helium's solution to that found here.
Also, I read some suggestions of adding the websites that I am opening to the Trusted sites list in IE, but still no go.
Any help is appreciated. Thanks.

Hopefully this will help someone going forward...
I have been researching this after having the same issue and whatever I tried it did not resolve it!
In the regedit I added the 'BF_Cache' -> 'iexplorer.exe', added '2500' set as '3' in the zones 0,2,3,4 (was already present in zone 1) & Set the 'protected mode' to enabled but no luck... My new window would open correctly through Selenium but when debugging I confirmed only one handle was available!
But... After I nearly gave up - I decided to run visual studio as an
admin. And as if by Magic Selenium picked up that there was more
than one window handle!

Here's a workaround I managed to use.
You can use javascript to open a new window. It will give you the handle.
string javaScript = "window.open('" + url + "','_blank', 'resizable=yes, scrollbars=yes, titlebar=yes, width=1280, height=680, top=10, left=10');";
jsExecutor = (IJavaScriptExecutor)ieDriver;
jsExecutor.ExecuteScript(javaScript);

Related

How to add extension to running remote multilogin webdriver? (Selenium C#)

First I tried to add extension directly from chrome webstore, but problem is alert popup which appears after you click "add to chrome" is not reachable by selenium. After I wanted to try using options.AddExtension, but multilogin profile starts before selenium takes control so this method is useless. I know that I can manually import .crx for every profile but there are thousands of profiles and I need to automate process of installing extensions. I don't know what to do, I heared there's option to reach popup alert in chrome webstore but can't find it. Or maybe there are other ways to install extension after browser started? I'm glad for any help or advice
I have 2 possible scenarios for you, hope that works out since I don't have direct access to this specific need you have because the extensions may differ.
Not using Selenium, you gonna start the chrome process using the class Process and add the flag --load-extension= to load the specific profile/extension that you need. You can see the entire comma here. To sum, you can use this snippet below to load:
chrome --user-data-dir=/tmp/someuniquedirname --load-extension=path/to/extension --no-first-run //Note: some flags may change between versions of chrome, see full documentation
After defining the extension and start chrome, you can now get hold of the process with Selenium by using another flag: --remote-debugging-port=http://localhost:[localporthere]. After that, start the process than tell to Selenium to get hold of that process with the port and do your job.
Another way is to start the process installing the extension manually and in another Thread use some Automation UI (Teststack.White or FlaUI) to click on the popup you have. I can't extend here in the entire code for this solution because it will go to a opinion-based answer, but you can check on FlaUI for that use, and follow that path:
Selenium starts the program, click on the install extension and wait for the popup to show-up;
New Thread;
FlaUI gets hold of the process that you already start;
Using UIElements, click on the "ok" button you need;
FlaUI drops;
Back to the main thread.
If you need any clarifications about the solutions above, just comment and I'll try to help you further.

chromeDriver window crushes during wait.until() C# selenium

I'm having a bit difficulties with chrome driver and selenium, i have selenium.support version 3.141.0 and chrome driver version 2.43.0.
during a test run i click on a button which redirect me to a new page, so i use WebDriverWait.until() until a condition that check if a certain element exist is met but for reason the window is flashing for a few seconds and later just close.
_wait.Until(d => d.FindElements(By.XPath("//div[#id = 'expression']")).Any());
edit: i found that the window is flashing and later shut down after this line which come after the wait.Until.
_driver.FindElements(By.ClassName("connections-list")).First();
the line return Sequence contains no elements although the element is visible and it still doesnt explain why the window shut down
Suggest you to use External supported library, And check your scenario with same:
Library : SeleniumExtras.WaitHelpers
You can use it as:
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(WebElement));

C# Selenium No new Window Handles When Opening A New Tab In Chrome and Firefox

Selenium for Chrome and Firefox, coding in C#, will not handle switching over to the new tab that is opening during my automation test. I don't think it focuses on the new tab open because when I run my program completely, I receive this error: "out of range" exception and "Index was out of range. Must be non-negative and less than the size of the collection."
I've tried using:
driver.SwitchTo().Window(driver.WindowHandles.Last());
and
driver.SwitchTo().Window(driver.WindowHandles[1]);
driver.SwitchTo().Window(driver.WindowHandles[0]).Close(); // close the first tab
System.Threading.Thread.Sleep(5000); //Wait 5 seconds just in case
driver.SwitchTo().Window(driver.WindowHandles[1]); //Makes sure I'm on the correct tab
but there is no luck. If I don't close the first tab, I receive the error: "NoSuchElementException:Unable to locate element:{method...}" which I assume is correct because the focus is still on the first tab. Any help would be greatly appreciated. Thank you.
Once you've closed tab[0], the old tab[1] becomes the new tab[0], so you should be switching to tab[0].
Selenium operates in the background so the window does not need to be in focus while it is running. This also means that Selenium will never change the focus back to the browser window. If you are watching the window while your code runs however you should be able to see the browser changing tabs.

Opening windows file dialog with selenium

I know this issue has been addressed a lot, but I haven't found a problem similar to mine, so please tell me if there's a solution.
I'm using selenium webdriver (chrome) and C# to test a web application.
In the application I have a button, which opens a windows file dialog in order to select and upload the file.
I am using Click() to click on the button and SendKeys() to paste the file's path in the windows dialog and to hit "Enter".
I'm not trying to control the dialog with selenium.
It's successful most of the time, but sometimes the dialog isn't opened once the button is clicked and it seems to be frozen for several minutes (it's impossible to click the button manually as well), but the test resumes as if the dialog had been opened. After 2-3 minutes the windows dialog finally appears, but needless to say that the entire test is messed up.
It is not a problem in the program itself, as the problem never occurres when the click is preformed manually.
What could be the problem and how can I solve it?
Please have in mind I cannot change the program I'm testing.
Thank you
You can do it without White using Microsoft UI Automation directly.
Without TestStack White. No sense to use the whole library for one window automation. White is wrapper only.
var FirefoxWindowElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty,"MozillaWindowClass"));
FirefoxWindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty,"#32770"))
//You can navigate directly to input field or just use the keyboard because input field is always focussed
SendKeys.SendWait("YOUR FILE PATH here");
Keyboard.Press(Key.Return);
What if you create a wait for dialog?
public void waitForElement()
{
while(!yourDialogElement.Displayed)
{
yourButtonElement.Click()
System.Threading.Thread.Sleep(1000) // sleep for 1000 milliseconds
}
}
You will probably want to build a try/catch into that button click, and perhaps build a counter into your loop and pass a timeout limit.
Let me know if this works or if you need more help!
You should consider adding a library called WHITE to your framework. This acts like Selenium but for Win32 applications and can handle most types of Windows dialog objects via Microsoft UI Automation.
With WHITE set up, you can add a method at the point in your code where the button is clicked by Selenium that opens up the dialog window. This method can poll for the presence of the dialog and if, after a set time, the window does not appear you could either fail the test there and then or try clicking the button again.
You could also poll indefinitely until the window appears if you are confident it always will. I would set an upper limit myself though to 5 minutes or whatever you feel is right here to prevent some sort of infinite loop situation.

Taking an IE screenshot returns a black image

I'm building a console app which will connect to different computers in the network and take browser screenshots of a webpage.
Using Selenium 2.47.1 to set up server & nodes. The console app runs in the PC which is set up as selenium hub.
The screenshot is fine in firefox,chrome,ie from the hub computer.
The screenshot is also fine in firefox in remote pc.
But with IE it returns a black image.
Both the hub and node computers run on windows 7 64-bit, have IE11. I am using the 64bit IEDriver in both PCs.
The node computer is not locked.
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
static void Main(string[] args)
{
IWebDriver NewDriver = null;
using (NewDriver = new RemoteWebDriver(new Uri("http://172.165.10.111/wd/hub"), DesiredCapabilities.InternetExplorer()))
{
if (NewDriver != null)
{
NewDriver.Navigate().GoToUrl("http://www.google.com");
NewDriver.Manage().Window.Size = new Size(1804, 1096);
Screenshot ss = ((ITakesScreenshot)NewDriver).GetScreenshot();
ICapabilities capabilities = ((RemoteWebDriver)NewDriver).Capabilities;
ss.SaveAsFile(#"C:\Path\123.png", ImageFormat.Png);
NewDriver.Quit();
}
}
}
It's because your screen is locked, or a screensaver is running.
You will need to turn off your screensaver and configure windows to never lock itself when not in use. To turn off your screensaver:
Click the Start button.
Click Control Panel.
In the search box, type screen saver.
Click Turn screen saver on or off.
Then modify your screensaver settings. Make sure you have unchecked "On resume, display logon screen".
While you're in the control panel it's probably worth checking the power options and making sure the machine isn't going to sleep or powering down after a set period of time as well.
You will also want to use VNC or remote assist to access the GUI. If you RDP in it will lock the screen for the local user who is currently logged in and again it will lock the screen when you disconnect.
Finally don't use the 64-bit IE driver, you should be using the 32Bit one. Nobody runs the 64Bit version of IE (even if they have a 64Bit capable machine).
****** Edit
Adding a bit more info from a credible and reputable source ******
Below is a link to a post on the Selenium users forum where Jim Evans (the developer who wrote the IEDriver binaries) explains this:
http://selenium.10932.n7.nabble.com/IE-Screenshots-are-black-when-not-connected-via-Remote-Desktop-to-the-VM-hosting-an-IE-Node-td37004.html
This quote in particular about taking screenshots when you have disconnected from a RDP instance:
It's a known limitation. There is no known workaround. Complain to
Microsoft. They're the ones that make the PrintWindow API (which is
the proper API to use when grabbing screen captures) behave that way.
Either that, or if you discover a way to make it work, you're welcome
to submit a patch.
He explains how the screenshot code works in more detail on his blog here:
http://jimevansmusic.blogspot.co.uk/2014/09/screenshots-sendkeys-and-sixty-four.html
Specifically:
The IE driver takes screenshots using the PrintWindow API function.
PrintWindow can only take a screenshot of the visible portion of any
given window, which means that in order to get a full-page screenshot
(as required by the WebDriver API), the window must be sized large
enough to display the entire page without scroll bars. However,
Windows does not allow the window to be resized larger than the
visible screen resolution. When we ask IE to resize itself, a
WM_GETMINMAXINFO message is sent on a resize event so the IE can
figure how large a window can be. By intercepting that message with a
hook, and modifying the max values, we can trick IE into thinking that
a window can be sized greater than the screen resolution would
otherwise allow.
It looks like this might be a known issue with configuration:
https://code.google.com/p/selenium/issues/detail?id=3536
From what #ShubhasmitGupta mentioned above, it does look like the IE driver hands out black-screen screenshots when the desktop is locked (I'm assuming this has something to do with DWM/Explorer not rendering windows).
There's a workaround involving not locking the targeted computer when connecting to it via RDP. Essentially, you create a disconnect.bat file with the following contents:
tscon rdp-tcp#0 /dest:console
I'm not sure exactly when that's supposed to be run, but the concept is to prevent the test computer from being locked when connecting via Remote Desktop. If that helps, great; I haven't been able to test the workaround myself and figure out how to set it up. Maybe someone else can write a more concise answer.
Open up the Service Control Manager -- You can do WindowsKey+R (run) and type services.msc and hit <ENTER> and it'll start up.
Find the service and right click it and choose properties from the context menu.
Allow Service to Interact With Desktop
Now, select the Log On tab.
You want to make sure the checkbox is selected for "Allow service to interact with the desktop".
You can see it in the image below.
Note: I don't have that service so I just picked another one in the sample picture.
you may want to try removing below line
//NewDriver.Manage().Window.Size = new Size(1804, 1096);
maximize your browser instead.
NewDriver.Manage().Window.Maximize();
I have had a sneaky suspicion that IE has been rendering with DirectX for some time and this article certainly seems to indicate that this is the case... which would explain why you'd have problems as GDI based screen grabs won't work with DirectX surfaces.
I'm saying this is an absolute... just a hunch
I had the same issue a while back, and though I don't completely understand the problem it was basically that the process executing selenium was running under a system account and thus the IE gui was visible only in session 0, whereas session 1 was showing only a black screen.
The solution which worked for me was to create a local user account on the remote host, keep that user permanently signed in and unlocked so that they have an interactive desktop available, then run your selenium app/tests as that user. I know that is not ideal, but as we had dozens of old boxes lying around, it didn't hurt to use one as a dedicated test server. Hope that helps somewhat!
Sorry, not enough rep to add this as a comment.
Perhaps an interesting hint...
...working with Selenium 2.47.x and 2.52.0 Jenkins, RDP and IE 11 in mode IE 7 (setting by website) on the server Win 2008. Additional to this, I have to screens connected to my PC.
If I am disconnected from the Win Server (not logged off) I have two situations:
The JAVA prog on the Server produces black boxes or real screenshoots. The difference is just one marker in RDP options in the display part on the local RDP configuration of my client (Win 7).
I have not an english spoken display so I hope the translation may ok.
The configuration detail is: "using all monitors by remote session"
If this marker is set, I get real screen shoots. If this one is not set I have black boxes. I can't explain why, but it works in this way...
Windows would sometimes cause black screens because the machine/desktop was not in use.

Categories

Resources