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 a budding tester 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.
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!
A web application is essentially an application running on a browser. The browser’s UI is actually working on 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 the URL of the server.
In order to automate this scenario via Selenium, you have a couple of options:
Option 1 – driver.navigate().to(“url”)
Or
Option 2 – driver.get(“url”)
3. Maximize the browser window
Testing is really about exploring 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:
driver.manage().window().maximize();
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)
or
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 a critical part of testing the application. Below is the command for this:
webDriver.switchTo().window(handle)
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 uniquely identified could be “element id” or “name.” Selenium provides many options for finding elements (ClassName, cssSelector, id, LinkTest, Name, partialLinkTest, tagName, xpath)
Out of the above, CSS selector and XPath are the most frequently used, and here is the syntax for using them:
driver.findElement(By.Xpath(“”))
driver.findElement(By.cssSelector(selector))
7. Writing into a Text Box
Webpages frequently contain 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:
driver.find_element_by_xpath(xpath).clear()
driver.find_element_by_xpath(xpath).send_keys(“data”)
8. Count of element
One use of Selenium-based automation could be running a set of scenarios on a group of elements on a UI. In this case, it would be helpful to have a count of elements that are to be subjected to the automation scenarios. Below is a command to count the number of elements:
iCount = driver.findElements(By.xpath(“xxx”)).size());
Waits – In most modern web applications, elements load at different times. This brings complexity to selenium-based automation testing. This is solved by using “wait” commands:
9. Implicit wait
Implicit wait means waiting for a specific period of time.
driver.implicitly_wait(55)
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
Explicit Waits means waiting for a condition.
WebDriverWait(driver, 55).until(“condition”)
The explicit wait will tell the Selenium web driver to wait for a condition.
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 a huge amount of data, over a period of time.
Have questions about Automation Testing? Contact the software testing experts at InApp to learn more.