Search code examples
rubycapybara

Validating is there any different menu item with capybara


I will try to improve the question. How to validate if a different item is included in the menu: GAR? I used it below and it still didn't work page.all('select#tbm2').map(&:value).should == %w(Regras, GAR Regras, GAE Log Processamentos GAR/GAE)

<div id="tbm2" class="tb">
      <div id="TBH_tbm2" onclick="javascript:showMenu('tbm2','tbm2', true, true)" class="tbh">GAR</div>
      <div id="TBB_tbm2" class="tbb">
         <div onclick="sendEvent(0,event,this,searchImage(this),0,'','1370','Menu','','','.13.3','',''); " class="tbi tbiSel">
            <div>
               <div class="tbiIcon"><img src="r/std/static/pixel.gif" class="icon icon_config" draggable="false"></div>
            </div>
            <div>Regras GAR</div>
         </div>
         <div onclick="sendEvent(0,event,this,searchImage(this),0,'','1370','Menu','','','.13.4','',''); " class="tbi">
            <div>
               <div class="tbiIcon"><img src="r/std/static/collapse16.gif" class="icon" draggable="false"></div>
            </div>
            <div>Regras GAE</div>
         </div>
         <div onclick="sendEvent(0,event,this,searchImage(this),0,'','1370','Menu','','','.13.6','',''); " class="tbi">
            <div>
               <div class="tbiIcon"><img src="r/std/icons/listini64.png" class="icon" draggable="false"></div>
            </div>
            <div>Log Processamentos GAR/GAE</div>
         </div>
      </div>
   </div>
</td>

Solution

  • all('select#tbm2') is going to find all <select> elements with an id of tbm2, of which there are none in your HTML. Also .value gets the value property of an element but you have all div elements which don't have a value. If you wanted to use the approach you're trying for it would be something like

    page.all('#tbm2 .tbi', count: 3).map(&:text).should == ['Regras GAR', 'Regras GAE', 'Log Processamentos GAR/GAE']
    

    Another option would be to use a negative regex match, something like

    page.find('#tbm2').should_not have_css('.tbi', text: /^(?!Regras GAR|Regras GAE|Log Processamentos GAR/GAE)/)
    

    Another option would be to use the filter block that can be passed to most Capybara finder methods, like

    page.should_not(have_css('#tbm2 .tbi') { |node| ['Regras GAR', 'Regras GAE', 'Log Processamentos GAR/GAE'].none?(node.text) })
    

    Note: I'm using the should syntax because that's what you attempted in your question, but you really should be switching to RSpecs expect syntax nowadays