• Page ......">

    Rspec 2 - class name inside the tag

    I have the following html code:

    <ul class="nav"> <li class="active"> <a href="/users/page">Page</a> </li> ... </ul> 

    I want to make sure that I have a li tag with the class "active" and inside this text.

    So I tried:

     response.should have_selector( "li", :class => "active" ,:content => "Page") 

    And this does not work, I get this error:

     Failure/Error: response.should have_selector( "li", :class => "active" ,:content => "Page") expected following output to contain a <li class='active'>Page</li> tag: 

    How can I solve my problem?

    +6
    source share
    3 answers

    I solved this:

     response.body.should have_selector( "li.active") do have_selector('a', :content => 'Pages') end 
    +3
    source

    I think you can add a class to the css selector. Sort of:

     response.body.should have_selector( "li.active", :content => 'Page') 
    +9
    source

    We had similar problems with testing Watir, in that: class matcher - exact match of strings; not like a CSS class selector at all.

    Our solution was to switch to the xpath selector, for example:

     response.should have_selector("li", :xpath => 'contains(@class, "active")') 

    Disclaimer: I was not able to verify the above code; I am just showing how we used 'contains' against the @class attribute to match partial class lines.

    0
    source

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


    All Articles