Selenium WebDriver is the Easiest-to-Learn Web Test Automation Framework

Step by Step showing how to learn to write raw Selenium WebDriver test scripts in minutes

Zhimin Zhan
11 min readFeb 4, 2021

Selenium WebDriver is not only the most powerful but also the easiest-to-learn web test automation framework. In this article, I will show you how to write a working Selenium WebDriver test in minutes. Yes, you heard me right. There will be no smoke and mirrors, only raw Selenium WebDriver and the way a test automation expert would use.

If you follow the steps in the article, you will get your first Selenium test running in minutes. Not evil record-n-playback, proper scripting raw Selenium, the same way that is used by the experts.

Many fake test automation engineers and ‘experts’ are wrong on saying that Selenium is difficult to learn. In fact, Selenium WebDriver is very easy to learn, far easier than others because its core syntax follows two intuitive patterns:

  • Find a web element, and drive it
  • Find a web element and retrieve its data for verification

Because it is so easy, I only spent a few minutes covering Selenium WebDriver syntax in my one-day Web Test Automation with Selenium Driver training, and the participants grasped it quickly with exercises. (the main topic of my training is to develop selenium tests efficiently and refactor them to be maintainable).

Seeing is believing. Here I will show how to create a user login Selenium test, step by step from scratch. I recommend you follow the steps and do this exercise on your own computer.

Everything I show below is free (freedom and pricing), you don’t have to pay a cent. The test scripts are in raw Selenium WebDriver syntax.

Prerequisite:

(installation is simple, just accept the default options). 🕒 ~30 seconds

  • basic knowledge of HTML
  • latest Chrome browser (v88 as the time of writing)

Here we go.

1. (🕒 ~30 seconds) Create a new test project in TestWise.

In TestWise, click the menu “File”-> “New Project”. Specify the location (a new empty folder, e.g. c:\demo) and the target server URL https://travel.agileway.net .

2. (🕒 ~25 seconds) Open new_spec.rb, right-click a line after “it … do” and select the first run menu item (see the above animated GIF).

You will see a Chrome browser starts up and opens your target site.

3. (🕒 ~15 seconds) Right-click the ‘User Name’ text field in the browser, select “Inspect”.

On the right pane, you shall see the element (in HTML) is highlighted. Double click to select the value of ID, which is used to identify the element. Copy it to the clipboard (Ctrl+C).

4. (🕒 ~15 seconds) In TestWise Editor, clear the commented lines. Enter the selenium test statement by using snippet (a common feature in code editors): “dfei” (means: Driver Find Element by ID) followed by a Tab key.

The driving of a text field is to send_keys to it. For a button, it will be click. The sequence of key actions are: dfei, Tab, “username” (from paste), Tab, .sk, Tab, “agileway”, Tab and Enter.

driver.find_element(:id, "username").send_keys("agileway")

5. (🕒 ~15 seconds) Select this step line, right-click to select ‘Run Selected Scripts Against Current Browser

You will see that only this step (entering the text ‘agileway’ in the user name text box) will be executed against the browser. You might notice the file name of the current editor is “debugging_spec.rb”. This special test file contains helper methods that connect your test scripts to the current Chrome browser. This will make trying/debugging a lot easier.

6. (🕒 ~45 seconds) Complete the login steps (‘enter the password’ and ‘click sign in’) by using two snippets dfen and dfex (by name & by XPath, different ways to locate a web element in a web page).

The //input[@value='Sign in'] is an XPath expression, type it in.

Clear the ‘agileway’ text in the browser. Run this test (in the debugging_spec.rb). You can see a successful login operation.

7. (🕒 ~30 seconds) Copy those three Selenium test steps back to your test script (new_spec.rb)

it "User Login successfully" do
driver.find_element(:id, "username").send_keys("agileway")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:xpath, "//input[@value='Sign in']").click
end

Rename the test case name to “User Login successfully” (done as the above), and the test file name “new_spec.rb” to “login_spec.rb”. Run the test again, you shall see a successful login in a new Chrome window.

New to Selenium testers might not remember find_element syntax.

You may use TestWise’s built-in library feature to look it up.

8. (🕒 ~45 seconds) Add Assertion

The above is not a complete test yet as there is no assertion. Add one using the exp snippet. Please note, this is the second form of using Selenium: rather than driving a web element, you retrieve its data for verification.

it "User Login successfully" do
driver.find_element(:id, "username").send_keys("agileway")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:xpath, "//input[@value='Sign in']").click
expect(driver.find_element(:id, "flash_notice").text).to eq("Signed in!")
end

The bold line is to verify text “Sign in!” is on the page (after sign-in), type it in. Then rerun the test.

Here is the video (4:08).

How long doe do it take? About 6 minutes at my slower instructing speed. For the first-timer, it surely will take longer to cater for typing mistakes, wrong operations, …, etc. Don’t worry about it. Firstly, to get it right. Then do it a few more times, and you will find it will be more natural, more efficient, and fun!

Easy, right? I have been writing automated test scripts for over 14 years, these are the steps I have been performing on a daily basis. Of course, there is more to Selenium WebDriver syntax. Let’s look at two examples.

  • Locate multiple elements
driver.find_elements(:link_text, "Next")[1].click 
# the above clicks 2nd 'Next' link
  • Advanced User Interactions
elem = driver.find_element(:id, "agileway_software") driver.action.move_to(elem, 190, 30).click.perform

You will probably understand them, won’t you?

To know more about how to use Selenium WebDriver, please refer to my book: “Selenium WebDriver Recipes in Ruby”, which contains over 200 ready-to-use selenium examples.

Getting test scripts working is not good enough, we need to make the test scripts maintainable. Test maintenance accounts for 70%+ of the work. Therefore, developing a new test is a minor effort.

