Selenium Chrome webdriver hangs occasionally c# - c#

I'm using selenium webdriver to handle Chrome browser for long running process, and some times it hangs on opening new url. This occurs randomly as I noticed, when the browser window is not active for long time. As soon as I activated the browser window, the browser resumes and loads the website. I'm using following options to initiate the driver.
ChromeOptions opt = new ChromeOptions();
opt.AddArguments(#"user-data-dir=./chromeprofile");
opt.AddArguments(#"disable-infobars");
opt.AddArguments(#"disable-notifications");
I guess the Chrome is pausing the browser until the user returns to the browser window. I just want to know is there any option to add here, to keep going the process even if the browser window goes inactive for long time. Can anyone point me in right direction to find a way to keep the browser working for long time without focus?

Related

Is there any way to hide chrome selenium?

I have tried
options.add_argument("--headless")
driver = new ChromeDriver(options)
it's working fine but my requirement is to hide it after initialization.
As you can see here: How to execute tests with selenium webdriver while browser is minimized there is no way for the browser to be minified.
You could in theory move the browser out of view as described here https://sqa.stackexchange.com/questions/15484/how-to-minimize-the-browser-window-which-was-launched-via-selenium-webdriver
driver.manage().window().setPosition(new Point(-2000, 0))
Chrome headless is an instance of chrome. You can't start from normal mode and switch to headless mid test. Once you start testing with any kind of browser mode, you need to stick to it. There are some work arounds I've not tried with opening windows with code and continuing from the new window.
Use phantomJSDriver for Headless automation
WebDriver driver = new PhantomJSDriver();

Selenium C# Click() cant get to another URL while using Jenkins

I have 30 tests working locally with Selenium C# in headless mode.
The problem occurs, when I try to use Jenkins. The message is "Unable to locate element".
It took lots of time to understand that the problem is not time wait and not frame size.
Chrome options (they say frame size will help, but its not):
ChromeOptions co = new ChromeOptions();
co.AddArgument("--window-size=2560,1440")
co.AddArgument("--ignore-certificate-errors")
co.AddArgument("headless")
Tested web application was made with vue.js (everything is dynamically generated).
To get the page or subpage displayed by URL Jenkins needs about 2-4 seconds:
driver.Url = "http://app.com/home" //not my URL
And every Click() works good on a certain URL: you can click anywhere, even on a pop-up form, if the URL is the same (Click() takes about 600 ms).
But if you Click() on element which directs to another URL, the page is not displayed (even if you wait 80 seconds).
This problem could be solved by using
driver.Url = "http://app.com/home/menu"
but thats not real testing.

Multiple IE web-drivers : session lost when click link/button that opens a popup window

I run multiple (2) IE web-drivers together.(for user side and admin side tests)
When the InternetExplorer WebDriver click on link/button that opens an modal popup, login page are opened on new window (abnormal).
When the ie webdriver popup the window , I can see for a fraction of a second the correct address the browser should load (in URL line) , but immediatly it changed to the login url, and required enter username and password.
I found the same problem in this link,
I made the solution there, but I still have not solved the problem, and I get an entry window instead of the expected window.
see this screenshot
My IE version is 11.
My Selenium.WebDriver is 3.141.0.0
My ie webdriver is IEDriverServer_x64_3.141.5
My InternetExplorerOptions:
private static InternetExplorerOptions ieOptions = new InternetExplorerOptions
{
EnsureCleanSession = true ,
EnableNativeEvents = true ,
RequireWindowFocus = true ,
EnablePersistentHover = true ,
ForceCreateProcessApi = true ,
BrowserCommandLineArguments = "-framemerging -private"
};
I added the registry keys:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FrameMerging\(DWORD)00000000
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\TabProcGrowth\(DWORD)00000000
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE\iexplorer.exe\(DWORD)00000000
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE\iexplorer.exe\(DWORD)00000000
Edited:
By the Deepak-MSFT references, I understand that the issue caused by more than one IE web-driver instances.
I tried to kill the IE driver also by adding the ConfirmCleanSession = trueoption to the code, and also manually by running of the following statements from the command line (before running the test);
taskkill / F / IM iexplore.exe / T
taskkill / F / IM IEDriverServer.exe / T
But it did not help once I set 2 IE web-drivers to work together.
(When I tried to run the test only on one driver - it worked great!)
I would appreciate help in solving the problem!
This issue is due to a quirk of IE itself and how the driver creates the popup window for the browser.
The workaround is to ensure there are no iexplore.exe processes running when you start the session with the driver.
References:
(1) session cookie lost when click made to a link that opens a window with window.open()
(2) session cookie lost when click made to a link that opens a window with window.open()
My Solution:
Split the test case into two tests:
First Test -
Use the IE web-driver for the first site (in my case - the admin side), and parallel use another web-driver (which does not cause problems like Chrome web-driver) for the second site (in my case - the admin side)
And a second test-
Use the other web-driver above for the first site, and parallel use the IE web-driver for the second site.
So I covered the tests in IE for the two sites.

Selenium, how to take screenshot without opening the browser?

I want to take a screenshot of a webpage using Selenium.
I have notice that the action to take the screenshot require to open the web browser itself.
tried to change webDriver.Navigate().GoToUrl("http://www.google.com"); with webDriver.Url = "http://www.google.com"; but no success,
I even tried to leave it with no url and the browser opened with url of 'data', which now I understand that something else makes the browser to be open.
private void button1_Click(object sender, EventArgs e)
{
var capabilitiesInternet = new
OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.
SetCapability("ignoreProtectedModeSettings", true);
IWebDriver webDriver = new ChromeDriver();
webDriver.Navigate().GoToUrl("http://www.google.com");
Screenshot screenshot = ((ITakesScreenshot)webDriver).GetScreenshot();
screenshot.SaveAsFile("E:\\ScreenShot.png",
System.Drawing.Imaging.ImageFormat.Png);
webDriver.Quit();
}
No - you need to let the WebDriver request the page, otherwise how can it know what screenshot to produce?
If you're trying to avoid a real, 'slow' browser starting up and opening a window, you should either consider running that browser headlessly, as per:
How do I run Selenium in Xvfb?
Or check out the headless WebKit browser PhantomJS (or maybe SlimerJS), and using almost exactly the same WebDriver API as you have now, ask it to produce your screenshots 'in-memory':
Phantomjs - take screenshot of a web page
Just replace:
WebDriver webDriver = new ChromeDriver();
with:
WebDriver webDriver = new PhantomJSDriver();
(Obviously requires the application to be installed locally)
Edit: Just a note that the typical use-case for this is 'overnight' continuous-integration / continuous-testing when run from headless CI servers. However, it can be very easily added to other work-flows, e.g. for visual regression-testing, and simple one-off checks.
There's multiple ways to go about it:
There is chrome flag --no-startup-window which should open chrome without window. I tried it, and it didn't work, however, you can give it a go.
Use hacks - there is a way to start chrome without you ever seeing it, it works nicely. Just use --window-position=-9999,0, note that, TakeScreenShot() has some weird anomalies in my experience - it focuses the window, it runs out of memory randomly, etc. What I ended up doing is I wrote chrome extension which can take screenshots and return them back to selenium through JS.

Make selenium webdriver start the command prompt minimized?

I am using Selenium PhantomJS. When my program starts it opens command prompt (cmd) which then turns on the browser and does everything I have programmed it to do.
According to this answer there is no way to hide the command prompt (until v2.4 gets released), so I want to ask if there is a way to at least start it minimized or maybe stop Windows from focusing it after it pops up.
I tried the following code to programmatically press Alt+Tab:
IWebDriver driver = new PhantomJSDriver(); //this starts browser and opens cmd
System.Windows.Forms.SendKeys.Send("%{TAB}");//alt+tab
Unfortunately this only freezes the mouse as if I am in endless loop until the program ends.
I am using Windows 7, VS2013.
Well, the new version 2.40 is out, so here is the solution - https://stackoverflow.com/a/21949015/1115382 - credit goes to #Martin Su.

Categories

Resources