Clicking on image using RSpec & Capybara - ruby-on-rails

I'm using Rspec and Capybara.
I am attempting to click an image, but cannot find a way to get Capybara to click the image. I'm not sure what to do, as I have in fact same class, alt and src.
Thanks
Example of html
<tr class="even">
<td class="gridData" style="text-align:center;">
<td class="gridData"> 221 </td>
<td class="gridData" route="default" reset="0" urlparams="users index edit {userID}" label="Username">
<td class="gridData">
<img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
</td>
</tr>
<tr class="odd">
<td class="gridData" style="text-align:center;">
<td class="gridData"> 222 </td>
<td class="gridData" route="default" reset="0" urlparams="users index edit {userID}" label="Username">
<td class="gridData">
<img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
In fact here is the source, which I want to test:
<img class="user-info-grid" alt="info" src="/themes/system/images/icon/16/information.png">
But as I said, I cannot find a way to get Capybara to click the image.
Any ideas?

You can use this:
page.first(".user-info-grid").click
This is use to find user-info-grid class and apply click function.
If multiple classes are comes with this name then the click will execute in first found class.

You can use that when looking for an id:
find("#the_id").click
Or for a class
find(".the_class").click

I put this and works:
find("img[src*='myphoto.jpg']").click

That's because your image is an image, not a link. Wrap it inside a link first, then click the link, like so:
<img ... />

Do I understand correctly that there's no <a> element because you're using a JavaScript onclick handler? If so, then you need to be aware that plain Capybara doesn't do JavaScript. You need to use capybara-webkit, Selenium, or something else similar.
But I'd advise you to put in the <a> element, and apply the JS handler to that. That will make it easier for user agents without JavaScript to handle (yes, there still are some!).
And Matthias is right: make your HTML valid.

Try using XPath, if not that then you can use either visit_to the source of the link, or you can use try help on http://railscasts.com/episodes/257-request-specs-and-capybara

Related

Decipher this XPath expression to get full href attribute