For example, the login steps need to be refactored into a helper function or into a page model.

For more on writing easy-to-maintain automated test scripts, please refer to my book “Practical Web Test Automation

If you prefer learning in an interactive approach, you may attend my one-day Web Test Automation with Selenium Driver training.

Usually, during my training, attendants were amazed at how easy and intuitive Selenium WebDriver is! They asked me questions. I will share some of my answers here.

1. “It can’t be this simple. My company have failed test automation again and again for over 10 years.

I never said mastering test automation is easy, I meant “learning”, in a practically useful manner. Most of the participants in my training could work on real tests the next day. Will they encounter problems along the way? surely they will. That’s why coaching/mentoring is important. For example, a tester may ask programmers’ help on XPath or ruby scripting in general.

2. “You created TestWise. Are you trying to sell a product?

Whether purchasing a TestWise license (US$30 per month) or not is totally up to you. TestWise comes with free mode with only a limit on test executions. Restarting TestWise (very quick) will reset the count. You may use TestWise this way, free of charge, forever.

The test scripts created in TestWise is raw Selenium WebDriver test in plain text, you may edit them in NotePad, and run them from the command line. There is absolutely no locking here. If you have a preferred tool such as Visual Studio Code, use it efficiently, as you need high efficiency when maintaining a large number of automated tests.

3. “You showed a simple case. With XPath, it is complicated, so Selenium is hard to learn.

That’s not true. I showed an easy-to-understand case. After scripting for 14 years, I performed the above steps the same way. For complicated scenarios, XPath may be used. But XPath and WebDriver are two different things. You cannot mix them. Other testing frameworks might use XPath as well.

4. “Our company tried Selenium (Java) and failed. I followed your exercises on my machine, it was so easy. How could the differences be so big?

For a start, using Java for functional test scripts is wrong, as it is not a scripting language. This shows that whoever (commonly a Java tech lead) suggested using Java knew nothing about automated functional testing. A messy outcome is not surprising.

Well, TestWise makes test automation easy, and so does Ruby. But as you can see, some TestWise features can be implemented in other tools as well. My approach (which is very practical and simple) is based on the knowledge and experience I have acquired over 14 years of hands-on working on automated test scripts. There is no magic about my way, I do adjust my approaches based on the feedback of testing results. Many years ago, I tried Java with Selenium naturally as I was a Java programmer for 8 years (since v1.0.2). After I found that it was not good for test automation, I switched to Ruby. Simple!

5. “Have you compared efficiency by using Selenium against other testing tools/frameworks?

It will be hard to answer. I can only give a couple of references. On two occasions, I was assigned to work on a set of automating existing User Acceptance tests, for a proof of concept. The estimated duration was 3 months (commonly, POC was set 1 or 3 months, for easier accounting, I guess). I managed to get most of the work done in 3 days, in the same way as I just showed you.

I am not suggesting that I am 30x more productive here. Every project is different. I used BuildWise (CT server, another topic a good test automation engineer shall master) to assist test executions as well. But wherever I went, people were amazed by my efficiency. The secret is to use raw Selenium WebDriver (with Ruby).

Once, a junior Micro Focus UFT test automation engineer showed me a test case that took her about 2 weeks. I told her that I could accomplish it within 20 minutes in TestWise. Of course, she was in doubt. Then I added, “Actually, you would be the operator, just to follow my instructions.” We did it in 18 minutes. To be fair though, she spent quite a bit of time with test design and test data. Still, she was deeply impressed with Ruby and TestWise, both of which she had never used before. She said: “Not just about the efficiency, the test scripts are more concise, and so much easier to understand”.

6. “I find creating some tests with a record-n-playback is easy, but find it hard with Selenium

Record-n-playback is wrong, most will agree. HT QTP (now Mirco Focus UFT) has been doing that way for decades and failed. Now Micro Focus is trying to support Selenium WebDriver. This is a fact.

Now I tell you why it is very very wrong. Recorded test scripts are hard to maintain, which require much more effort than test creation. With a large suite, the mission of maintenance becomes impossible. That’s why so many test automation attempts started OK, then failed badly. A wise executive shall forbid using record-n-playback tools.

Most testers found Selenium hard is because their minds were contaminated with wrong concepts (such as recording, object map, non-scripting-based, GUI tool, .., etc) of test automation. These test engineers only have failed experiences with all sort of testing tools and frameworks. Some of my clients failed with Selenium WebDriver as well, because of the incompetence of tech leads who pretended they knew test automation. Once the decontamination process (getting a real test automation coach) is completed, you will find a much more effective, creative and fun way to do test automation properly.

7. “I want to see Selenium with Gherkin BDD

Don’t mix your learning of Selenium with Gherkin. Selenium works well with all test syntax frameworks such as RSpec, JUnit, Mocha, PyTest and Cucumber. Focus on learning the selenium first.

By the way, Gherkin BDD can work with Selenium, but might not be the right path. Please read my article “Why Gherkin (Cucumber, SpecFlow,…) Always Failed with UI Test Automation?”, which was featured on “The StartUp” and Software Testing Weekly.

Learning Selenium WebDriver is easy, to become a master of test automation (much more than Selenium syntax), like any skill, takes time and dedication. One effective way to speed it up is to get access to a real test automation coach (as you will do for your kid who wants to learn to play tennis).

Finally, I share a recent test execution report of WhenWise (my web app)’s regression test suite (492 in total, all in Selenium WebDriver) in BuildWise CT server.

These 492 Selenium tests enabled us to push updates to production multiple times a day if necessary.

--

--

Zhimin Zhan
Zhimin Zhan

Written by Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.

No responses yet