The Resplendent Developer

Software Development and Software Quality Assurance

Your first automated web test!

This is the second post in a series started here.

Now that all the software from this post has been installed it is time to WRITE YOUR FIRST PIECE OF AUTOMATION!

I’m going to work under the assumption that you have installed the IE Dev Toolbar and that we are testing against Internet Explorer. By the way, you need to be connected to the internet to run this test.

Step 1:
Fire up Visual Studio and create a Console project called “SeleniumHelloWorld”.

Step 2:
Right Click on References and add references to each of the DLLs you downloaded the previous post: the Selenium Client Drivers for C#.

Step 3:
Open the Program.cs file and add the following code after the using clauses in your demo project

 using OpenQA.Selenium;
 using OpenQA.Selenium.IE;
 using OpenQA.Selenium.Firefox;
 using OpenQA.Selenium.Support.UI;

This will provide the library support for the Selenium Webtest.

Step 4:
In the Main method enter the following code

 IWebDriver driver = new InternetExplorerDriver();
 driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(30));
 driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
 
 driver.Navigate().GoToUrl("http://www.google.com/");

The first line is the declaration of the interface that will be running your test.
The next two lines are setting timeouts. I find this is helpful when running tests since sometimes Selenium will “jump the gun” when trying to find controls on the page. The first wait, “SetPageLoadTimeout”, changes the wait on the initial navigation. The second wait, “ImplicitlyWait”, sets how long after the page has loaded before the project gives up if it cannot find the control. The last line, starts IE and navigates to google.com.

Step 5:
This step represents 99% of your time spent doing web test automation. Start up Internet Explorer and navigate to google.com. You need to determine the code necessary to enter text into the search text box. Press F12 to start the IE Toolbar. On the Toolbar menu, select “Find” and click “Select Element By Click”. Then move your mouse over the text box and click. At this point the toolbar displays the line in the HTML of the code that represents the TextBox control. You are looking for the tag that looks like

 id="gbqfq"

This serves as the unique identifier we need to provide Selenium a way to find the control. The “gbqfq” is merely a variable name for our purposes. Each control should have an id tag. Not all do, so you may need to find another way to identify the control. Google has been good enough to provide us with one, so we may use it.

Step 6:
Type the following line:

 driver.FindElement(By.Id("gbqfq")).SendKeys("Hello World\n");

The “FindElement” is the workhorse that gets a pointer to the control on the page we want. It takes an argument that tells how to find the control. In our case, we are passing the static object/method “By.Id” and giving the the variablename we found in step 5. The SendKeys is command is simply the “type here”. We provided a carriage return so the routine will behave as if we’d typed in the code and pressed Enter.

Step 7:
Finish up with the following code:

 Console.WriteLine("Press any key to continue...");
 Console.ReadKey();
 driver.Quit();
 return;

This simply displays an instruction to the console window that will be open and waits for keypress. The line driver.Quit() closes the browser.

Now, you should be able to compile and run this code. You will need to be certain that you’ve placed the IE driver server executable in your path so that it will run.

I posted a copy of the code on Github for you to look at here: https://github.com/russellasinclair/SeleniumHelloWorld

You now have the primary foundation to write almost any web automation. In the future, I will write posts on more difficult automation problems.

I’ll be happy to answer questions posted below as time permits!

Comments are closed.