Is there a way I can get the full href attribute (https://studyacer.com/question/audit-and-assurance-services-444592) instead of a partial href? (https://studyacer.com/question/audit-and-) from this markup?
<td class="word-break">
<span class="label label-success">Due in 5 days</span>
<a href="https://studyacer.com/question/hey-greg-here-is-my-hrm522-discussion-444593">
<strong>hey Greg here is my HRM522 discussion</strong></a>
<small>"Auditing of Organizational Ethics and Compliance Programs" Please respond to the following:...
</small>
<br />
<strong>Business > Management</strong>
</td>
The XPath expression I have is this '//td[#class="word-break"]/a/#href' and it's just giving me a partial url.
The site uses absolute urls (if that helps).
Edit: I am using Scrapy to implement a basic crawler. When I run
response.xpath('//td[#class="word-break"]/a/#href')
I get the partial url.
For anyone with a similar issue. Turns out that running
response.xpath('xpath_expression')
gives you a partial url in Scrapy. Especially if the url is a long one.
For the full value use extract() at the end. Like this
response.xpath('xpath_expression').extract()

Showing the user a custom page after list add in Sharepoint 2007

After my user adds a new list item with my custom list page, Sharepoint 2007 redirects them to AllItems.aspx. I want them to go the the custom page ThankYou.aspx instead.
My buttos are the standard buttons generated by SharePoint Designer
<table>
<tr>
<td width="99%" class="ms-toolbar" nowrap=""><IMG SRC="/_layouts/images/blank.gif" width="1" height="18"/></td>
<td class="ms-toolbar" nowrap="">
<SharePoint:SaveButton runat="server" ControlMode="New" id="savebutton2"/>
</td>
<td class="ms-separator"> </td>
<td class="ms-toolbar" nowrap="" align="right">
<SharePoint:GoBackButton runat="server" ControlMode="New" id="gobackbutton2"/>
</td>
</tr>
</table>
I have seen several solutions that require adding 150 lines of JavaScript or changing the underlying SharePoint code, and I just can't believe that is the solution - I think either it is so simple that no one has written about it, or I am using the wrong search term to look for it.
I was just overlooking something simple.
The Source parameter on the form link will change the redirect for you. So if your custom form is at
https://myserver/lists/customadd.aspx
Then you can just add the source parameter and Sharepoint will redirect there on submit:
https://myserver/lists/customadd.aspx?Source=ThankYou.aspx

Conditional template in a table in dart web ui

I recently update Dart to Dart SDK version 0.7.1.0_r27025 and have not been able to get conditional templates inside tables to work. The code I have at the moment is:
<td template if="loaded">
<p>Loading</p>
</td>
<td template if="!loaded">
<div>...</div>
</td>
However, if I run that in dartium and inspect the DOM all I see is
<td></td>
<td></td>
and I get a RangeError in the code that was generated by build.dart.
Has something changed in how dart handles conditional templates in tables in the latest release?
I was able to fix this by doing
<td>
<template if="!loaded">
<p>Loading...</p>
</template>
<div id="my_id"></div>
</td>
It seems it is no longer necessary to use the template as an attribute in tables.

How to dynamically generate id in dataTable using ui:repeat tag

I am using ui:repeat tag which render images. I have five images i want that on each iteration my image get ids like image1, image2, image3.... imagen. I tried this but it is not working.
<div id="imageGallery">
<ui:repeat value="#{countryPages_Setup.images}" var="image" varStatus="status">
<tr>
<td>
<a href="javascript:void()" class="launchLink">
<p:graphicImage id="image#{status.index}" //problem
value="/resources/images/#{image}"
width="100"
height="100"
rendered="#{countryPages_Setup.renderImages}"/>
</a>
</td>
</tr>
</ui:repeat>
</div>
I also tried {staus.index + 1}. I also tried id= image#{1++} But it is also not working. How can i do it?
Thanks
You can use EL in the id attribute, but it has to be available during view build time. The <ui:repeat> however runs during view render time, it will reuse the same <p:graphicImage> to generate multiple HTML <img> elements. It doesn't run during view build time, so the id remains image.
If you replace <ui:repeat> by <c:forEach>, then it'll work as you intented. The <c:forEach> runs during view build time and it will generate multiple <p:graphicImage> components which will then each get rendered only once.
<div id="imageGallery">
<c:forEach items="#{countryPages_Setup.images}" var="image" varStatus="status">
<tr>
<td>
<a href="javascript:void()" class="launchLink">
<p:graphicImage id="image#{status.index}"
name="images/#{image}"
width="100"
height="100"
rendered="#{countryPages_Setup.renderImages}"/>
</a>
</td>
</tr>
</c:forEach>
</div>
You cannot use el expressions within the id attribute. It needs to be static. The ui:repeat itself generates a prefix to your id. You don't need to care about uniqueness.
So, for instance if you have an id="image", then the generated ids are
somePrefix:0:image, somePrefix:1:image, ...
But you can use variable in Javascript, for example:
<h:graphicImage ... onclick="top.location.href='http://blabla.com/imageclicked?id=#{status.index}'"/>
You can also use Variables with event handlers (since JSF 2.0):
<h:commandLink ... action="#{myBean.imageClicked(status.index)"/>
But your loop with c:forEach can cause problems. Be aware that the JSTL tags (everything that begins with c:) are not fully compatible to JSF. If you have luck, then they work as expected. But anyways the slow down the rendering engine, since the page gets processed multiple times by the JSF and JSP rendering engine.
Better use ui:repeat.
Just change <ui:repeat> to <c:forEach>

Grails - Custom tag inside a standard Grails tag

It is possible to call a Grails' tag inside another one using the following synthax.
<g:aContainingGrailsTag value="${aContainedGrailsTag(attr:'whatever')}"
Is it possible to include a custom tag into a Grails tag using the exact same syntax.
I am trying it this way:
<td class="${redOrGreen(number:'i')}"> </td>
but it does not work.
Any insight on this greatly appreciated.
In this specific case, you can do it like this:
<td class="<yourNameSpace:redOrGreen number='i' />"> </td>
or in general:
<td class="${yourNameSpace.redOrGreen(number:'i')}"> </td>
Only for tags within g: can be called without namespace prefix.

Resources