Selenium Webdriver c# when debugging if test fails Chromedriver does not close - c#

I've run across an issue when running my Automation Tests. If i am debugging a test, it fails. If i after that stop the debugging, the browser is left open and the Chromedriver.exe task is left running in the background. its easy to close the chrome browser but the Chromedriver.exe is a bit more annoying as I could do a lot of debugging and have many of them left open. is there a correct way to close these in debugging and can I stop the test mid way? In my tests I use the
public void Dispose()
{
_driver.Quit();
}
and this works as long as I am not in debug mode and I stop the test in the middle. Any ideas on what should be the proper method to close these? Maybe a misunderstanding on my side to how im using Visual Studio. Any help is much appreciated.
Edit:
Im not sure thats related. When in debug mode, if you are running an automation test and if it fails. If you then stop the test at the fail point it leaves the browser open and the chromedriver running. my question is,
Is there a correct way to exit debug mode that closes the left open processes and browser. or is this a manual process or something that you use to do this outside of Visual Studio?

I do the same thing all the time. The problem is that you are stopping the test mid-run. When you do this, you leave the browser open. There are a couple options.
When you are done debugging, just hit Run. It will attempt to continue and fail and should close everything down properly.
Keep using it the way you currently do. You can kill processes using Task Manager, as needed. I have considered writing a batch file that kills all the chromedriver.exe processes, etc. You could do that and have it target IE and FF also to make cleaning up a lot of them easier.

Related

mySolutionName.exe file deleted from /debug directory. How to restore or rebuild?

I'm new to c#. Antivirus deleted the mySolutionName.exe file out of the /debug directory and now I cannot execute my code. I'm concerned that anything I do may make the issue worse.
When I press F5 I get an error of:
CS2012 C# Cannot open for writing --> C:\Users\me\source\repos\MyApp\MyApp\obj\Debug\MyApp.exe''
Can someone please tell me how to rebuild the file so that I can continue developing?
after your edit:
Your program is probably still running outside of the debugger.
You need to use the task-manageer to kill all instances of MyApp.exe if this doesnt solve your issue a reboot should do the trick as well
So why is this happening?
Most liky your application is somewhere stuck on a blocking function or in a never ending loop. maybe there is even a seperate thread still operating that you forgot to close. We lack some information to tell yout that for sure. But to find out what is happening you can close your window while running in debugger mode and see if the application properly closes. if it doesnt you can hit pause and see where the program is stuck and resolve this issue by ending the task/loop/whatever in your OnClosing function of your window.
A good way to solve this issue is looking which process currently accesses the file.
A tool to do that is Microsoft SysInternals Process Explorer. It has a feature called "Find handle or DLL ..." which can be accessed by Ctrl+F.
The result will show the process which accesses the file. You can then judge whether it's Antivirus or something else that prevents you from writing to the file. If possible, you can then take an action in that program to release the file.
Example: a program is accessing my powerpoint presentation, which has the term "Schulungen" in its file name.
Process Explorer figures out: it's open in Powerpoint.exe, so I can simply close the file in Powerpoint - problem fixed.

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 how to close Internet Explorer has stopped working

I am screen scraping a legacy website that uses activex controls. I am using selenium to accomplish this.
This website is buggy and whenever you signout of it, it crashes IE and you get the "Internet Explorer has stopped working" window. This happens no matter what, website needs to be recoded.
Is there a way in InternetExplorerDriver.Quit() to detect if this window is up and close it? The annoyance is that while the dialog is doing "searching for a solution..." it doesn't release selenium, so my code hangs up waiting.
Right now I am attempting to kill the process through the windows api, but that requires waiting for selenium to finish closing.
The solution Richard posted in the comments worked. I just told windows to stop checking for solutions.

How to change selenium WebDriver priority from c#?

I'm trying to make my tests run faster on a dedicated server. I've noticed, that normally the tests run sluggishly, but when I increase firefox priority (which by default is lower than normal), they run much faster.
I was looking for a setting in FirefoxDriver which would let me choose process priority, but I can't find one.
Can anyone point me to how to set web driver priorities in selenium?
I disagree with why you are doing this, and I think simply changing the priority is not the way to solve your issue.
There is no API exposed to do this, so you could send a request off to the Selenium developers for this (http://code.google.com/selenium).
Due to this, you will have to change the priority process after Selenium has created a browser session.
You will need to find the process:
var fireFoxProcesses = Process.GetProcessesByName("firefox");
This will return an array of Process objects, however, if you are running one test after another, there should only be one firefox.exe process open. This is my assumption. Therefore, we get the actual process object:
// should only be one, unless you are opening a few tests in concurrently.
var actualFirefoxProcess = fireFoxProcesses.First();
Finally, change it's priority class:
actualFirefoxProcess.PriorityClass = ProcessPriorityClass.High;
I would guess this can get a little unreliable though.
Edit
As for differentiation of a 'user created' Firefox, and one run by Selenium, you can look at the parent process of the firefox processes. That is, what process launched the Firefox process?
No point in copying code, but this solution worked well for me: How can I get the PID of the parent process of my application ...this then gets tricky because a user can launch Firefox multiple ways, but if they are using a shortcut/start menu list item, the parent process will be explorer.
You've not mentioned what solution you are using for running the tests. Whether it's through Visual Studio's Test Runner, NUnit's own GUI, TeamCity, CruiseControl, Jenkins, TFS or some other CI solution, but you'll need to check what launched the Firefox process in order to determine whether it was a "user created" Firefox instance or one from Selenium tests.

Visual Studio 2010 Pro, not ending debug mode on application close

When I run my code within VS, when I enter certain methods, upon ending the application (closing the form) been debugged, VS does not end the debug session automatically forcing me to end it manually.
This doesn't always happen, the results are consistent with certain windows / classes loaded when the application is run within VS.
I want to know why this may be, I'm assuming its an indication of a file not been closed in my managed code or 'something' like that.
Why might this occur and how can I trace and fix it?
This would happen if your code keeps running after closing the window.
Pause the debugger after closing the window to find out what it's doing, then modify the code so that it stops running when you close the window.
On Debug menu, click "Detach All".
Hope that helps!
EDIT
Follow, SLak's advice, as that will go after the problem. My solution may only fix the symptom. I will leave the answer here just in case it is helpful to you though.
Good luck!

Categories