Get Selenium on Rails to Use A Separate Firefox Browser Profile

Posted by Alvin Lai on November 05, 2009

You might notice that Selenium’s rake acceptance test would fail the second time because Firefox restored tabs that belonged to the first time you ran the acceptance test.

If you run Selenium for the first time round, you’ll end with 2 tabs, the second one being the results:

5b218e74660545b72c952d249954ac5e

When you run the test again, the first tab (circled in red) which was created in the previous session (also the first Selenium test), restored by Firefox, also runs a parallael Selenium test, resulting test failures:

C17adfa3188545cbbb61a90835821b6b

The way to fix this is to disable session restore in Firefox’s about:config:

15a44d34a68104d1f38d716a4872d801

5ec56c333c8308b83b38fca179ebb1ad

547440cfb1dd3719dca9cec797876ea4

But what if you love Firefox’s session restore feature? Create a separate Firefox profile for Selenium testing!

Go to Terminal.app then enter this to run Firefox’s Profile Manager:


/Applications/Firefox.app/Contents/MacOS/firefox-bin -ProfileManager

E2cddef89cdc23dcf81ae6c582a51642

Then modify /vendor/plugins/selenium-on-rails/lib/selenium-on-rails/acceptance_test_runner.rb:

Under the start_browser method, add a


-P Selenium

An example:


command = "\"#{path}\" -P Selenium \"http://#{HOST}:#{@port}#{TEST_RUNNER_URL}?test=tests&auto=true&baseUrl=#{base_url}&resultsUrl=postResults/#{log}&multiWindow=#{MULTI_WINDOW}\""

That would set Selenium to use Firefox with your newly created Selenium Profile, where you can turn off Session Restore just for that profile and still enjoy it in your default Firefox profile.

Git Ignore

Posted by Alvin Lai on November 04, 2009

Edit .git/info/exclude

and add your directories, i.e.:

/log/*
/db/schema.rb

Run Rake Tasks Locally in Capistano with Quit on Failure

Posted by Alvin Lai on November 04, 2009

After a git commit and push, the next command you might run is cap deploy. But how about running your tests?

If you don’t want to run any extra commands beyond git commit/push and cap deploy, you’ll want to look having your Capistrano script run your Rake tests locally too.

The Capistrano command “run” execute stuff remotely on your target deployed server, but since the deploy.rb Capistrano script is just a regular ruby script, you can just use “system()” or ““” (backticks) to execute commands locally.

Here’s an example:


# acceptance test
task :run_acceptance_test do
system("rake test:acceptance") # can use backticks `` as a shorthand also
exit if $? != 0
end
before "deploy:set_comment", :run_acceptance_test

Let me explain.


before "deploy:set_comment", :run_acceptance_test

would tell Capistrano to run your custom named task, in the case here, it’s “:run_acceptance_test”.


exit if $? != 0

Would check the error code of the previously run system command and if the error code (defined by $?) is not 0 (zero), it’ll cause the whole Capistrano script to exit.

So, if rake test:acceptance fails, the Capistrano script will exit and deployment will not happen at all.

Installing RSelenese in Selenium IDE

Posted by Alvin Lai on October 30, 2009

Selenium on Rails is just plain awesome, providing an web testing interface that doesn’t interfere with your main code. That said, there’s a Firefox plugin called Selenium IDE that records your actions in Firefox and generates tests for use with Selenium, how cool is that!

The only problem is, Selenium IDE doesn’t output in RSelenese and having to port one of their supported formats (Ruby Selenium) is a pain. So what do you do? Install the RSelenese plugin of course!

Here are the steps:

35aaa9dae09704b6186bf35f150277c2

F1b862385d68c472b8a0dca4758cd1cf

Then paste the following code:


/*
* Selenium on Rails RSelenese format for Selenium IDE
*
* Written by Shinya Kasatani (kasatani at gmail.com)
*/

load('formatCommandOnlyAdapter.js');

