Getting Started With Selenium - Chapter 6: Packaging for Use
Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from intent to execution in minutes.
|
x
Blog
Getting Started With Selenium - Chapter 6: Packaging for Use
This situation is the 6th in a series of “ Getting Started with Selenium Testing ” posts from Dave Haeffner, a noted expert on Selenium and automated testing, and a frequent subscriber to the Sauce blog and Selenium community. This series is for those who are brand new to test mechanisation with Selenium and a new chapter will be post every Tuesday (eight chapter in all).
Packaging For Use
In order to get the most out of our tests and page objects, we & # x27; ll ask to package them into a more useful structure. Let & # x27; s do that using the examples from the previous write-ups.
Globose Setup And Teardown
First we & # x27; ll need to pull the tryout setup and teardown actions out of our tests and into a central property. In RSpec this is straight-forward through the use of a & # x27; spec_helper & # x27; file.# filename: spec_helper.rb
require & # x27; selenium-webdriver & # x27;
RSpec.configure do |config|
config.before (: each) do
@ driver = Selenium: :WebDriver.for: firefox
end
config.after (: each) do
@ driver.quit
end
end
We need to include the Selenium library here, and by doing so, can remove it from our tests. And by having our test configuration here, we can withdraw it from the ` before (: each) ` and ` after (: each) ` in our tests as well -- replacing them with a simple require statement at the top of the file (` require_relative & # x27; spec_helper & # x27; `).
Base URL
Rather than hard-coding a URL in our tests and page objects, We & # x27; ll need to put it someplace central, and we & # x27; ll want it to be configurable. So let & # x27; s create a & # x27; config.rb & # x27; file in the parent directory and place it there.
# filename: config.rb
ENV [& # x27; base_url & # x27;] ||= & # x27; http: //the-internet.herokuapp.com & # x27;
For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.
By utilize a conditional when setting the environment variable (` ||= `) we are making it so we can override this value when launching our test suite (e.g., ` base_url= & # x27;http: //localhost:4567 & # x27; \ `). It fundamentally means if the surround variable already subsist and contains a value, use it. Otherwise, set it to & # x27;http: //the-internet.herokuapp.com & # x27;. We can then reference this variable in our page objects where necessary (if we haven & # x27; t already). For instance:# filename: login.rb
category Login
...
def initialize (driver)
@ driver = driver
@ driver.get ENV [& # x27; base_url & # x27;] + & # x27; /login & # x27;
end
...
Folder Organization
It & # x27; s about time we make some folder for our specs and page objects. To err on the side of simplicity, let & # x27; s phone the folders & # x27; spec & # x27; (for our trial) and & # x27; Page & # x27; (for our page objective). We are using & # x27; spec & # x27; since it is a default folder that RSpec will look for. Here & # x27; s everything we should have after creating folders and moving file around:
.
| -- config.rb
| -- Gemfile
|-- pages
| | -- dynamic_loading.rb
| ` -- login.rb
`-- spec
| -- dynamic_loading_spec.rb
| -- login_spec.rb
` -- spec_helper.rb
Updating Require Statements
As a result of doing this, we will need to update the require statements in our examination.# filename: spec/login_spec.rb
require_relative & # x27; spec_helper & # x27;
require_relative & # x27; .. /pages/login & # x27;
describe & # x27; Login & # x27; do
...
# filename: spec/dynamic_loading_spec.rb
require_relative & # x27; spec_helper & # x27;
require_relative & # x27; .. /pages/dynamic_loading & # x27;
describe & # x27; Dynamic Loading & # x27; do
...
Note the use of double-dots (` .. `) in the page object require statement. This is how we tell Ruby to traverse up a directory (from our spec directory) before attempt to entree the page objects folder. The ` spec_helper ` require continue unaltered since this file go in the same directory as our tests.
Running Everything
Now that things are clean up, we can run everything. To do that we & # x27; ll want to make sure to include our new config file. We can do that by specifying it at run time with ` rspec -- involve ./config.rb `, or, ` rspec -r ./config.rb ` (for short). Note the ` ./ ` before ` config.rb `. This recite RSpec that the config file is in the current directory. Give it a shot. All of the tests should run and pass. For more examples like this (along with accomplished working code) -- grab your copy ofThe Selenium Guidebook.
Dave is the author of Elemental Selenium (a free, erst weekly Selenium tip newsletter that is read by century of try professionals) as well as a new record, The Selenium Guidebook. He is also the Almighty and maintainer ofChemistryKit(an open-source Selenium framework). He has helped legion companies successfully implement automated adoption quiz; including The Motley Fool, ManTech International, Sittercity, and Animoto. He is a laminitis and co-organizer of theSelenium Hangoutand has spoken at numerous conferences and meetups about acceptance examination.
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