Getting Started with Selenium - Chapter 3: Writing Your First Selenium Test
Sauce AI for Test Authoring: Move from design to executing in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from design to executing in minutes.
|
x
Blog
Getting Started with Selenium - Chapter 3: Writing Your Inaugural Selenium Test
This post is the 3rd in a series of “ Getting Started with Selenium Testing ” posts from Dave Haeffner, a celebrated expert on Selenium and automated testing, and a frequent subscriber to the Sauce blog and Selenium community. This serial is for those who are make new to essay automation with Selenium and a new chapter will be posted every Tuesday (eight chapter in all).
Writing Your First Selenium Test
Fundamentally, Selenium works with two pieces of information -- the element on a page you want to use and what you want to do with it. This one-two biff will be repeated over and over until you achieve the effect you want in your covering -- at which point you will perform an asseveration to confirm that the result is what you destine. Let & # x27; s conduct logging in to a website as an example. With Selenium you would: 1. Visit the main page of a site 2. Find the login button and chatter it 3. Find the login descriptor & # x27; s username battlefield and input text 4. Find the login form & # x27; s password battleground and input text 5. Find the login form and click it Selenium is capable to find and interact with ingredient on a page by way of respective locator strategies. The list includes Class, CSS, ID, Link Text, Name, Partial Link Text, Tag Name, and XPath. While each serves a aim, you only need to know a few to start writing effective tests.
How To Find Locators
The simplest way to find locater is to scrutinize the elements on a page. The best way to do this is from within your web browser. Fortunately, democratic browsers come pre-loaded with development instrument that make this simple to accomplish. When viewing the page,right-clickon the element you want to interact with andclink Inspect Element. This will bring up a small window with all of the HTML for the page but zoomed into your highlighted selection. From here you can see if there are unique or descriptive attribute you can work with.
How To Find Prime Elements
Your focussing with picking an effective element should be on finding something that isunique, descriptive, and unconvincing to change. Ripe candidates for this areid and classattribute. Whereas copy (e.g., textbook, or the schoolbook of a link) is less ideal since it is more apt to change. This may not hold true for when you make assertions, but it & # x27; s a full destination to reach for. If the elements you are attempting to work with don & # x27; t get uniqueid and classattributes directly on them, look at the ingredient that house them (a.k.a. the parent factor). Oftentimes the parent element has a unique locator that you can use to depart with and drill down into the element you want to use (with CSS selector). And if you can & # x27; t find any unique elements, have a conversation with your development team letting them know what you are essay to accomplish. It & # x27; s not hard for them to add helpful, semantic markup to make test mechanisation easygoing, especially when they know the use case you are trying to automate. The alternative can be a lengthy, painful procedure which will credibly yield work test code -- but it will be brittle and difficult to maintain.
Steps To Writing a Selenium Test
There are five parts to write a Selenium test: 1. Find the ingredient you want to use 2. Write a tryout with Selenium actions that use these elements 3. Figure out what to assert 4. Write the assertion and verify it 5. Double-check the statement by forcing it to fail As part of writing your Selenium exam, you will likewise need to create and destroy a browser instance. This is something that we will pull out of our tests in a future moral, but it & # x27; s worth knowing about up front. Let & # x27; s conduct our login representative from above and step through the test penning process.
An Example
The best way to find the Selenium actions for your specific language is to look at the available language binding wiki pages (Ruby, Python,JavaScript) or the Selenium HQ go start support for WebDriver. For this example, I & # x27; ll be utilisethe Ruby programming language, RSpec(an open-source testing framework written in Ruby), andthe-internet(an open-source exemplar application I built).
1. Find the ingredient you want to use
Let & # x27; s usethe login example on the-internet). Here & # x27; s the markup from the page.
html
Username
Password
Login
Note the unique elements on the form. The username input field has a uniqueid, as does the parole input field. The submit push doesn & # x27; t, but the parent component (form) does. So instead of clicking the submit button, we will have to submit the shape instead. Let & # x27; s put these elements to use in our initiatory tryout (or & # x27; spec & # x27; as it & # x27; s ring in RSpec).
For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.
2. Write a test with Selenium actions that use these elements
# filename: login_spec.rb
require & # x27; selenium-webdriver & # x27;
describe & # x27; Login & # x27; do
before (: each) do
@ driver = Selenium: :WebDriver.for: firefox
end
after (: each) do
@ driver.quit
end
it & # x27; succeeded & # x27; do
@ driver.get & # x27;http: //the-internet.herokuapp.com/login'
@ driver.find_element (id: & # x27; username & # x27;) .send_keys (& # x27; tomsmith & # x27;)
@ driver.find_element (id: & # x27; password & # x27;) .send_keys (& # x27; SuperSecretPassword! & # x27;)
@ driver.find_element (id: & # x27; login & # x27;) .submit
end
end
If we run this (e.g.,rspec login_spec.rbfrom the command-line), it will run and pass. But there & # x27; s one thing missing - an assertion. In order to find an element to make an assertion against, we need to see what the markup is after submitting the login sort.
3. Figure out what to assert
Here is the markup that renders on the page after submitting the login descriptor.
You logged into a untroubled area!x
Secure Area
After logging in, there looks to be a duo of thing we can key off of for our assertion. There & # x27; s the tatty message class (most appealing), the logout button (appeal), or the transcript from the h2 or the flash message (least appealing). Since the flash content class name is descriptive, denote success, and is less likely to change than the copy, let & # x27; s go with that.
4. Write the asseveration and control it
# filename: login_spec.rb
require & # x27; selenium-webdriver & # x27;
describe & # x27; Login & # x27; do
before (: each) do
@ driver = Selenium: :WebDriver.for: firefox
end
after (: each) do
@ driver.quit
end
it & # x27; successful & # x27; do
@ driver.get & # x27;http: //the-internet.herokuapp.com/login'
@ driver.find_element (id: & # x27; username & # x27;) .send_keys (& # x27; username & # x27;)
@ driver.find_element (id: & # x27; password & # x27;) .send_keys (& # x27; password & # x27;)
@ driver.find_element (id: & # x27; login & # x27;) .submit
@ driver.find_element (css: & # x27; .flash.success & # x27;) .displayed? .should be_true
end
end
5. Double-check the assertion by forcing it to fail
Now when we run this trial (rspec login_spec.rbfrom the command-line) it will legislate just like before, but now there is an assertion which should catch a failure if something is awry. Just to get sure that this test is make what we think it should, let & # x27; s change the asseveration toforce a failureand run it again. A bare fudging of the locator will suffice.
@ driver.find_element (css: & # x27; .flash.successasdf & # x27;) .displayed? .should be_true
Dave is the author of Elemental Selenium (a free, once hebdomadal Selenium tip newsletter that is say by 100 of try professionals) as well as a new volume, The Selenium Guidebook. He is besides the Jehovah and maintainer ofChemistryKit(an open-source Selenium framework). He has helped numerous companies successfully implement automated adoption testing; include The Motley Fool, ManTech International, Sittercity, and Animoto. He is a founder and co-organizer of theSelenium Hangoutand has spoken at numerous conferences and meetups about credence testing.
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free