kloia Blog

Using Capybara Effectively with 8 Useful Configuration Parameters

Written by Ümit Özdemir | Dec 16, 2019 9:30:27 PM

Capybara is a test automation framework for developing automated functional UI tests. There are more efficient configuration parameters for Capybara.

      Capybara works with Selenium, Rack etc. and it shortens very long code blocks into as few code blocks as possible. When developing test automation projects with Capybara, changing the default features in some cases can make your tasks easier. We reviewed the capybara configuration parameters for you.

 

1- app_host

      This parameter specifies the default host of the test automation project that you developed and performs your operations through this host. The app host does not change unless you overwrite it.

2- default_driver

      This parameter specifies which driver will run as the default driver on Capybara. This parameter has driver values ​​such as rack, apparition, selenium. Selenium is recommended for UI processes.

 



3- default_max_wait_time

      It specifies the maximum wait time for projects which use the Capybara framework. When a step of the test scenario can not get an expected result within the default_max_wait_time, the scenario raises an exception. Let’s say, Capybara.default_max_wait_time = 10 seconds and you’re expecting “Sign Up” button on the current page. With this configuration, Capybara waits to see the button for 10 seconds, maximum.


Capybara.configure do |config|
    config.app_host = 'https://www.google.com'
    config.default_driver = :selenium
    config.default_max_wait_time = 10
end
    
	
And(/^click "([^"]*)" button on page$/) do |button|
   click_button(button)
end
    


4- default_selector

      In test automation, we grab elements using their specific values. Capybara allows us to manipulate the selector with these values.
      Capybara uses the CSS selector by default. You can change this selector based on your needs. You can see a list of all selectors here. If you want to change this feature one time, you can overwrite it.


Capybara.configure do |config|
	config.app_host = 'https://www.google.com'
    config.default_driver = :selenium 
    config.default_max_wait_time = 10 
    config.default_selector = :css 
end

5- exact

      It expresses precision and expects the elements to match exactly. It is 'false' by default.

6- ignore_hidden_elements

      This parameter allows you to exclude hidden page elements in your matching. It is false by default. If you use true, it gives an error when you want to check if the hidden item is on the page. On the other hand, if you use 'false,' you can also search for and verify items that do not appear on the page.



7- match

      With the Capybara match parameter, you can specify how to make a selection when there are multiple elements. It is "smart" by default.


find('.btn', match: first) # first matching element
find('.btn', match: one) # one matching element
find('.btn', match: prefer_exact) # single element exactly matching
find('.btn', match: smart) # the closest element to the truth,
which is certain or not. Dependent on 'exact' property.
(Capybara uses this by default.)



8- Capybara.save_path

      If you want to save any output while the test scenario is running, you can use this parameter to define the save path of these outputs. If you do not define this parameter, you must define the directory manually.

      In this example, the save_screenshot method will do the saves on the path specified here.


    Capybara.configure do |config|
    	config.app_host = 'https://www.google.com'
        config.default_driver = :selenium
        config.default_max_wait_time = 10
        config.default_selector = :css
        config.exact = :false
        config.ignore_hidden_elements = :false
        config.match = :smart
        config.save_path = "../screenshot"
    end
    


      Capybara is a web-based test automation software written in Ruby programming language that tests your applications by creating functional tests and simulating user behavior. Thanks to its ease, you can quickly develop test automation and simplify very complex structures.

 

Happy Tests!