Selenium Simple Example

Given some simple sample barebones HTML to get us started, here is a generic login page:  Username  Password   A couple of the most common basic HTML elements that Selenium may interact with for automation purposes include and. Notice the above elements have id="String" attributes. These are our object references. Selenium WebDriver API uses ID, Class, TagName, HTML Attribute, Innertext, and other properties with which to identify elements on the rendered HTML page by matching attribute values. In the absence of one of these supported Selenium FindBy attributes, it is also possible to use an XPATH query string to traverse the DOM (document object model) of the HTML to find an element in question for automation purposes.

Using Selenium WebDriver API, we can "wrap" HTML to obtain object references and then bring them back for in-memory runtime processing to a general purpose high-level programming language such as Java or C#. Then from within Java or C#, we use the programming syntax, keywords, data structures, and mannerisms to code an automated test that calls the Selenium API, brings back elements, and then commands them to do things, such as click or be clicked, or send text to a control on the screen. How to actually set up an automation architecture will be discussed at a later time.

For the moment, the following is the above HTML rendered within a browser: Here's a simple, barebones script in C#.NET implementing Selenium WebDriver API, to enter some text into the above fields and click the Submit button: using OpenQA.Selenium; namespace SeleniumSandbox {    class Program {        public static IWebDriver browser; static void Main(string[] args) {            // Assume a local reference to an IWebDriver instance exists, called 'browser' // Find an element with the ID attribute = "j_username" // Then send the text "User1"

IWebElement myUsernameElement = browser.FindElement(By.Id("j_username")); myUsernameElement.SendKeys("User1");

// Find an element with the ID attribute = "j_password" // Then send the password text.

IWebElement myPasswordElement = browser.FindElement(By.Id("j_password")); myPasswordElement.SendKeys("ABCxyz");

// Click the Submit button browser.FindElement(By.Id("btnSubmit1")).Click; }    } } Note this is example code, so the above code if copy\pasted into Visual Studio will not run at this time, as the setup for Selenium has not been done. However, for demonstration purposes, the simple code shows at a high level that there are two primary objects for Selenium interaction: the WebDriver object, and the WebElement object.

The WebDriver object (shown here as IWebDriver browser) manages the administrative functions of Selenium, such as instantiating a Selenium-instance browser of Chrome, Internet Explorer, or Firefox, a special process that can be controlled by Selenium during automation runtime.

The WebDriver object does things like launching the browser and navigating to a URL by string, telling the browser to be maximized, obtaining the process ID of the next tab or the next window and making it the current target of automation, and of course terminating the browser window when automation finishes, stuff like that.

The WebElement objects serve to wrap HTML. In the above examples, I am using the IWebElement like a variable, giving it a name, initializing it, calling browser.FindElement(By.Id(String)) and bringing back an element so it can be processed using C# syntax. The string is verbatim matched to the actual value of the ID attribute within the HTML.

Notice also that I am calling SendKeys(string) upon the variables. SendKeys is a WebDriver method that sends simulated keystrokes to the element on screen at runtime. Selenium works very much like simulating UI interactions, it is meant to "drive the browser" and do what a normal manual user could do through the UI, except in an automated manner of course.

To broach an advanced topic briefly, instead of having the method calls on different lines, it is possible to condense the method calls through dot notation, as shown in the final call to click the submit button. Notice I am bringing back an element that matches the ID = "btnSubmit1" and I am calling Click directly on that object (instead of Click on a new line), Click being Selenium API.

In closing, it must be emphasized that automation that is not maintained is abjectly worthless. Automation must be maintained to be of value. The object references on the site will change due to development changes, developers adding new HTML elements, or changing the attributes of existing ones. Tests will break (often when a new build is received into the test environments) when this happens, so one among many of an automation engineer's duties include to fix object references within the automation scripts to get the tests running again, and for this purpose, it is necessary to look into the topics of data-driving the object references through external files, whether they be CSV files, plain flat text files, Excel spreadsheets, or SQL databases, thereby abstracting and de-coupling the test data maintenance to a level that is external to the C# source code level. In this manner the C# code does not need to be recompiled over and over whenever object references change, but updating the database or data-driving spreadsheet and such is then needed. This type of data-driving has been proven to be more efficient than rebuilding the C# code repeatedly.