Testing a cucumber to test a parent with a specific class

I have an element with id: #postand this element has a parent <li>. Now I want to check if the <li>class has .currentor not. This step may look like

Then the element "post" with parent "li" should have class "current"

If any body can help me with step_definitions, it will be gr8!

+3
source share
3 answers

I used this, (which works):

Then /^"([^\"]*)" should have class "([^\"]*)"$/ do |id, parent_class|
  assert page.has_xpath?('//a[@id="'+id+'"]/..[@class="'+parent_class+'"]')
end
0
source

With webrat, you can find the css selector. Perhaps this solves your problem:

Then the element "([^"]*)" with parent "([^"]*)" should have class "([^"]*)" do |element_id,parent,css_class|
  response.should have_selector "#{parent} .#{css_class}" do |matches|
    matches.should have_selector element_id
  end
end

I have not tried this code, but it should work for your purpose.

+4
source

These suggestions should work for you, but you can also consider decoupling your selectors from your stories to make it easier to support your test suite and more expressive business value. For example: you can change the step to something like:

Then the post should be displayed as the current post

This is pretty easy to implement if you use Pickle :

# /features/posts/viewing.feature
Given the following posts exist:
  | post  | title             | slug              |
  | Lorem | Lorem Ipsum Dolor | lorem-ispum-dolor |
  | Hello | Hello World       | hello-world       |
When I go to the post page for post "Hello"
Then post "Hello" should be displayed as the current post

# /features/step_definitions/post_steps.rb
Then /^#{capture_model} should be displayed as the current post$/ do |post_reference|
  post = model!(post_reference)
  page.should have_css("li.current ##{dom_id(post)}")
end
+1
source

Source: https://habr.com/ru/post/1792773/


All Articles