Re: [selenium-users] Starting with Selenium/ C#/ Visual Studio 2008

678 views
Skip to first unread message

Madan Singh

unread,
Mar 13, 2013, 2:39:14 AM3/13/13
to seleniu...@googlegroups.com
Hi, Tarun,
for that ,
You have to follow these step in beginning,
1. first u have to start from recording and the go to file and export
test case as C# /NUNIT/Webdiriver.
2. Go to Visual Studio and Create a class library
3. Copy the exported code in that class library(dll).
4. And add the reference for the following dlls
like as
1.nunit.core.dll
2. nunit.mocks.dll
3. nunit.framework.dll
4. Selenium.WebDriverBackedSelenium.dll
5. ThoughtWorks.Selenium.Core.dll
6.WebDriver.Support.dll


On Wed, Mar 13, 2013 at 11:40 AM, Tarun Gupta <gupta.t...@gmail.com> wrote:
> Hi,
>
> I am an automation tester and I have been working with HP Quick Test
> Professional till now. My company wants me to learn to Selenium with C#. I
> have downloaded the following files from Selenium's website:
>
> selenium-ide-1.10.0.xpi
> selenium-server-standalone-2.31.0.jar
> selenium-dotnet-2.31.0.zip
> NUnit-2.6.2.msi
> NUnit-2.6.2.zip
>
> In addition to these I have Visual Studio 2008 installed on my system. I
> have some basic knowledge of C# as I have developed some small web
> applications using it.
>
> Can anyone please guide me on how to use Selenium with C#. I have explored
> the basic record and play features of the Selenium IDE in Firefox.
>
> Regards
> Tarun Gupta
>
> --
> You received this message because you are subscribed to the Google Groups
> "Selenium Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to selenium-user...@googlegroups.com.
> To post to this group, send email to seleniu...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/selenium-users/-/3KNZHDFSrJUJ.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
M P Singh
9971360313

Madan Singh

unread,
Mar 13, 2013, 5:13:48 AM3/13/13
to seleniu...@googlegroups.com
Hi Tarun,

continuing from previous mail,

5. Install NUNIT on your machine(Window)
6. Add these file into the bin directory of NUNIT
1. nmock.dll
2. WebDriver.pdb
3. Selenium.WebDriverBackedSelenium.pdb
4. ThoughtWorks.Selenium.Core.pdb
5. WebDriver.Support.pdb
7. When u have exported the test cases as C# like as test.cs
8. Goto the Visual Studio and create new class library then Copy the
code which was exported in that class.
9. Now build this class to create DLL
10. Go to the NUNIT and create new project and goto Project and ADD Assembly.
11. Run test.





On Wed, Mar 13, 2013 at 12:41 PM, Tarun Gupta <gupta.t...@gmail.com> wrote:
> Hi Madan,
>
> Thanks for your reply. I am able to create a test by recording and then
> exporting it as C# /NUNIT/Webdiriver. A new .cs file gets created.
>
> But how do we create a new class library?
> And after creating the class library what do we do next?
>
> Regards
> Tarun Gupta
> https://groups.google.com/d/msg/selenium-users/-/F3r_uT83p8QJ.

Tarun Gupta

unread,
Mar 13, 2013, 6:18:34 AM3/13/13
to seleniu...@googlegroups.com
Also how can we integrate NUnit with Visual Studio 2008?


On Wednesday, March 13, 2013 3:12:08 PM UTC+5:30, Tarun Gupta wrote:
Hi Madan,

Thanks for your reply. I have followed all your steps but right now I am not able to run the test because whenever the test launches a new Firefox window, its proxy settings are are changed and because of that it is not able to open the website. Is there a way to pause the test so that I can change the settings?

Also I have two more questions:

1. In this example we recorded the test using Selenium's IDE and then exported it into C# code. Do we usually create scripts this way or do we write tests in C# itself.

2. NUnit is still not clear to me. Can anyone explain why we need NUnit, we can directly run the tests using Selenium's IDE also?

Tarun Gupta

Arran

unread,
Mar 13, 2013, 7:00:10 AM3/13/13
to seleniu...@googlegroups.com
Well I think you should take a step back and learn C# and unit testing first. This will give you the background you need to understand how it fits together.

So currently you are recording the steps in the IDE and exporting them. This is a good idea to get things up and running but in the long term it is doomed for failure! You are absolutely right, in that coding these tests should be done manually, using the native C# WebDriver API. Why? Not everything can be converted directly from the IDE to the C# API, more 'advanced' things will need to be done using the C# API anyway and the code the IDE spits out isn't great, you need to use your own logic and structure to have reliable, logical, readible and extensible tests that can be used for the future!

As for NUnit in particular, it is commonly used for unit testing. Very very briefly, unit testing is testing a particular class in complete isolation. So let's say I have an AdvertGenerator and it's purpose is to give me adverts (added in the database) that can be displayed on my website, but it is also supposed to allow me to give each 'advert' a weight

