Payday Loans Online Payday Loans Online

Todd Rothe : UI/UX Developer

it's pronounced rowth-ee

By 2g1c2 girls 1 cup

FlexPilot, Ruby, Selenium WebDriver, Jenkins and Windows Part 2

Recently I have been tasked with running automated tests against an html/javascript/flex app as part of the automated build.
This is part 2 in a series of posts detailing the flexpilot install, writing ruby tests, and configuring the jenkins targets and windows box to run the automated tests. The completed ruby test can be seen by viewing source, downloading the project and opening the SampleTest.rb on your local maching.

Part 2 – Writing FlexPilot tests in ruby.

I should start by saying I’m on a mac (and you can too). Open a terminal and type:

ruby –version

If you don’t see ruby 1.9.3p125 you may want to install RVM and upgrade to ruby 1.9.3.
Feel free to run through this with ruby 1.8, but know that you may experience issues.

Create a tests.automation.flexpilot package in the root of the project we created in part 1. Inside, create a new text file and call it SampleTest.rb. Then add the following boilerplate code:
require "rubygems"
require "selenium-webdriver"
require "test/unit"
class MySampleTest < Test::Unit::TestCase
# Called before every test method runs.
def setup
@driver = Selenium::WebDriver.for :firefox
@wait = Selenium::WebDriver::Wait.new(:timeout => 20) # seconds
@verification_errors = []
end
# Called after every test method runs.
def teardown
@driver.quit
assert_equal [], @verification_errors
end

Forgive my smashing this snippet together. I need to get a new ‘code display’ plugin for my blog.
You see the ‘require’ statements at the top which correspond to the gems we will be using.
Next is the ‘class’ declaration, class name and base class (Test::Unit::TestCase).
Then a ‘setup’ method is defined. Inside;
@driver – an instance of selenium web driver for firefox is created
@wait – a 20 sec wait
@verification_errors – an array to hold errors.
Finally, a ‘teardown’ method is defined. Inside, we tell the driver to quit and assert that the verification_errors array is empty.

Now for the less boilerplate.
Define your test method below teardown and tell the driver to load your app. Something like:
def test_loadAndClickButton
@driver.get("http://localhost:8888/coolShit/MyApp.html")
sleep(5)
end

*note: the ‘test_’ in our ‘def’ name alerts ruby that this is a test that should be exed when this class is run
*note: the ‘sleep(5)’ sleeps our test until the app and flexpilot are loaded, after which we can use our ‘@wait’.

Go to your terminal window and cd into the flexpilot dir that holds SampleTest.rb and then type the following:
ruby SampleTest.rb
Unless you have the required gems installed, you should get an error that looks something like this:
no such file to load — selenium-webdriver
Simply install the missing gem(s) by typing the following into the terminal:
gem install selenium-webdriver
You’ll see some text in the terminal confirming that the gem has been found and is loading/loaded.

Run your test again (ruby SampleTest.rb) and watch firefox popup, load your app, and then close after 30 seconds (our sleep duration). Very cool, but worthless so far. Let’s add some code to allow our test to interact with our app
.
MyApp is super basic, consisting of the flexpilot hook and one button that launches an alert when you click it. The button looks like this:
< s:Button id="loginButton" automationName="loginBtn" label="Login" click="{Alert.show('Zark Wad!')}"/>
Notice the ‘automationName=”loginBtn” ‘. This is what we will use to target and click the button from our test. Add the following to the bottom of you test method:

@wait.until{ @driver.execute_script("return document.MyApp.fp_assertDisplayObject({automationName:'loginBtn'});") == true }
@driver.execute_script("document.MyApp.fp_click({automationName:'loginBtn'})")
sleep(10)

The first line of code tells the driver to wait until the loginBtn exists as a child of the stage.
The next line clicks the loginBtn.
The next line delays the teardown so you can see the results of the click.
Hopefully you are seeing the alert in all its automated glory!

 

No Responses to “FlexPilot, Ruby, Selenium WebDriver, Jenkins and Windows Part 2” (post new)

 

Leave a Reply

You must be logged in to post a comment.