06: The second test: Failed to sign in (tips for productivity)
Add another test case in an existing test script file
Learning Objectives
Review
selenium
find_element
syntax withID
locatorRSpec test structure
Productivity Tips:
Validation
TestWise Snippet
Reformat (pretty-printing)
Code Completion (sort of)
Script Library
Test Case 06
There is no new concept for this test case, basically a clone of the first test (session #1) with a different user login, within the same test script file. But don’t copy and paste. Type them in in a quicker and smarter way, see below.
Productivity Tips
Tip 1. Faster (and Safer) Typing Selenium Statements
The first challenge for new-to-selenium-automation testers is typing Selenium statements correctly. When that happens (very common), the attendees are frustrated.
As a coach, I worked out a simple approach to overcome the following issues bothering beginners.
Not Remembering the Selenium syntax
Slow typing
Typo
Selenium syntax is quite simple (as below), still, beginners will take some time to get used to it.
driver.find_element(:id, "login-btn").click
driver.find_element(:name, "username").send_keys("ABC")
A simple solution to use Snippets in TestWise. Watch this video below.
To help you remember: d
is shorthand for driver
, fe
for find_element
.
dfe
→ driver.find_element(:how, "what")
, → means typing a Tab key to expand. This is a general selenium statement, users need to replace :how
and what
.
Normally, we add another character for a specific locator.
dfei → driver.find_element(:id, "id")
dfel → driver.find_element(:link_text, "Link")
dfen → driver.find_element(:name, "Name")
You get the idea.
Tip 2. Validation
The automated test scripts need to conform to a certain format, in this case, RSpec (and Ruby). In other words, to run a RSpec test script, it must be a valid Ruby script first.
A common reason that a beginner’s test script does not run: the test script is an invalid Ruby script.
The error is “15: syntax error, unexpected local variable or method, expecting ‘)’”. This indicated the possible error on line 15. Thanks to the syntax highlighting (in TestWise), it is quite easy to spot a missing right double quote after `username
`.
Please note, the error line number in the validation message is only indicative.
Tip 3. Reformat
New-to-coding testers’ first test scripts often end up like the below.
Click the ‘reformat’ button (as indicated below) on the toolbar to reformat the test script.
This looks much better (it is called proper indenting), right? That’s why “code reformat” is also known as “pretty printing”. Not only that, this will it much easier to spot syntax errors.
Tip 4. IntelliSense (sort of)
IntelliSense is a code-completion aid. Ruby is a dynamic language and TestWise a not a coding IDE, the code-completion support is limited in TestWise. It is still quite useful.
Let me illustrate (then you do it later) with an example. After `dfen
` and followed by a Tab key, I typed “.”.
TestWise will populate available Selenium operations to select. Type characters (‘se’
in the example below ) to narrow down the options list.
Press the “Enter” key to select.
Tip 5. Script Library
Beginners can complete this exercise under coaching. When practising this or similar test script alone, they often run into problems: they forget some syntax.
There is a way, to use the ‘script library’ (not test best name, I know) in TestWise.
Click the “Insert” button to add the selected selenium statement into the editor (test script).
Full Test Script
load File.dirname(__FILE__) + "/../test_helper.rb"
describe "Test Group" do
include TestHelper
before(:all) do
# browser_type, site_url defined in test_helper.rb
@driver = Selenium::WebDriver.for(browser_type, browser_options)
driver.manage().window().resize_to(1280, 800)
driver.get("https://travel.agileway.net")
end
after(:all) do
driver.quit unless debugging?
end
it "User can sign in OK" do
driver.find_element(:id, "username").send_keys("agileway")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:name, "commit").click
expect(page_text).to include("Signed in!")
end
it "User can sign in failed" do
driver.find_element(:id, "username").send_keys("agileway1")
driver.find_element(:name, "password").send_keys("testwise")
driver.find_element(:name, "commit").click
expect(page_text).to include("Invalid email or password")
end
end