- February 14, 2016
- Posted by: InApp
- Category: Testing
Before we come to the Selenium Automation testing commands, let me relate to you an interesting anecdote on the christening of Selenium. Jason Huggins (the creator of Selenium) worked at ThoughtWorks. One day he got really irked with their Competitor “Mercury” (owned by HP). In his frustration, Jason wrote a mail to his colleagues at ThoughtWorks. The mail said that selenium supplements could cure mercury poisoning. Ever since that day the name Selenium got associated with this product.
Since the inception of Selenium 2.0 (commonly referred to as Selenium Webdriver) in 2009, test engineers around the world swear by it. Selenium is the most popular testing framework for web applications. If you are budding testers or a developer looking for some common Selenium Automation testing commands to make your life easier while doing automation, you have come to the right place.
Below are the 10 most common Selenium Automation testing commands:
1. Open a browser – Web applications are the flavor of the season. Why?
Coz, they are running on browsers! Duh!
The web application is essentially an application running on a browser. The browser’s UI is actually working like the client side.
So, the most basic thing for a budding automation tester working on a web application is to simulate opening a browser.
Here is the command for it:
Driver = new <browser name>Driver();
2. Navigate to the web application –As mentioned earlier, if you compare the web application architecture to the client-server architecture, the web application’s client is actually the browser’s UI. In order to access the application, one needs to query the server, which is done by typing in URL of the server.
In order to automate this scenario via Selenium, you have a couple of options:
Option 1 – driver.navigate().to(“url”)
Option 2 – driver.get(“url”)
3. Maximize the browser window – Testing is really about exploring the real life user interaction with any application. One of the most common user behaviors is to maximize the browser before using any application.
The command to execute this action is:
4. Close the browser – The most basic actions are opening and closing the browser. We already saw how to open a browser, now let’s see how we can close the browser using a selenium code, automatically;
There 2 options for closing the browser:
driver.close() (will close the current window in focus)
driver.quit() (will close all windows, including child windows and safely end.
5. Switching frames: how does the application handle a pop up window (child window)? This is critical part of testing the application. Below is the command for this:
6. Finding an Element: Another frequently used Selenium command is to find an element on the UI. The element can be easily found on the basis of a unique identifier. The unique identified could be “element id” or “name.” Selenium provides many options for finding element (ClassName, cssSelector, id, LinkTest, Name, partialLinkTest, tagName, xpath)
Out of the above, cssSelector and xpath are the most frequently used and here is the syntax for using them:
7. Writing into a Text Box- Webpages frequently contains forms with text boxes. So a basic test would include commands to write onto a text box. In order to do so the existing text needs to be cleared and new text inserted using the following commands:
8. Count of element- One use of selenium based automation could be running a set of scenarios on a group of element on a UI. In this case it would be helpful to have a count of elements which are to be subjected to the automation scenarios. Below is a command to count the number of element:
iCount = driver.findElements(By.xpath(“xxx”)).size());
Waits – In most modern web applications, elements load at different times. This brings in a complexity to selenium based automation testing. This is solved by using “wait” commands:
9. Implicit wait, means waiting for a specific period of time –
It is a quick and easy way to handle the problem of elements loading at different times on a web application. It is applicable at a global level and affects all the elements. It tells selenium to wait for an exact period of time before it throws a “No Such Element” Exception.
10. Explicit Waits, means waiting for a condition-
Explicit wait will tell Selenium web driver to wait for a condition to occur.
Best Practice while running automation tests – Using Tear Down techniques:
It is generally a good practice to clean up the database after each automation run. This is important because after each run the size of data in the database increases. This could slow down the application if it has to deal with huge amount of data, over a period in time.
Thank you for reading 10 Common Selenium Automation Testing Commands for Newbies.