Capybara - how to test presence of class attribute for nth element - capybara

I have a list like this
<ul>
<li></li>
<li .current></li>
<li></li>
</ul>
and if a user clicks on an li it adopts the class "current". I want to test this in capybara but im wondering how to write the test. I want to be specific in the test by saying the second element should only have the class "current".
i know how to target the second element using xpath i.e //li[2] and i also know how target an element by class //li[#class="current"] but i dont know how to bring them both together to to write this test.

Is this what you want? :)
assert _the_li_element_you_found.node[:class] == "current"

Related

Capybara Selecting CSS classes via regexp Ruby

Is there a way to find all div's on a page via their css class using a regexp if the classes match the regex? Below is an example snippet:
<div class="runtime">...</div>
<div class="runtime2">...</div>
<div class="runtime3">...</div>
I was hoping there is a way to get all divs via a regex because there could be more div's I want to find following that class format on the page but they change on a page by page basis.
For that format you can use a starts-with, and contains attribute selector
all('div[class^="runtime"], div[class*=" runtime"]', minimum:1)
The second selector is for the case where there's another class preceeding runtime... in the element. For a more general case there is no built in way to use a regex for class matching, although you could get an array of all the divs and then filter that based on the class attribute yourself (not going to be very performant)

Thymeleaf th:if with th:each on the same html element

I am generating all menu link dynamically using Thymeleaf. I have written code which is working fine.
<ul>
<li th:each="menu : ${menus}">Home</span></li>
</ul>
My question is, how can I add a class (activeMenu) on li element if menu's value is equal to Home.
Usually you would insert this part in the element that you want to insert specific class, in your case span element:
th:class="${menu}=='Home' ? activeMenu"
or:
th:class="${menu}=='Home' ? activeMenu:''"
it should work like this:
<ul>
<li th:each="menu : ${menus}">th:text="${menu}">Home</span></li>
</ul>
I have not tried this specific condition, but it should work.
Hope this helps.

How to select from multiple CSS selectors with Capybara

Using RSpec and Capybara to test for the existence of an element within a div with class 'foo'.
<div class="foo">
<p>Text zzz</p>
Looking for element here
</div>
<div class="foo">
<p>Text aaa</p>
Element should not exist within this div.
</div>
There are many divs with class 'foo' on the page, and I can give them different ID's based on foo's ID in the database.
But I don't know foo's ID from within the test. And, I don't want to test the parent of the divs because an element should be present in one div and absent in another.
What is the best way to test for an element in this case?
If I understand the question correctly (and I'm not 100% confident I do), I think this should work:
el1 = find(:xpath, '//div[#class="foo"][./p[contains(.,"Text zzz")]]')
el2 = find(:xpath, '//div[#class="foo"][./p[contains(.,"Text aaa")]]')
There's probably a slightly simpler way to do this using css instead of xpath, but I've found that this works for this type of situation. (Note: I haven't actually tested this code.)
parent = find("p[text()='zzz']").find(:xpath,"..")
within parent do
...
https://github.com/jnicklas/capybara/pull/505

Prototype: select the first of two childs

I'm implementing a functional test in Ruby on Rails. I need to make an assert_select on a web page, which has a content div, which was two <ul>lists. I need to check the number of <li> items in the first list. And I can't add any class or id to any of the <ul> list.
So my selector would look like this:
assert_select 'content ul<something_here_to_specify_the_first_one> li', <number_of_li_elements
However, I can't find the way to do that 'select the first one of the ul elements'. I've taken a look at http://content.labnotes.org/assert_select/assert_select.html but haven't been able to figure out which one is the right for me.
Cheers!
In order to select the first ul element, do:
content ul:first-child ...

Smarty - same template different content

What is the strategy in smarty for using different variables each time a template is included in another template?
Here is what I mean.
I have a smarty template that creates a simple navigation list.
<ul class='linkList'>
<li>
<h3>{$title}</h3>
<ul>
{foreach $links as $d}
<li><a title='{$d...}' href='{$d....}'>{$d.text}</a></li>
{/foreach}
</ul>
</li>
</ul>
I want to include it a number of times in my main template and each time pass it different values. Im not sure what strategy to use to do this.
If I assign variables in my php file like this
$smarty->assign('links',array(.....);
$smarty->assign('title','My first link list');
$smarty->assign('links',array(different values);
$smarty->assign('title','My second link list');
and then include the template twice i will just get the same list twice with the second lot of values.
The {include} tag allows you to pass variables in the call:
{include 'linklist.tpl' title="Sample Links 1" links=$link_array1}
{include 'linklist.tpl' title="Sample Links 2" links=$link_array2}
Otherwise, I'm pretty sure you can use either {assign} or the short form of assign ({$var=value}) before including the template.

Resources