Make selenium webdriver start the command prompt minimized? - c#

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.

Related

Selenium Chrome webdriver hangs occasionally 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?

IE Windows Security dialog appears to block Selenium navigation

I am using Selenium in C# on Windows 10. The site under test should challenge with a Windows Security login box in IE, which it does. But the login box appears to block the call.
var home = "https://site.under.test.com/";
driver = new InternetExplorerDriver(AppDomain.CurrentDomain.BaseDirectory);
driver.Url = home;
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(home + "secure/");
//code to handle login box goes here, never gets executed unless the dialog box
//is manually addressed or something times out in GoToURL(),
//and then the dialog box doesn't work.
The login box appears:
Click here for screenie of the login box
But the execution is stalled on the GoToUrl() call:
Click here for screenie of execution
Doesn't matter what code I place after this to handle the popup, execution is blocked until something times out inside GoToUrl().
Is this expected behavior? How does one get around it?
Clarification: The problem is not how to enter data into the popup. It is about the code execution not advancing to the point where I can enter data into the popup without intervention or timeout.
This works on Win8.1, but not on Win10
The Webdriver no longer officially supports Basic Authentication. It has been removed from the Java code and the .Net version works accidentally. From my experience you can expect it to work (for now) on Win7-8.1, but not on Win10
This from Selenium support:
The support of authentication dialogs was removed from the java bindings with this commit.
I am surprised, they are still present in the c# binding, because there is no support for basic authentication in the webdriver specification.
Not sure if you found the answer yet but here is what I found so far.
.GoToUrl waits for the page to load and since the page has not loaded yet, it waits and waits and throws exception.
You can enter the url usign Javascript. Below for is .Net code for it
string script = "window.location = \'" + url + "\'";
((IJavaScriptExecutor)driver).ExecuteScript(script);
You have to manage your own wait after using the above code.
And Selenium still doesnt work with the Windows Security dialog that you see after this (Or, I havent found any info anywhere that shows how to make Selenium work with this dialog)
The best solution I have found so far is to use AutoIT, and use TAB commands within AutoIT to enter username, move to password adn enter password and then do tab tab until you get to submit button and then click on this location. Please comment here if you have found any better solution than this until now

Process.WaitForExit() not waiting for multiple Chrome instances

I'm trying to start Chrome from WPF application using .NET framework 4.0 with code similar to the following:
var arg = string.Format("--app=\"{0}\" --window-size=1024,1000", "http://bing.com");
this._process = Process.Start("chrome.exe", arg);
this._process.WaitForExit();
// Perform relevant operations once the process completes/ exit
I'm opening chrome in app mode.
Case 1: When I have a no additional Chrome instance/window opened, WaitForExit blocks the control till the Chrome window created through code is closed - This is what I'm looking for.
Case 2: If I have a Chrome instance running already. Then it does not wait for the chrome instance created through code to exit and moves on to the next line. I want to have similar experience as in case 1, that is the control should be blocked until the user closes the chrome instance.
Is there anything extra I need to do get this working when I have multiple instances of Chrome already opened?
You can use Process.GetProcessesByName to get all opened chrome process,and invoke WaitForExit for each process
Process.Start(#"%AppData%\..\Local\Google\Chrome\Application\chrome.exe",
"http:\\www.YourUrl.com");
You can also try with dis
Process.Start("chrome", #"http://www.stackoverflow.net/");

Open Google Chrome developer tool console while C# selenium test is running, or read it programmatically

Is there a way to tell WebDriver in C# Selenium tests to open Chrome developer tool console, or some other way to get console to open while running Selenium tests without breaking them?
Or ability to programmatically read output to the console?
So far I have tried opening console manually (CTRL + SHIFT + I) while test is running, but that did break the test every-time.
To open chrome console:
var inSim = new WindowsInput.InputSimulator()
inSim.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.LCONTROL);
inSim.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.LSHIFT);
inSim.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.VK_J);
inSim.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LSHIFT);
inSim.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LCONTROL);
What you can do: take a snapshot of the browser (including the opened console).
What you cannot do: use the webDriver (it will crash the test, but if you close the console, the same way you opened it, you will be able to continue)
Why: selenium needs an exclusive connection to DevTools.
Notice - some OS have strict input rules and might prevent the inputSimulator from working when the computer is locked or when you are running this code in a machine which has no keyboard connected to it (a server that is handled remotely)
hope this helps...

Process.Start("IEXPLORE.EXE") immediately fires the Exited event after launch.. why?

i have a strange problem with IE8 installed in xp. i was trying to launch IE using an System.Diagnostics.Process.Start method in c#. And i have a requirement to trap the exited event of the IE and do some operation. But i ended up in a rather strange problem where the IE immediately fires the exited event after launch.
this is the sample code
Process objProcess = Process.Start("IEXPLORE.EXE", "http://google.com");
if (objProcess != null)
{
objProcess.EnableRaisingEvents = true;
objProcess.Exited += new EventHandler(myProcess_Exited);
}
public static void myProcess_Exited(object sender, System.EventArgs e)
{
MessageBox.Show("You exited");
}
But the above code perfectly works when laucnching different process (ex:notepad) and it fires the exit event when i close the exe.
this only gives problem launching IE 8. Can someone clarify me what is the problem??
UPDATE
Most friends replied my post and saying why you can't just use an URL? why stick with IE?
here the reason
the ultimate aim of the app is to launch an URL from the windows application and will hide an exe when working on the IE. And show the exe after closing the IE.
Thanks
Most probably is that you have IE already running as a process, so when you try to launch it again as a new process it looks that there are IE running already, tells it that user initiated a new window (so the initial IE will create a "new" window rather than a new one) and exit.
Possible solution:
try starting the process with "-nomerge" command line option:
Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");
Interesting observation: objProcess.ExitCode (for IE8 at least) will be equal to 0 if exited passing control to another instance, and 1 if it was actually closed by user.
If another instance of iexplore.exe is already running on the machine, new instances will connect to that and immediately exit. Also, it's possible that even in the case where iexplore is not running, the multiprocess architecture of Internet Explorer 8 has the parent launch child broker process and exit immediately.
But these answers are besides the point. You should not be launching Internet Explorer directly. If the user has configured another default browser, they will be unhappy that you are ignoring their preferences. Instead, why don't you try
System.Diagnostics.Process.Start("http://google.com");
directly and that will do the right thing. You won't be able to tell when the browser closed, but if the command has opened a new tab in an existing browser session for example, the browser close event will be meaningless to your application.
Maybe IEXPLORE itself launches a different process for the URL and ends the process you created? Like a fork on Unix?

Categories

Resources