-
Notifications
You must be signed in to change notification settings - Fork 0
Introduction
Let’s begin with some definitions:
Ruby - an object-oriented programming language designed with a natural language syntax that makes it easy to use while retaining all of the power and convenience of a modern programming language.
Gem - a gem is a packaged Ruby application or library. It is used to extend or change functionality within ruby applications.
Cucumber - a gem for Ruby that is used to test applications. It extends the natural language syntax of Ruby even further to create test scenarios that are easy for everyone involved to understand. All of the scenarios and feature files used in this project are based on Cucumber.
Watir - a gem for Ruby. It provides a means of manipulating a web browser and access to simplified methods for doing common tasks like clicking on links, navigating to web pages and interacting with other html elements.
Page-Objects - a gem for Ruby. Within your web app's UI there are items and elements that your tests interact with. A Page Object simply models these as objects within the test code. This reduces the amount of duplicated code and means that if the UI changes, the fix need only be applied in one place.
Git - a version control system that allows individuals to collaborate on complex projects without fear of losing work or overwriting other people’s changes.
Cloud9 - an online IDE(integrated development environment) that is running on an Ubuntu Linux virtual machine. Cloud9 provides access to this virtual machine via the browser and runs entirely in a browser window, thus removing the barrier of installing and configuring Ruby and Git in a Windows environment. This gives you an simple, intuitive interface with your file/resource browser, code editor, and terminal window all easily accessible while drastically reducing setup time.
Before getting started we need to get your Cloud9 workspace setup. Go to Cloud9 and click the Sign Up button. Click on GitHub and sign in with your GitHub ID. Now that you’re logged in we can set up a workspace for the project:
- Click CREATE NEW WORKSPACE and select ‘Clone From URL’.
- In the Source URL: field, paste the Git url for our project:
- Click the Ruby on Rails icon and then click CREATE.
- Wait for the project to finish processing.
- Click on the project name on the left, ‘ruby-automation-bs’, and then click the START EDITING button.
- After the workspace loads, you’ll see 3 sections.
- A file browser tree on the left showing all the files that were cloned in from the project repository on Github.
- A Welcome screen on the top.
- A terminal window on the bottom labeled
bash - "your_username".
- Click inside the terminal and enter this command:
bundle install- This will tell Ruby to check the Gemfile of the project for the required gems for the project and install any that are missing.
- Enter this command:
bundle update- This will tell Ruby to update any installed gems to the latest versions and check the Gemfile for specific versions of any of the gems and update to those specific versions.
- Enter this command:
sudo apt-get install firefox=28.0+build2-0ubuntu2- When asked "Do you want to continue? [Y/n]", type “y” and press Enter.
- This command installs Firefox version 28 onto the virtual machine, which is necessary because that’s the application the project is testing against.
- Enter this command:
sudo apt-get install xvfb- When asked "Do you want to continue? [Y/n]", type “y” and press Enter.
- This command installs the Xvfb application, which allows the virtual machine to run Firefox without actually displaying the browser, known as ‘headless’ mode. This functionality is what enables Cucumber to run tests in headless mode(using the Ruby gem ‘headless’) and allows us to use Cloud 9 as a development environment for our Ruby project.
- Now that all the necessary software is installed, we’ll make sure Git is setup correctly.
- Enter this command:
git config --global user.name "John Doe"with your name between the quotes. This sets the display name that will show on your commits on Github. - Enter this command:
git config --global user.email johndoe@example.comwith your email address. This sets the email address that will show on your commits on Github. - Enter this command:
git config --global push.default simple. This tells Git to use the newer push style and will avoid prevent Git from spamming you with a reminder message every time you use the push command. - Enter this command:
git config --global credential.helper 'cache --timeout 86400'. This will ensure that you only have to enter your Git username and password once every 24 hours.
- Enter this command:
- Your Cloud 9 environment is now setup and ready to develop and run tests! Let’s test the functionality and make sure everything is working correctly.
- Enter the command:
HEADLESS=true cucumber - This will run the current set of Cucumber Features. It should take about 5 minutes and complete with all Green and no Red.
- Enter the command:
Now that we have Cloud 9 set up and Git is configured, we’ll go over some basics of using Git and Ruby in a Linux environment like Cloud 9. Take a look at the prompt in the terminal, it should look something like this:
ryderstorm@ruby_automation_bs_test:~/workspace (master) $
The prompt contains a lot of information and is organized like this:
username@cloud9_workspace_name**:**~/current_folder_location (current_git_branch) $
The first thing to do is switch to the tutorial branch of the repository to make sure we don’t make any accidental changes to the master branch. Enter this command in the terminal: git checkout tutorial. This will switch your working branch to the tutorial branch and you will see that change reflected in the terminal prompt.
To get a feel for Ruby we’re going to use the Ruby program IRB, or Interactive Ruby. This is built into the installation of Ruby and to run it, simply type irb into the terminal and press enter. The terminal prompt should now look similar to this:
2.1.1 :001 >
We can now run Ruby commands directly in the terminal and instantly see the output. For example, if you type "Hello, World!" and press enter, the terminal will respond with => “Hello, World!”. If you type 1 + 2 and press enter, the terminal will respond with => 3. For more examples of what can be done in IRB, checkout Ruby In 20 Minutes in the Additional Resources section.
Next we’re going to run the init_irb_browser.rb file. This file creates a @browser instance of the Watir webdriver using Firefox and the headless gem, navigates to the BlueSource staging environment at https://bluesourcestaging.herokuapp.com, and logs in as the company.admin user. You can view the contents of the file by double clicking it from the file tree on the left to see the code.
To run the file, enter this command in the IRB terminal: load ‘,/init_irb_browser.rb’. You should see the following output:
2.1.1 :001 > load './init_irb_browser.rb'
Creating headless instance
Creating browser instance
Logging into Blue Source as company.admin
Browser loaded and initilization complete!
The @browser instance variable is now available for use.
See the Browser_Loaded image in the ./screenshots directory
for verification of browser state.
Use the take_screenshot method to capture an image of the current browser state.
=> true
2.1.1 :002 >
IRB now has an @browser object available that represents a headless instance of Firefox that’s running in the background.
NOTE: One drawback of using Cloud9 is the browser created by Watir isn’t directly visible. This is why we have to use the headless gem. While this is a limitation, it will reinforce your understanding of how Ruby uses Watir to control the browser. Since your only interaction with the browser happens through the IRB terminal you’ll quickly learn how to use Watir’s methods to control the browser. You can take a screenshot of the browser at any time to verify its state. Also, any command you run in IRB against the browser will provide output in the terminal telling you the result of the command. You can always keep another local browser open to the website for visual reference whenever you need it.
It is certainly possible to install Ruby and Watir on your local Windows machine to control the browser directly if you need to see what is happening in real-time, but be aware that you will almost surely run into configuration issues.
To begin interacting with the browser, enter this command in the IRB terminal: @browser.goto('www.orasi.com). Wait for the command’s output to appear. It should be => "http://www.orasi.com/Pages/default.aspx". The browser should now be at the Orasi homepage. Now enter this command: take_screenshot. A new screenshot file should appear in the screenshots folder in the file tree on the left. Double click it to open and you should see the Orasi homepage appear. Now enter this command in the IRB terminal: @browser.link(text: 'Testing Services').click. The command should give the following output: => [] . Once it has, enter the take_screenshot command again, open the new screenshot, and you should see the Testing Services page of the Orasi website.
To quit IRB, simply type quit and press enter and you’ll see the main terminal prompt appear again. This also closes any programs(like Firefox) you started via IRB. You can launch IRB from the terminal at any time.
Next we’ll add make some changes to a local file and use Git to save them to the repository. Enter the command git status in the terminal. It should respond with the branch you’re on, the branch status in relation to the origin branch(master in this case), and the message nothing to commit, working directory clean, which indicates you have not made any changes to any of the project files.
Find the ‘Tutorial_Participants’ file in the folder tree on the left and double click it to open it. Add your name and the date to the file and save it. Now enter the git status command again. It should show you that the Tutorial_Participants file is now in a modified status and is shown in red, indicating it is not staged for commit. Enter this command to add it to the staging area: git add Tutorial_Participants. Enter git status again. Tutorial_Participants should now be listed in green under changes to be committed.
Enter the command git commit -m ‘adding myself to the participants file’. Git should respond with a summary of the changes you just made with your commit. Keep in mind that a commit only stores the change locally. To add your changes to the project on Github enter this command: git push. This should prompt you for your Git username and password. Enter them and you should see a summary describing all of the changes that were made to the project and sent to Github. Now all of those changes are available to everyone else on the project. To see the log of changes that have occurred on the branch, you can use the command git log.
That covers the basics of using Ruby with Watir to automate the testing of a website and how to save your work using Git. It is highly recommended that you go through the items in the Additional Resources section in the order they are listed. The Cucumber tutorial is especially essential. While it is lengthy it will give you everything you need to understand how to write scenarios and features and how all of the project files interact with each other.
-
There is no object repository with Cucumber and Watir, so any interaction with elements of the browser depends on descriptive programming or creating references to the element within the page objects. Looking at the source code for the website should provide everything you need to access the elements on the site. If you are having a problem interacting with an element it can be helpful to do a search for "Watir " - this will usually point you in the right direction
-
Whenever you pull down an update from Git that included changes to the Gemfile, you'll need to run
bundle installandbundle updateto make sure all your gems get updated. If you don't do this you will probably get errors -
While Git is great for keeping development of different features separate to avoid the confusion that happens when multiple people are working on the same thing at the same time, the only way anyone will know that you are working on something is if you tell them. Keep the other members of the team aware of what you’re working on to avoid duplicating work.
-
Since Git is a version control system that logs every change along with who was responsible for it, nothing can ever be broken beyond repair. However, it is important that all work on the project be done in its own branch - DO NOT work in the master branch and never push to the master branch. This will help avoid conflicts and ensure that all work submitted is approved by project leads.
-
Remember that because it is an online IDE, Cloud9 is accessible from any computer at any time in the exact state you left it. Your project can also be shared with other people for simultaneous collaboration, so don’t hesitate to invite others to your workspace to get help with a problem.
-
Any changes you make have to make it past Rubocop or the build that is automatically generated whenever you push your code up will fail. Make sure to run Rubocop periodically and before you push code to ensure your code meets the requirements of the Ruby Style Guide. This will keep all of the code on the project clean, efficient, and easy to maintain.