follow me icons

Wednesday, September 5, 2012

Frame in Capybara

To access the element in a frame or iframe using capybara use the following within method

  within_frame('iframex') do
    fill_in "email", :with => random_email

Wednesday, August 22, 2012

Ignore browser untrusted security certificate

To ignore the browser untrusted security certificate so that the cucumber test can be run in the test environment you can modify the env.rb to ignore the warning:
require 'selenium/webdriver'

Capybara.register_driver :selenium do |app|
   profile =
   profile.assume_untrusted_certificate_issuer = false, :browser => :firefox, :profile => profile)

Tuesday, August 14, 2012

file upload using capybara

To put the file name into the file upload dialog box with Capybara we can use the following Selenium webdriver method. We can use the Ruby code Dir.pwd to get the current working directory path.
page.driver.browser.find_element(:id, "fileUploadId").send_keys 
(Dir.pwd + "file upload path")

Monday, July 2, 2012

Webdriver can't click element in IE

The issue that I have is that the capybara/selenium webdriver method of click element does not seem to work in IE.
  find(:css, ".submit").click #does not seem to click the element in IE
This method works fine in other browsers like FF. When I try to just find the element by using the
  find(:css, ".submit")
  it can return this:  #<Capybara::Element tag="input">
Apparently this is because I accidentally change the zoom level in Internet Explorer. To fix this issue simply set the zoom level to be 100%.

Wednesday, June 20, 2012

IE webdriver maximize window

Running cucumber test with webdriver in Internet Explorer browser has alot of issues. One issue that I encounter is that the IE browser window is not opening in a maximized state. Apparently, webdriver needs to have IE browser fully maximize in order for webdriver to be able to locate elements. A workaround that I use is to maximize the IE browser on my first cucumber steps. To change/resize the browser window:
#need to check the browser version is IE via webdriver as other browsers like ff and chrome do not to be maximised
if(page.driver.browser.browser.to_s == "internet_explorer")

IE undefined method map for nil class

I get the following error when running webdriver in IE 7 and 8. I am using ruby, Capybara with selenium webdriver version 2.2.24
Undefined method `map` for nil:NilClass (NoMethodError)
NoMethodError Exception: undefined method `map` for nil:NilClass
It seems that IE stand alone webdriver could not switch between http to https then to http again correctly and therefore it could not find any of the DOM element. This also cause the error of unable to delete cookie in IE
Selenium::WebDriver::Error:JavascriptError Exception: Unable to delete cookie with name 'cookiename'
The workaround that I have for this Internet explorer webdriver issue is to start the session with https instead of http. This seem to have solve the issue. Alternatively, during the session you can make the test to visit a https page and then visit the current page again.

Capybara refresh back forward a page

To refresh a page in Capybara using selenium webdriver simply call the webdriver method directly
page.driver.browser.navigate.refresh (to, back, forward)


you can use the visit 'url' method

Capybara save page method

This is a pretty useful method to save the current active page for debugging. This method can be triggered when the scenario fail.
After do |scenario|
  if scenario.failed?
This will create a file called capybara-timestamp.html in the current directory

Tuesday, June 19, 2012

Capybara running in IE - Internet Explorer

To switch capybara to run in IE or different browser:
Capybara.register_driver :selenium do |app|, :browser => :internet_explorer)


Capybara.register_driver :selenium do |app|, :browser => :firefox)


Capybara.register_driver :selenium do |app|, :browser => :chrome)
For IE, you just need to download the standalone ie driver from selenium webdriver page InternetExplorerDriver otherwise you will get the webdriver warning "the IE driver is moving to a standalone executable... Falling back to bundled DLLs for now.." You can put the IE standalone driver in your rubyxxx/bin folder.

Monday, June 18, 2012

Modal dialog present webdriver Error

To surpress the IE security warning modal dialog present (Selenium::WebDriver::Error::UnhandledAlertError) use the following code

Internet Explorer security warning modal dialog box:

"Do you want to view only the webpage content that was delivered securely? This webpage contains content that will not be delivered using a secure HTTPS connection, which could compromise the security of the entire webpage "

"Internet explorer - This page is accessing information that is not under its control. This poses a security risk. Do you want to continue?" This warning is probably because we are using the IE Standalone selenium webdriver. The workaround for the issue:

   click_link "test"
rescue Selenium::WebDriver::Error:UnhandledAlertError => e
   # may need to use the page.driver.wait_until method to wait until the alert present  other wise you may get the 
   # No alert is active (Selenium::WebDriver::Error::NoAlertPresentError)
   click_link "test"

cucumber undefined method 'step' for error

This error undefined method `step` for "#<object:0x1a4f9f0>" (NoMethodError) occurs because you are using an older version of cucumber and therefore and older version of the Gherkin gem.

I was using cucumber 1.0.2 and I got the undefined method 'step' error. I upgraded to cucumber 1.2.1 and it has the method defined.

Wednesday, June 13, 2012

Installing and managing Ruby version using RVM

To install ruby using RVM please go to this article which also explain on how to switch back and forth between different version of ruby: Install Ruby Mac

Tuesday, June 12, 2012

Using 'Given/When/Then' in step definitions is deprecated, use 'step' to call other steps

If you are getting this warning "Using 'Given/When/Then' in step definitions is deprecated, use 'step' to call other steps" then just change this line in your steps definitions:
Given /^the user logs in$/ do
 And 'the user visits the main page'
 And 'the user clicks on the login button' 
Given /^the user logs in$/ do
 step('the user visits the main page')
 step('the user clicks on the login button')

term/ansicolor LoadError - cannot load such file

When you are using Using cucumber (1.2.1), you may get the cannot load such file -- term/ansicolor (LoadError). To fix it simply install 'gem install term-ansicolor'

Thursday, May 24, 2012

Capybara using contains to select an element

Suppose you have a select element that has a random number as the id like the one below:
< select class="select_xpath_class" name="myMethods[select_method_id_23749837]" id="select_method_id_23749837">
Please select... Select1 Select2 Select3

Capybara method of select won't work:"Select1", :from => "select_method_id")
This is because it will only find the exact match of the id.

To solve this we can use the find method directly with xpath:
find(:xpath, "//*[contains(@id, 'select_method_id')]").select("Select1")

GEB - selecting an element with partial name

To select an element on a webpage with a partial name in Geb and Groovy:
   static content = {
        elementField {elementName -> $('select', name: contains(/button[$elementName/))}

   def selectContent(String selectOptionName, String value) {

Check element exists in GEB

To check whether an element exists in a page using GEB:
   static content = {
        searchVisibleField(required: false) {$("#search-id")}

    def isSearchVisible()
        if (browser.searchVisibleField() == geb.navigator.EmptyNavigator.instance())  {
        else {
searchVisibleField returns the instance of geb.navigator.EmptyNavigator class so we can use that to check whether an element exists or not in a page. Reference from The Book of GEB

Wednesday, May 23, 2012

Selecting radio button value in GEB

To select a radio button value in GEB using Groovy: in the page model define:
    static content = {
        selectRadioButtonOptionField {option -> $("input[type='radio']", name: "radioButtonName", value: option)}

    def selectRadioButtonOption(String radioButtonValue) {
I could not use the .value or = to select the radio button so I need to use the click method to select.