Why Ruby is the Best Scripting Language for End-to-End Test Automation?
Ruby is an excellent all-around scripting language that pioneered web test automation and is a “most-in-demand” skill.
As a veteran software engineer and test automation engineer/SDET, I fully understand how sensitive when software engineers come to language debate. If you are an engineer fixated on a specific language, don’t read this.
The reason for this article: a frustrated Cypress tester (many pieces of evidence show Cypress.io is dying) questioned my advice of “Migrate Cypress Tests to Selenium”. He said he tried Selenium + JS before. Because I knew nothing about this person (i.e. his skills or understanding of test automation), I provided safe advice: “You should use raw Selenium WebDriver + RSpec, i.e., in Ruby language”.
Table of Contents:
· 1. My Objectiveness to Programming Languages
· 2. Selenium WebDriver includes the “unfamiliar” Ruby for a good reason
· 3. Ruby pioneered web test automation
· 4. Ruby is designed with features that good for test automation
· 5. Ruby is “elegant, expressive, and tersely powerful” and “ideal”
· 6. A good Software Engineer should learn Ruby
· 7. “Ruby is the most in-demand skill”
· 8. Ruby is easy to learn
· 9. Rake, the standard Ruby build tool, is also in the Ruby language
· 10. Ruby, on daily testing use, is faster than Java and C#
· 11. Ruby has mature and high-quality libraries and a good community
· 12. Ruby and its Gems support Advanced Testing
∘ 1. I built models for an insurance platform
∘ 2. I built fake dependent external services
· 13. Ruby is delightful to use!
1. My Objectiveness to Programming Languages
If a software engineer only knows one programming language well, of course, his/her opinion on language choices is biased. I am probably more objective than most, as My Selenium Recipes Book series covers all of Selenium WebDriver’s 5 official programming languages.
I have written thousands of end-to-end tests in all these 5 languages, most in Ruby.
2. Selenium WebDriver includes the “unfamiliar” Ruby for a good reason
According to IEEE’s Top Programming Languages 2022, Ruby is ranked 12 in terms of Jobs.
Compared to the other four popular ones: Java, Python, C# and JavaScript, The fact the Selenium Team chose Ruby means it is suitable for testing, don’t you agree?
3. Ruby pioneered web test automation
Web Test Automation started with Watir (Web Application Testing in Ruby), which drives Internet Explorer with its OLE, in ~2003. The creators loved Ruby so much that they included the language in the framework name. (Ruby on Rails, a popular web framework, did the same).
RSpec (Ruby), to my knowledge, is the first BDD framework. Mocha (JavaScript) and PyTest (Python) are clones of RSpec.
Cucumber, the first Gherkin-style BDD framework, is also developed in Ruby. SpecFlow (C#) and JBehave (Java) are the clones of Cucumber (Ruby).
4. Ruby is designed with features that good for test automation
Compiled languages such as Java and C# are designed for building business apps, not for testing. Automated Test Scripts Shall be in the Syntax of a Scripting Language, Naturally!
Scripting languages, on the other hand, are designed to execute automation tasks (typically, utility, build automation or test automation). In the words, design with automated testing in mind.
Experienced tester engineers know the importance of parsing/generating requests/responses (especially in API testing) and test data preparation, that’s text processing/manipulation. Regarding text manipulation, I believe that Ruby is in the second place, trailing only behind Perl (although Perl scripts are harder to read).
JavaScript is a scripting language used to develop web pages. In other words, it is developer-focused. The main reason that so many (failed) JavaScript automation frameworks: developers try to use the coding language for testing.
For more, check out my other article, Why JavaScript Is Not a Suitable Language for Real Web Test Automation?
Python is a good scripting language, which became popular because of AI/Machine Learning. For testing, its indentation rule is no good.
5. Ruby is “elegant, expressive, and tersely powerful” and “ideal”
Authors of “Agile Testing”, a classic testing book, used the above adjectives to describe Ruby.
Here are some examples.
## COOL Date operations
sign_up_page.enter_dob( 17.years.ago.strftime('%Y-%m-%d' )
#...
report_page.enter_start_date(Date.today.beginning_of_week.to_s)
#...
## Remove uses of parentheses
driver.find_element("text").send_keys "ABC"
## Intuitive looping
3.times { driver.find_element("button").send_keys(:tab) }
6. A good Software Engineer should learn Ruby
Test Automation is a skill not taught at Universities, i.e., need to learn it.
Many automated testers came from programming backgrounds, they tend to use a coder’s thinking and mindset to script end-to-end tests. Of course, this often leads to failures.
If you are a Java or JavaScript programmer who is working in test automation and feeling frustrated, I recommend learning test automation from scratch with Ruby.
It is really about changing mindset and learning attitude.
I remember when first seeing Ruby, I didn’t like it when seeing @instance_varaible
. This is because my mind was not willing to learn and judged a new thing with my old and comfortable thinking. I am so glad that I persisted and once I dropped background thinking ‘this should be done this day in Java/JavaScript”, I appreciated, liked and then loved Ruby quickly. After that process, I think my learning ability improved as well.
7. “Ruby is the most in-demand skill”
According to Hired’s 2023 State of Software Engineers Report, “Ruby on Rails and Ruby are the top two most in-demand skills”.
While Ruby jobs, compared to other languages, are not many, according to Hired (a leading hiring marketplace company), Ruby skills will help you to get interviews, which really matters.
“The most in-demand skill was Ruby on Rails, with engineers skilled in this framework and scripting language receiving 1.64x more interview requests compared to the marketplace average. Ruby on Rails bumped Go from first place in 2022’s report to fourth this year.” — Hired
8. Ruby is easy to learn
My daughter learned programming in Ruby at the age of 12, using this book (from the very beginning).
I have trained many manual testers and business analysts on test automation. They all find the syntax (Ruby) easy and intuitive. Below are messages from a manual tester after the half-day hands-on training.
Please note the time, she really liked the training and was excited about it.
9. Rake, the standard Ruby build tool, is also in the Ruby language
Rake (Ruby’s make) is a build tool for Ruby, it is like Ant for Java. Junior test automation engineers might not realize the value of a build tool. When you have a large number of test scripts, how to execute them becomes a task.
Some will say, “We will run in a CI/CD pipeline”. I call it Continuous Testing (CT) when comes to end-to-end test execution. Executing automated end-to-end tests is far more challenging than unit/integration tests, for two simple reasons:
much slower
more brittle, i.e., high maintenance
What does a CT process typically do? It invokes a build target to select which test scripts to run, where to output the results, …, etc.
As Rakefile (equivalent to build.xml for Ant) itself is in Ruby language, it not only makes learning easier but also can use a wide range of libraries directly.
10. Ruby, on daily testing use, is faster than Java and C#
A Java/C# developer would say “Haha, Zhimin, you are wrong. Ruby is much slower a Java on execution speed”.
Scripting languages are much slower than compiled languages by nature. But don’t forget the context: end-to-end testing. The majority of execution time is on the app itself. For example, if a payment step takes 10 seconds and an execution time of 10.5 in Ruby and 10.02 in Java, makes little difference. (check out Performance Comparison: Selenium Ruby, Python, and JavaScript)
More importantly, when developing and debugging an end-to-end test script, we need to run it many times. The start-up time in scripting languages is much faster than the compiled languages, which need to compile and start up a JVM. So, I say, Ruby when used in end-to-end testing, feels faster. This is not the fault of Java/C#, they are not built for this.
Update 2023–07–26, My daughter did a benchmark comparing Selenium Ruby and C# for the same test case. C#: 11.8 seconds, Ruby: 8.5 seconds ✅.
Besides, Ruby’s performance is greatly improved in recent years, thanks to language enhancement itself (Ruby 3) and much faster hard drives (scripting languages read many small files). Github is developed in Ruby, do you find it slow?
11. Ruby has mature and high-quality libraries and a good community
Ruby's library is called Gems. When I first started developing my first personal web app, I was amazed at the quality of Ruby libraries. Nearly all well-known libraries come with a comprehensive of unit tests.
I remember one former colleague’s blog (not exist any more): he joined ThoughtWorks as a senior Java programmer. He felt compulsory to learn Ruby, as the senior ones there often talk about Ruby. You can get a feel from this Martin Fowler’s article, Ruby at Thoughtworks.
My point here is that many Ruby libraries were created by top programmers at that time.
Not only that, the library dependency is managed very well in Ruby. It strikes the right balance, not as complex as Maven, and not as time-consuming as Node.js.
Besides, Ruby programmers are proud of the Ruby community. If I have a problem, there are always nice people to help. 👍
12. Ruby and its Gems support Advanced Testing
A motivated test automation engineer can learn raw Selenium + RSpec quickly and can handle most of the work needs in a matter of days. There are challenging situations, in which many just give up, but only Ruby offers viable solutions. Here I will list two.
1. I built models for an insurance platform
The app’s UI is not a true web app, rather, a custom Windows native runs only in Internet Explorer. Software Testers might remember HP’s Quality Center (formerly Mercury Interactive’s Test Director, now a part of Micro Focus’ ALM) only runs IE on Windows.
“QC client is a Microsoft technology. It cannot be run without IE installed on a system.” — Micro Focus’ web site (a discussion)
API Testing via SOAP is the only possibility for test automation. The previous attempt with ReadyAPI (a paid version of SoapUI) failed.
I came up with a solution by building models using the fantastic ActiveRecord library.
class Policy < ActiveRecord
belongs_to :client
def get(policy_id)
# invoke soap webservice to retrive
end
def save
# invoke soap webservice to save
end
end
The resulting test script is like below:
client = Client.new(age: 17)
client.save
client_id = client.id
the_client = Client.get(client_id)
the_client.gender = "F"
the_client.save
#...
Manual testers (who all started using this) loved it.
2. I built fake dependent external services
When we test complex enterprise apps, which often have external dependencies, such as payment gateway.
Once for a serious (not mission-critical, quite serious) app, the testing team spent too much to create data scenarios, and still many were unable to test. Then I shared my approach with the testing team, who were very thankful.
What did I do? I implemented the fake server to mimic the dependent behaviours. (not sure what fakes means, check out my article, Fakes vs (Stubs & Mocks) in Software Integration Testing).
Some will be surprised, “Are you crazy? implementing a third-party server?”. Yes, I did it a number of times for different projects. Of course, as I said, this is beyond normal end-to-end testing. Also, I consider myself a good programmer (won an international programming award).
But the real reason: it is much easier to do in Ruby than you think, thanks to the great Ruby on Rails framework. I was considered a 10X Java programmer back in 2006 (after working with Java for 10 years), but I wouldn’t dare to do one with Java.
The fake systems I implemented not only greatly assisted testing, but also helped developers a lot. I remembered, after leaving the project, I received an email from a maintenance programmer (who was in the team as the developer). He wanted me help to restore the fake third-party server, why? In his email, “Using the real test server to complete a whole workflow takes about 15 minutes; using yours, only 2 minutes. This saves big time on development.”
There are more examples like the above two. When others said “Testing XXX is not possible”, from my experience, many of those could be done and done well by using the power of Ruby.
13. Ruby is delightful to use!
Finally, after the above facts and experts’ (four co-authors of Agile Manifesto) opinions. Here is my added opinion: “Ruby is delightful to use!”, this is after I have professionally (getting paid) used the following languages: C, Java, Perl, Python, C#, C++, JavaScript, Perl and Ruby, over the last 27 years.
“Ruby is a wonderfully powerful and useful language” — Martin Fowler
“programming in any language other than ruby will feel like you’re pushing rope.” — Mike Clark, author of “Pragmatic Project Automation”“Ruby is smart, elegant, and fun” — James Britt
“Ruby is one of those great languages that takes an afternoon to start using, and years (maybe a lifetime) to master. In C, I’m always having to work around the limitations of the language; in Ruby, I’m always discovering a neater, cleaner, more efficient way to do things. ” –Ben Giddings
“It is not easy to actually do things unless you are using a great language with strength in quick and clean prototyping. In my case, the language is Ruby! Thank you!” — Michael Newmannin the famous “Programming Ruby” book by Dave Thomas, Chad Fowler and Andy Hunt (two of them are coauthors of Agile Manifesto”
I still remember, after using Ruby for a month in my spare time, one day when I was coding (at that time, I was a Java tech lead, contractor), I surprisingly realized that I was thinking of Ruby first, then converted to Java! I have programmed Java and JavaScript for about 10 years! The “thinking in Ruby” became more and more obvious, and soon I realized that I couldn’t do much Java/JavaScript work for long. Maybe a year later, I switched my day work from Programming to Automated Testing, using Ruby at work. Never looked back. I still love programming, but do it at home for developing my own apps, most of them in Ruby, too.
Related reading:
My Books:
- Practical Web Test Automation with Selenium WebDriver
- Learn Ruby Programming by Examples
- API Testing Recipes in RubyMy daughter’s “Discover Ruby Programming Through Fun Examples” interactive course on Educative