Git Product home page Git Product logo

xpath-specs's Introduction

Build Status Test Coverage Code Climate

Xpath::Specs

An RSpec library to get better messages when matching XPaths

Installation

Add this line to your application's Gemfile:

gem 'xpath-specs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install xpath-specs

Usage

As an example, suppose you have website with dishes and receipes. You want to test your views (either in RSpec or Cucumber) to make sure that they contain parts you want them to.

Declare your page parts

In order to encourage reuse, create one or many custom page part definition files

# spec/support/knows_page_parts.rb

module KnowsPageParts
  def dish_panel
    Xpath::Specs::PagePart.new("the dish panel", "//table[@id='dish-panel']")
  end

  def dish
    # match a sub element of dish_panel
    dish_panel.with("a dish", "//tr")
  end

  def dish_with_name(name)
    # match a special dish
    dish.that("is named #{name}", "[td[contains(.,'#{name}')]]")
  end
end

Use them in your specs

With these page parts definitions in place, you can now use them to test your views. Suppose this is the html for your view :

<html>
 <head></head>
 <body>
  <div>
   <table id="dish-panel">
    <tr><td>Pizza</td>...</tr>
    <tr><td>Cheese Burger</td>...</tr>
    ...
   </table>
  </div>
 </body>
</html>
  • You can test that your view contains the dish panel :
expect(html).to contain_a(dish_panel)
  • You can test that it contains at least one dish
expect(html).to contain_a(dish)
  • You can also test that it contains a pizza
expect(html).to contain_a(dish_with_name("Pizza")
  • Eventually, if you try to search for a dish that is not there :
expect(html).to contain_a(dish_with_name("Grilled Lobster")

You'll get a nice error message :

expected the page to contain a dish that is named Grilled Lobster (//table[@id='dish-panel']//tr[td[contains(.,'#{name}')]])
       it found a dish (//table[@id='dish-panel']//tr) :
          <tr><td>Pizza</td>...</tr>
       but not a dish that is named Grilled Lobster (//table[@id='dish-panel']//tr[td[contains(.,'#{name}')]])

Testing from Cucumber

Just use the 'page' in place of your view :

expect(page).to contain_a(dish_panel)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

xpath-specs's People

Contributors

philou avatar

Stargazers

Wilson Silva avatar Abe Voelker avatar

Watchers

 avatar  avatar

xpath-specs's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.