05: Multi-Login Tests (RSpec Test Syntax Framework)
Learn the RSpec syntax framework in minutes.
Learning Objectives
Understand Test Syntax Frameworks
Learn RSpec Structure
before(:all), before(:each), after(:each), after(:all)
in RSpecNavigate to a URL in Selenium,
driver.get
Execute all test cases in a test script file
Test Case 05
Test Design
There will be three test cases. We can build from the test script file from the previous exercise.
Update to use an invalid username.
Remove the assertion.
Change the test case to ‘Login 1’ or alike
Copy-n-paste to create two other test cases “Login 2” and “Login 3”.
Essentially, there are three similar test cases, for each test case:
Navigate to the login URL,
https://travel.agileway.net/login
Enter username, password, and click the sign-in button.
make up a username or password, as we don’t care about the results.No need to perform assertions (as specified)
Knowledge Point: Test Automation Scripts use two frameworks: Automation + Test Syntax
Selenium WebDriver is an Automation (also known as Driver) framework, which drives the app.
What is a test syntax framework then? Let’s have a look at the test script we have done in the previous session.
There are three driving statements (indicated by arrows), the rest are in the scope of the test syntax framework.
A Test Syntax Framework provides:
Syntax Structure
such asit "a test case" do
marks the start of one test case.Assertions
A test is not complete without checks (a.k.a assertions).expect(...).to include(“…”)
is an assertionTest reporting
Capture the test results.
The test syntax framework you have seen is RSpec. RSpec is one of the most popular test syntax frameworks, it is also classified as a Behaviour Driven Development (BDD) framework.
Special Mention: Avoid Cucumber and other Gherkin BDD frameworks
If you have never heard of “Cucumber” or “Gherkin”, that’s good as they are bad for test automation. Then you can safely ignore this section.
This section is for readers who have heard of “Cucumber” and “Gherkin”. Be aware of Cucumber and its variants such as SpecFlow and Concordion, and those “Given, Then, When” (i.e. Gherkin) style test frameworks.
Automated tests in a Gherkin framework are often appealing to managers and business analysts, great for demonstrations. But in reality, to my knowledge, all test automation attempts using Gherkin failed badly. Some might think me being subjective, but “Cucumber as a test tool it sucks” is actually the comment from Cucumber’s creator. For more, check out my other articles:
Why Gherkin (Cucumber, SpecFlow,…) Always Failed with UI Test Automation? (featured in Medium’s top publication: The Startup)
To summarise, avoid Cucumber, and avoid “Given, When, Then” in automated test scripts. Just use RSpec.
Learn RSpec in under a minute
RSpec is simple and easier to learn
describe "a group of releated test case" do
to its matchingend
A test group can contain one or more test cases.
Example:describe("User Authentcation")
inlogin_spec.rb
, the test script file can contain several user login test casesit "one test case" do
to its matchingend
For a test case, you can use plain English as the test case name.
Example:it "User login OK" do
,it "User login failed" do
expect(actual).to match(exepcted)
An assertion (a.k.a check).
Example:expect(page_text).to include("Sign in successful")
Of course, there are a lot more features in RSpec. Basic RSpec knowledge is enough for functional automated testers.
Before and After test case sections in RSpec
Have a look at a complete Rspec test structure (on the left).
Besides describe
and it
(two of them), there are before(:all)
, before(:each)
, after(:each)
and after(all).
These are called hooks which are used to execute scripts before and after test cases.
Now, try to work out the output of the RSpec test on the left. The purpose of this simple exercise is to understand the execution order. Compare yours with the answer (on the right).
Tasks
Task 1. Navigate to a URL
driver.get("https://...")
or driver.navigate.to("https://...")
, they are equivalent.
Task 2. Execute test script file (multiple test cases)
In previous sessions, you have run a single test case (as shown below)
To run the script file (i.e. all test cases in the test script file), you can select the second menu option ( Run test cases in ‘test script file name’
), or click the blue play button on the toolbar.
The run results panel in TestWise shows the results of three test cases.
Task 3. Optimize the test scripts to use `before(:each)` hook
For the following two test cases, do you notice their first test step is the same?
it "Login test 1" do
driver.get("https://travel.agileway.net")
driver.find_element(:id, "username").send_keys("agileway1")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:name, "commit").click
end
it "Login test 2" do
driver.get("https://travel.agileway.net")
driver.find_element(:id, "username").send_keys("another")
driver.find_element(:name, "password").send_keys("buildwise")
driver.find_element(:name, "commit").click
end
In other words, this step runs before each test case. We can move it to the before(:each)
hook.
before(:each) do
driver.get("https://travel.agileway.net")
end
it "Login test 1" do
driver.find_element(:id, "username").send_keys("agileway1")
# ...
end
it "Login test 2" do
driver.find_element(:id, "username").send_keys("another")
# ...
end
Full Test Script
load File.dirname(__FILE__) + "/../test_helper.rb"
describe "Multi Login" do
include TestHelper
before(:all) do
@driver = Selenium::WebDriver.for(browser_type, browser_options)
driver.manage().window().resize_to(1280, 800)
end
after(:all) do
driver.quit unless debugging?
end
before(:each) do
driver.get("https://travel.agileway.net")
end
it "Login test 1" do
driver.find_element(:id, "username").send_keys("agileway1")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:name, "commit").click
end
it "Login test 2" do
driver.find_element(:id, "username").send_keys("agileway2")
driver.find_element(:name, "password").send_keys("buildwise")
driver.find_element(:name, "commit").click
end
it "Login test 3" do
driver.find_element(:id, "username").send_keys("agileway3")
driver.find_element(:name, "password").send_keys("whenwise")
driver.find_element(:name, "commit").click
end
end