Selenium functional testing. A brief intro.

Functional testing involves checking that your software does what it is meant to. It is a broad term that covers many things. But from a UI perspective, it has a more specific meaning. Typically, it means smoke testing, integration testing, and regression testing. Nowadays, the complexity of UIs means you really need to automate your testing, so here is our brief intro to Selenium functional testing.

Background

Functional testing does exactly what it says – test functionality. This is distinct from testing to find bugs or performance testing. Functional testing can happen at every level in the system, from unit testing all the way to acceptance testing. At the system and UI level, there are 3 main forms of functional testing:

Smoke testing is used to quickly check that your build is working as expected. As a minimum, you should run a smoke test every time you build your code, even if there were no changes made. Typically, this involves running through the main user flows rather than exhaustively testing everything. 

Regression testing is about verifying that the product still functions properly after the latest changes. This is critical when you have added any new code or made changes to the functionality. It is also necessary if you have made any bug fixes since these have a nasty habit of affecting other pieces of code. This is a much longer process that needs to test every moving part of your software.

Integration testing is testing new functionality that has been integrated into the system. This is sometimes challenging if the new functionality has a big impact. It’s important to do integration testing for new UI features as well as backend and core functionality.

All these can be done manually, but where possible, you should try and automate them. This will ensure tests are run more efficiently and will speed up the velocity of your project delivery.

Selenium functional testing

Selenium is a powerful framework for test automation. It has been about since 2004, making it a very mature system. Selenium uses test scripts in order to verify the functionality of your UI. The script essentially gives the list of commands that should be executed on the UI. The following diagram shows the Selenium architecture.

Selenium functional testing architecture

The test script is passed to the Selenium server. This uses JSON to communicate with the appropriate Web Driver. The Web Driver then passes the command to the actual browser and returns the result. 

How to create test scripts

A test script consists of a series of steps that Selenium will perform on the UI. You start by taking a detailed test plan based on a specific user journey or a known way of triggering a bug. A very simple test plan might look like this:

  1. Go to the Facebook homepage
  2. Locate the email field at the top of the page
  3. Enter “test@testing.com”
  4. Locate the password field next to the email field
  5. Enter “Pass12345”
  6. Locate the login button next to the password field
  7. Press the button

NB most scripts would then go on to check that the correct user page has loaded.

The first thing you need to decide is what language to use for your script. Selenium can cope with C#, Java, JavaScript, PHP, Python, and Ruby. Whichever you choose, it will be converted to Selenese by the server. 

A simple Python example

Let us look at Python as an example. The start of your script is going to initialize things.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

Now you need to look at the first couple of steps of the test plan. First, navigate to the test URL:

browser.get('https://www.facebook.com/')

You now need to identify good element selectors for 3 elements: the email field, the password field, and the login button. You need to find a selector that is unique on the page and won’t change each time the UI reloads. For more details on CSS selectors, you can read our recent blog. Fortunately, the FB login page has quite simple selectors: “email”, “pass”, and “loginbutton”. Start with the email:

user = browser.find_elements_by_css_selector("input[name=email]")
user[0].send_keys(‘test@testing.com’)

Now, find the password field and enter the correct password:

pass = browser.find_elements_by_css_selector("input[name=pass]")
pass[0].send_keys(‘Pass12345’)

Finally, find the login button and click it. 

login = browser.find_elements_by_css_selector("input[name=loginbutton]")
login[0].click()

You could have chosen other selectors for the login button. For instance, “input[type=submit]”. However, if you look at the source of the FB homepage, you will find several other buttons with that type. This means you really need to find a more-unique selector, else a future page change may result in the wrong button being clicked.

Moving it on

Most scripts are much more complex than the above. For these, you will probably need to develop them a few steps at a time, checking at each stage if they are right. In order for Selenium functional testing to work well, you will also need to modify your script so that it works on multiple browsers and devices. For a simple script like the above, this isn’t an issue. But for realistic scripts, you might even end up having to rewrite most of the code. The upshot is, even skilled developers take many hours to create a new test. Of course, if all this sounds a bit daunting, you can always use Functionize’s intelligent test agent to help you. This takes your test plan and converts it into a fully functional test that just works.

Sign Up Today

The Functionize platform is powered by our Adaptive Event Analysis™ technology which incorporates self-learning algorithms and machine learning in a cloud-based solution.