Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to use : Capybara get attribute

How to use :vCapybara get attribute

My recent project is builded with capybara and site_prism with selenium webdriver, i like it because it’s easy to use , nit have to bother too much about driver session , I can stay focus on what matters , writing new tests and improve the existing test suite performance and reusability.Sometimes in order to achieve the coverage that we want we need to create calculated results, one of them is to use element’s attribute , but first of all lets see how we can get element attribute with capybara.

Few examples How to get elements’s attribute with capybara:
   find('#my_element')['data_type']
        # => "some string"

        find('#my_input')['placeholder']
        # => "My placeholder value"

        find('a#example-link')['href']
        # => "http://example.com"

        find("//img[(@some_data_attribute='value')]")['src']
        # => "http://example.com/path/to/the/image.jpg"

        find('#element')['missing_attribute']
        # => nil

So now that you know how to get the element attribute with capybara you can basically extend your tests  , for example if you have an image on the page and you want to assert that image is present within a certain  context you can write something like this :

 within('#context') do
          att = find("//img[(@some_data_attribute='value')]")['src']
          assert_equal("http://example.com/path/to/the/image.jpg",att)
        end

You might run in a situation when you have a collection of elements and you need to check all of them.

 def foo
          page.all('locator')  do |i|
            att = i['attribute']
            puts att
          end
        end

More complex examples for getting element attribute with capybara

find('select', :name => 'attributes[attr_1][attr_2]') 
# or 
find_field('attributes[attribute_1][attribute_2]')

So once you know how to get it the world is your you can basically do whatever you want with that date and you can perform any kind of operations against it.

I strongly recommend you to read more details into capybara get attribute documentation .

Attributes are quite important in m day to day job , and they are quite useful to me , like an asset for an intel , for example I can’t live without manipulating attributes data when I do testing for google maps.

If you enjoyed this article and if you have any topics in your mind that you’ll like to read don’t hesitate to use the comments form bellow.

Happy testing!

The post How to use : Capybara get attribute appeared first on TestingRepository.



This post first appeared on Testing Repository - Creating Testing, please read the originial post: here

Share the post

How to use : Capybara get attribute

×

Subscribe to Testing Repository - Creating Testing

Get updates delivered right to your inbox!

Thank you for your subscription

×