For instance, if I have an advert with a higher "weight" than another, the AdvertGenerator should be more likely to give me that advert to display on the website, or at least it should be return that one more often!

So I put this functionality into my website and it works, great. How do I tell it will always work and that if I break it, how will I know at the earliest notice? Unit testing.

Using something like NUnit, I can set up a test whereby I give the AdvertGenerator a few random dummy adverts that I create in memory, give them each a different 'weight' and then ensure it returns the correct ones, in order. That is unit testing. I'm giving my class a particular set of scenarios, and I'm asking it to do some work with them, then I'm checking what happened. I'm verifying my functionality is still working. So let's say I break it, I don't know how, but I change something. my unit tests will tell me it's broken. You can then combine these tests with a continuous integration solution, and have them run every night so that you can a constant reminder of what is working in your system.

NUnit comes to play with Selenium for mainly the reason it can be used to run tests. You don't have to use NUnit, you can use XUnit, MSTest (built into Visual Studio). It is mainly because NUnit is free and very very extensible. For instance what if you wanted to have a custom attribute to allows you to 'ignore' certain tests for certain browsers. I have no idea on earth how you'd achieve this with MSTest, but with NUnit, it will allow you to hook into each individual test run and allow you to manipulate it on the fly. So you can have this custom attribute, have it check on each run what browser is being run, if it's one in the 'ignore list' for that test, you can ignore that test programatically. 

NUnit is also very good to allow you to hook into other continuous integration solutions, like I talked about earlier. So you've already seen you can have unit tests run every night to verify the underlying logic to your website is working, what about having tests to verify your UI on your website is also working OK? For instance I can verify that my advert is returned by my AdvertGenerator in the order I expect, but how can I verify the actual advert is shown on the page? That is where Selenium comes in, I can create a test for that, use NUnit to run it, use TeamCity or Jenkins (Continuous Integration software) and have it run every night. That is every night, I'm checking both my logic and my UI are working!

In terms of your proxy problem, you'll need get a little down and dirty with the C# API. This is where the IDE falls behind, you'll have to do this manually in code.

The code for it would be something like (you'll need to look into the documentation for the 'Proxy' class to set your correct settings, I just set some test data below):

            var proxySettings = new Proxy { HttpProxy = "test" };
            var firefoxProfile = new FirefoxProfile();
            firefoxProfile.SetProxyPreferences(proxySettings);
            var driver = new FirefoxDriver(firefoxProfile);

Below are the docs to the C# API for Selenium, read it, use it :)


On Wednesday, 13 March 2013 09:42:08 UTC, Tarun Gupta wrote:
Hi Madan,

Thanks for your reply. I have followed all your steps but right now I am not able to run the test because whenever the test launches a new Firefox window, its proxy settings are are changed and because of that it is not able to open the website. Is there a way to pause the test so that I can change the settings?

Also I have two more questions:

1. In this example we recorded the test using Selenium's IDE and then exported it into C# code. Do we usually create scripts this way or do we write tests in C# itself.

2. NUnit is still not clear to me. Can anyone explain why we need NUnit, we can directly run the tests using Selenium's IDE also?

Tarun Gupta

Tarun Gupta

unread,
Mar 13, 2013, 7:16:43 AM3/13/13
to seleniu...@googlegroups.com
I need to set my Proxy to Manual. Could you tell me how to do this using Proxy() constructor? Can we use the ProxyKind enumeration?

Arran

unread,
Mar 13, 2013, 10:44:09 AM3/13/13
to seleniu...@googlegroups.com
Should be able to, I haven't tested it though since I don't use a proxy, yes, use the Manual Proxy Kind and give the 'HttpProxy' property the URL of the proxy server.

Tarun Gupta

unread,
Mar 14, 2013, 6:09:02 AM3/14/13
to seleniu...@googlegroups.com
Thanks Arran I was able to change the proxy settings of Firefox browser programatically. Here is the code for changing the proxy settings:

FirefoxProfile profile = new FirefoxProfile();

            String PROXY = "x.x.x.x";

            OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();

            proxy.HttpProxy = PROXY;

            proxy.FtpProxy = PROXY;

            proxy.SslProxy = PROXY;

            profile.SetProxyPreferences(proxy);

            driver = new FirefoxDriver(profile);

Madan Singh

unread,
Mar 28, 2013, 8:22:37 AM3/28/13
to seleniu...@googlegroups.com
Can any body help me, there is a login page on my application here a
popup is appearing in some situation not regularly, Now I want to
handle this situation by C# and Web driver and want to perform
further action, any help is appreciated.
1. If popup alert is present then click on OK and continue to step A.
2. If If popup alert is not present then also continue to step A
> https://groups.google.com/d/msg/selenium-users/-/RflFYixesJwJ.
Reply all
Reply to author
Forward
0 new messages