https://github.com/philou/xpath-specs
An RSpec library to get better messages when matching XPaths
https://github.com/philou/xpath-specs
Last synced: 2 months ago
JSON representation
An RSpec library to get better messages when matching XPaths
- Host: GitHub
- URL: https://github.com/philou/xpath-specs
- Owner: philou
- License: mit
- Created: 2014-05-13T04:31:07.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-08-25T05:43:04.000Z (almost 11 years ago)
- Last Synced: 2024-10-09T13:07:30.713Z (9 months ago)
- Language: Ruby
- Size: 191 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://travis-ci.org/philou/xpath-specs) [](https://codeclimate.com/github/philou/xpath-specs) [](https://codeclimate.com/github/philou/xpath-specs)
# 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
```ruby
# spec/support/knows_page_parts.rbmodule KnowsPageParts
def dish_panel
Xpath::Specs::PagePart.new("the dish panel", "//table[@id='dish-panel']")
enddef dish
# match a sub element of dish_panel
dish_panel.with("a dish", "//tr")
enddef 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
Pizza...
Cheese Burger...
...
```
* You can test that your view contains the dish panel :
```ruby
expect(html).to contain_a(dish_panel)
```* You can test that it contains at least one dish
```ruby
expect(html).to contain_a(dish)
```* You can also test that it contains a pizza
```ruby
expect(html).to contain_a(dish_with_name("Pizza")
```* Eventually, if you try to search for a dish that is not there :
```ruby
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) :
Pizza...
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 :
```ruby
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