How do I use Selenium RC in C# under Microsoft Visual Studio?

432 views
Skip to first unread message

Bruce Mihura

unread,
Nov 30, 2008, 11:00:19 PM11/30/08
to selenium-users...@googlegroups.com
I recorded a job-hunting script using Selenium IDE in Firefox in Windows XP, then decided to see if I could run it as C# code under Microsoft Visual Studio. I exported the C# version of the script from the Selenium IDE, and created a Windows Forms project in Visual Studio. Everything compiled and linked, but at run-time I got an error at the "*selenium.Start()*" line in this code:

        public void SetupTest()\\         {\\             selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.monster.com/");\\             *selenium.Start();*\\             verificationErrors = new StringBuilder();\\         }

The error was this:

+An unhandled exception of type 'System.Net.WebException' occurred in System.dll+
+Additional information: Unable to connect to the remote server+

I think I need to pass different arguments to DefaultSelenium(...) so that it links up to my Firefox application, but I don't know how to do this. I added Firefox's directory to my PATH environment variable, as suggested by these discussions.

ejohn

unread,
Dec 1, 2008, 6:23:06 AM12/1/08
to selenium-users...@googlegroups.com
Your applicateion tries to connect to the selenium server. So you must launch the server first - start selenium server on your local machine from the comand line:
*java -jar selenium-server.jar -port 4444 -multiWindow*

As soon as you selenium server is running you are able to connect to it through .net client driver

Bruce Mihura

unread,
Dec 1, 2008, 1:59:41 PM12/1/08
to selenium-users...@googlegroups.com
I tried the

*java -jar selenium-server.jar -port 4444 -multiWindow*

command under the Command Prompt in Windows XP, and that almost worked. The server appeared to be starting, but finally my C# software returned this error:

+An unhandled exception of type 'System.Net.WebException' occurred in System.dll \\+
+Additional information: The remote server returned an error: (500) Internal Server Error.+

Looking over at the Command Prompt window for more information, here's what was there:

+        at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534) \\ Caused by: org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher$Fil \\ eLockRemainedException: Lock file still present! C:\DOCUME~1\COMPAQ~1\LOCALS~1\T \\ emp\customProfileDir0aeec1abcca6414a99e19b6157608a4a\parent.lock \\         at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.wai \\ tForFileLockToGoAway(FirefoxChromeLauncher.java:247) \\         at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.wai \\ tForFullProfileToBeCreated(FirefoxChromeLauncher.java:288) \\         ... 18 more+

Since I'm just guessing at how to make this work, here are a few basic questions:

1. Was I supposed to run "*java -jar selenium-server.jar -port 4444 -multiWindow*" from the Windows Command Prompt, or is there a better way?
2. I arbitrarily picked a "Windows Forms Application" project from Visual Studio, because I'm used to that. But would "Console Application" or "WPF Application" work better?
3. How do I debug this?

Bruce Mihura

unread,
Dec 1, 2008, 2:02:06 PM12/1/08
to selenium-users...@googlegroups.com
+Why a windows forms project?+

Just because I'm more experienced there than other project types. I'll try anything that works, though!

ejohn

unread,
Dec 1, 2008, 3:11:56 PM12/1/08
to selenium-users...@googlegroups.com
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "[http://www.monster.com/");|http://www.monster.com/%22%29;]

Why do you use *chrome browser launcher?

Try this:
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "[http://www.monster.com/");|http://www.monster.com/%22%29;]
or
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "[http://www.monster.com/");|http://www.monster.com/%22%29;]

What kind of application winform/cpnsole to use is up to you ))

Bruce Mihura

unread,
Dec 1, 2008, 5:53:29 PM12/1/08
to selenium-users...@googlegroups.com
+Why do you use *chrome browser launcher?+

That +*chrome+ argument was the code that the Selenium IDE generated when I was using Firefox. I tried the +*firefox+ argument there, but I got the same ++FileLockRemainedException++ results mentioned earlier.

I made it a little further with the +*iexplore+ argument. Internet Explorer actually launched, went to www.monster.com, and made it to the *WaitForPageToLoad* line in my code here:

        public void TheNewTest()\\         {\\             selenium.Open("/");\\             selenium.Click("link=Advanced Search");\\             selenium.*WaitForPageToLoad*("30000");
            ...
        }

It then timed out, even though I could see the page had clearly loaded. Changing the timeout to 300000 didn't help, either.

So I'm stuck in one place with Firefox, and a little bit further down the road with Internet Explorer. I'd be happy to make progress with either browser if you have any ideas.

ejohn

unread,
Dec 2, 2008, 4:43:48 AM12/2/08
to selenium-users...@googlegroups.com
1) You can write a *.bat file to start Selenium Server.
2) Console application is better because you do not really need all that annoying forms :)
3) You can debug all these using Visual studio inline debugger.

ejohn

unread,
Dec 2, 2008, 4:47:57 AM12/2/08
to selenium-users...@googlegroups.com
try to close all instances of Fire Fox before launching the screipt.
Are you shure your page is reloaded after click?
You can also try this:
selenium.*WaitForCondition*("selenium.isElementPresent(\"<LOCATOR>\")");
where <LOCATOR> is the id of some element that appears on the page after you do the click action.

Bruce Mihura

unread,
Dec 2, 2008, 7:49:59 AM12/2/08
to selenium-users...@googlegroups.com
That selenium.*WaitForCondition*() function will get me going under Internet Explorer, thanks.

Although there still is the mystery about the *FileLockRemainedException* from selenium.Start(); under Firefox, it doesn't happen under Internet Explorer, so I'll defer that to a later thread someday if need be.

Thanks for getting me started under Windows *ejohn*, all in one thread!

Reply all
Reply to author
Forward
0 new messages