function string(value) {
if (value != null) {
value = value.replace(/\\/g, '\\\\');
value = value.replace(/\"/g, '\\"');
value = value.replace(/\r/g, '\\r');
value = value.replace(/\n/g, '\\n');
return '"' + value + '"';
} else {
return '""';
}
}

function underscore(text) {
return text.replace(/[A-Z]/g, function(str) {
return '_' + str.toLowerCase();
});
}

function formatCommand(command) {
var line = underscore(command.command);
if (command.target) {
line += ' ' + string(command.target);
if (command.value) {
line += ', ' + string(command.value);
}
}
return line;
}

this.playable = false;

(Taken from http://wiki.openqa.org/display/SIDE/SeleniumOnRails)

here:

39603dd88dd72866cfb0e2d5b074ff32

If you’d wanna tweak the generated RSelenese code, here’s a handy reference:

http://svn.openqa.org/fisheye/browse/~raw,r=1000/selenium-on-rails/selenium-on-rails/doc/classes/SeleniumOnRails/TestBuilder.html

Enjoy! :)

Ruby Singapore

Posted by Alvin Lai on July 06, 2006

Excited about this phenomenal language that’s so easy to use, flexibly powerful for most tasks, yet is actually fun to work with?

Then you might be interested to join singapore.rb!

As rssfwd’s author Choon Keat (also a member of singapore.rb) says, “It’s a young group, so be gentle :-D ”!

8 of us from Singapore.rb managed to meet up at City Hall’s Starbucks, with a couple of us displaying makeshift handwritten notepad signs of “RUBY SG” and “Ruby on Rails” on the coffee tables just to draw attention, a first for myself! :D

Among the ruby fans, there were an interesting bunch dealing with embedded systems, electronic payment (like point of sales), a technopreneur looking at improving workflows and customer relations of SMEs, web designers and developers.

We had a good exchange of how Ruby/Rails had helped them in their work and their hobbyist pursuits, interesting in every way.

There was also an energetic discussion RESTful paradigms for rails :)

Check us out!

Think Night

Posted by Alvin Lai on May 22, 2006

Met up with BingXiong again for his birthday treat at the famous Bedok Blk 85. Had really really nice oyster omelette (sorry was too carried away talking forgot to take pics), real good bak chor mee (no ter kwa), so-so hokkien mee and fresh succulent sambal stingray.

After sharing about what has kept us occupied for the past week (I was just busy mucking about with Ruby on Rails features, setting up my inept 1GHz desktop PC as a linux server, helped out a day in Arts open house preparations and eating real nice Stamford House Japanese curry, you can read what BingXiong’s been up to at his blog), we proceeded to have an interesting discussion about the entrepreneurship scene in Singapore, voicing our thoughts about what we want to do, which eventually led to some fruitful conclusions..

About how ProjectSenso decided its time for strategic change and stuff.

BX had plans for a online checklist for business start-ups, where they would login to update their status/progress, which was good in terms of providing value – something that protential start-ups would like to review and keep track, but I felt was somewhat limited and more apt to be packaged as a FAQ section, as I thought people wouldn’t actually want to signup, login and remember their account logins just to tick some checkboxes to feel good. There must be something more compelling for them to want to sign up and keep coming back and logging into their account.

Soon it came to me.

What do I want to do?

I have a vague idea in mind of doing web development in Singapore, to construct customised web applications to facilitate business or any company workflow. Am I also looking at providing features similar to Basecamp? Maybe.

Why do I want to do this?

Maybe because I had experience in using web technologies to facilitate an organisation’s workflow previously and had really enjoyed doing it. It just feels awesome when your application complements the organisation’s workflow and triples productivity!

Other more reasons? Maybe but the former is probably the strongest reason so far.

Questions and Self Reflection

Soon after we departed, I took out a jotter book to pen down the thoughts that were swirling in my head.

Why am I bothered with Ruby on Rails?

It’s fast

Can rapidly prototype and churn out rough sketches/design early and quickly for client’s approval and correction.

It also gives me an edge over many other developers who use “more traditional” web scripting languages (read: PHP, ASP) and frameworks (like Tapestry).

Similar to what Paul Graham article on Beating the Averages, where he reveals that he used a powerful languge called Lisp in his early days to have an edge over his competitors in terms of speedy development.

It’s easy to maintain

Many a times I’ve tried to go back to my old code to make updates, only to find that I’ve taken some obscure shortcut that would take me several hours to figure out before I can get back to the same mental state of mind when I was coding it.

With Rails where convention is emphasised, it would be more straightforward.

On the way back home, I realised that I’ve been too caught up learning about other Rails’ features and capabilities and other sweet stuff like Automated Application Deployment via Capistrano and fussing about Subversion with Rails

They’re just plain distractions from what I really want to achieve using Ruby on Rails. The above are secondary and should only come when I’m ready with Rails.

Well what’s my Ruby on Rails mission statement? Thought about it for a long while on the bus and here’s it.

My Ruby on Rails Mission Statement

To come up with a web application that has user authentication, login to user’s own profile and space. An administration panel to manage users in groups where different roles and permissions can be applied to a single user or to a group. QUICKLY.

Next Actions

As in true GTD style, here’s a list of next actions for me to undertake in line of my Rails mission statemnt.

  1. Do Database Migrations – why: easy to keep track of db schema changes, don’t need to write SQL code.

  2. Clarify basic templating, hyperlinking from base controller actions via link_to

  3. Clarify Hyperlinking to independent pages from independent controllers. Look at own experiment with railsapp alblog.

  4. Clarify templating data, ie, using another controller’s scaffolding.

  5. Check out alternatives to authentication and different styles, decide which one best to learn and use.