Why I Prefer Ruby As E2E Test Automation Scripting Language? Part 2
More suitable for E2E testing
This article is featured in the Software Testing Weekly #260.
In Part 1, I explained some of Ruby’s great features in general scripting. In this article, I will go a bit deeper in the context of E2E Test Scripting.
7. Dynamically locating file
In compiled languages such as Java, referring to relative files is not straightforward, including it in CLASSPATH. This might be fine for coding. But for E2E testing, referring to test data (files) is rather common, the dynamic-ness of scripting language is clearly advantaged here.
8. Less restrictive on single-quoted string
In compiled languages such as Java, there is a distinct difference between single-quoted (character ‘a’) and double-quoted strings (string “a”).
In E2E testing, QA engineers rarely need to worry about the single character data type, because our end-users don’t care. While there are still differences between ''
and ""
in Ruby, at many cases, we can use it freely, such as send_keys('testwise')
or send_keys("testwisely)"
.
9. Ruby Mixins
Besides inheritance (in OO), Ruby supports Mixin to enhance code reuse. Again, Ruby makes it easy to use in E2E test scripts. For example, nearly every one of my father’s E2E test scripts has this statement:
include TestHelper
Which loads in the reusable functions defined in test_helper.rb
.
10. Ruby Block
For QA engineers, Ruby Blocks might be too deep of knowledge. My father invented an effective yet simple approach to handling waiting (common in web test automation) using this feature.
For more, check out “My Innovative Solution to Test Automation: Easy Wait in Web Automation Scripts”.
11. Ruby does not have some annoying features.
We often see irrelevant features in some E2E test scripts, such as:
await
,async
Promises in JavaScript
The above makes sense in coding but is irrelevant in E2E testing. Somehow, developers have injected these concepts into E2E test automation.
Here is an example, a ‘good version’ from a Cypress expert:
it('adds numbers via aliases', () => {
cy.visit('public/index.html')
cy.get('[name=a]').invoke('val').then(parseInt).as('a')
cy.get('[name=b]').invoke('val').then(parseInt).as('b')
cy.get('#result')
.invoke('text')
.then(parseInt)
.as('result')
.then(function () {
expect(this.a + this.b).to.eq(this.result)
})
})
It even used:
String literals as local variables, referred as an ‘alias’
Pass function name
parseInt
as parameter
The equivalent version of Selenium Ruby is much simpler and intuitive.
it "adds numbers" do
driver.get("public/index.html")
a = driver.find_element(:name, "a")["value"].to_i
b = driver.find_element(:name, "b")["value"].to_i
result = driver.find_element(:id, "result").text.to_i
expect(a + b).to eq(result)
end
Besides, node_modules
is quite annoying, totally unnecessary for E2E testing (there is no need, performance or otherwise, to have a local cache of dependent libraries).
Some might say, “You are targeting JavaScript”. Not particularly, Python, a language I am quite familiar with, has a strict indenting rule which is also not good for E2E testing.
Related reading: