How to verify the Literal text on the UI - java

Here is part of the UI where i have got some literal texts like
Opportunity Name Momentum Test (ID=AANA-19KVBE)
Account Account Internal
Owner Christopher Braden
Sales Order Region North America
Locale English United States
Currency US Dollar
The piece of code for the above is as follows
<table class="datatable" width="100%">
<tbody>
<tr class="oddrow">
<td>Opportunity Name</td>
<td class="wrap_content">
Momentum Test (ID=AANA-19KVBE)
<br>
<a target="_blank" href="http://www.example.com">View in Salesforce</a>
</td>
</tr>
<tr class="evenrow">
<td>Account</td>
<td class="wrap_content">
Akamai Internal
<br>
<a target="_blank" href="http://www.example.com">View in Salesforce</a>
</td>
</tr>
<tr class="oddrow">
<td>Owner</td>
<td>Christopher Braden</td>
</tr>
<tr class="evenrow">
<td>Sales Order Region</td>
<td>North America</td>
</tr>
<tr class="oddrow">
<td>Locale</td>
<td>English - United States</td>
</tr>
<tr class="evenrow">
<td>Currency</td>
<td>US Dollar</td>
</tr>
</tbody>
I need to retrieve these values individually, store it to compare it in a different page.
for example : i need to know "United States" is stored under "Location"
Please help me
Thanks & Regards
Kiran

You can read and store all this information in a Map -
Map<String,String> map = new Map<String,String>;
List<WebElement> list = driver.findElements(By.xpath("//*[#class='datatable']/tbody/tr"));
for(int i=1;i<=list.size();i++){
String key = driver.findElement(By.xpath("//*[#class='datatable']/tbody/tr["+i+"]/td[1]")).getText();
String value = driver.findElement(By.xpath("//*[#class='datatable']/tbody/tr["+i+"]/td[2]")).getText();
map.put(key,value);
}
In this way you can read all the information in the table and store it in Map to be used later. Let me know if this works for you.

Related

Selenium Webdriver Xpath: Find and compare the text of an element then click the link that's 4 rows after

I want to find and compare the heading in the table. Then click on the delete button that in the next 3 rows of the heading.
Example: Lets say a given input is Heading 2. I want to search Heading 2 and see if it exists then click on the delete button that is associated with 'Heading 2' (like 3-4 rows after).
So far this is the code that i came close to. The only problem is that it always click the first selection instead of where i want it to go.
String HeadingName = "Heading 2"
driver.findElement(By.xpath("//tr[contains(.,'" + HeadingName + "')]/tr[position()=4]/td[2]/div/a[2]")).click();
This is what the table and it's code looks like.
Heading 1
Name: Joe
Gender: Male
Options: Update | Delete
Heading 2
Name: Jenny
Gender: Female
Options: Update | Delete
<table>
<tbody>
<tr>
<th class="st-head-row" colspan="2">Heading 1</th>
</tr>
<tr class="even">
<td class="st-key">Name:</td>
<td class="st-val">Joe</td>
</tr>
<tr class="even">
<td class="st-key">Gender:</td>
<td class="st-val">Male</td>
</tr>
<tr class="even last-row">
<td class="st-key">Options:</td>
<td class="st-val">
<div style="white-space:nowrap;">
<a id="save" href="linkaddress">save</a>
|
<a id="delete" href="linkaddress">delete</a>
</div>
</td>
</tr>
<tr>
<th class="st-head-row" colspan="2">Heading 2</th>
</tr>
<tr class="even">
<td class="st-key">Name:</td>
<td class="st-val">Jenny</td>
</tr>
<tr class="even">
<td class="st-key">Gender:</td>
<td class="st-val">female</td>
</tr>
<tr class="even last-row">
<td class="st-key">Options:</td>
<td class="st-val">
<div style="white-space:nowrap;">
<a id="save" href="linkaddress">save</a>
|
<a id="delete" href="linkaddress">delete</a>
</div>
</td>
</tr>
</tbody>
</table>
Try this code , hope it will help you.
driver.findElement(By.xpath("//tr/th[contains(text(),'" + HeadingName + "')]//following::tr[3]/td[2]/div/a[2]")).click();
Thanks
Try next code with xpath
String headingName = "Heading 2";
driver.findElement(By.xpath("//th[text()='"+ headingName +"']/parent::tr/following-sibling::tr[#class='even last-row']/td[#class='st-val']/div/a[#id='delete']")).click();
If it works, for detailedinfo how this xpath is created look here - http://www.w3schools.com/xpath/xpath_axes.asp
I'd try and keep the XPath a bit more "semantic", e.g.
//tr[th = 'Heading 2']/following-sibling::tr[starts-with(td[1], 'Options')][1]//a[. = 'delete']
Here I'm looking specifically for the "delete" link in the next "Options" row, rather than something more fragile like "the second link in the next-but-two row".
I actually made an answer that surprisingly worked for me.
dUtil.findElement(By.xpath("//tr/th[contains(text(),'" + headingName + "')]/../following::tr[3]/td[2]/div/a[2]")).click();
I got every condition I wanted.
Searches for the heading that is similar to the given input heading
Clicks the link 'Delete' that is 3 rows bellow the searched element
Should work even if there are more than one batch of rows (batch meaning from heading to delete)
The batch doesn't have to be in order.

add a serial number while performing iteration in thymeleaf

I have a senario in which i must perform iteration on a list and display result as a grid.Also include serial number in it. Now what I did for serial number is given below
<div th:with="i=0">
<tr th:each="mydate:${data.Listdata}" >
<div th:with="i=${i+1}">
<td th:text="${i}">
</div>
//other codes
</tr>
</div>
but only 1 appears in all serial number.Can anyone help me with this?
You can use the index of the iteration status:
<tr th:each="item,iterator : ${items}">
<td th:text="${iterator.index}"></td>
</tr>
You can use the iterationStatus to begin with 1 instead of 0.
<tr th:each="item,iterationStatus : ${items}">
<td th:text=${iterationStatus.count}></td>
</tr>
This gives the table Sno. Starting with 1.
To get the index:
<tr th:each="item: ${items}">
<td th:text="${iterator.indexOf(item)}"></td>
</tr>
To get a numbering increment index by one.
<tr th:each="item: ${items}">
<td th:text="${iterator.indexOf(item) + 1}"></td>
</tr>

Alternative of element.children in jsoup

Alright I am having trouble with finding an equivalent of Element.children() because I have an Elements object...
What I am trying to do is download an html file (well I have done that...) and identify a single table row (, I've done that by using doc.getElementsByClass("emphasizedRowColor"); because the row I want has that emphasizedRowColor class and no other elements do). I just don't understand how to isolate the one Element in my Elements object RWTableRow.
Html:
<tr class="rwOdd emphasizedRowColor">
<td class="jewel" style="">
<div class="teamJewel" style="background-position: 0px -336px;margin: 0 0 2px 2px;"></div>
</td>
<td class="left" style=""> Detroit</td>
<td style="">18</td>
<td style="">9</td>
<td style="">5</td>
<td style="">4</td>
<td class="narrowStatsColumn cSrt" style="">22</td>
<td class="narrowStatsColumn" style="">9</td>
<td style="">45</td>
<td style="">48</td>
<td style="">3-2-4</td>
<td style="">6-3-0</td>
<td style="">3-3-4</td>
</tr>
I can figure out what to do once I actually get the table as an Element but oh boy I think I just need a fresh set of eyes to figure out what I'm doing...
Java:
Document doc = Jsoup.connect(url).userAgent("Mozilla").get();
Elements RWTableRow = doc.getElementsByClass("emphasizedRowColor");
As you can see, I'm in quite the pickle...
Elements is a standard java.util.List, you can simply call
Element e = RWTableRow.get(0);
And there you have it.

struts2 checkboxlist to show aligned label

I have a List<Bill> bills. Bill is a bean that has id, amount, date, billDescription.
I want to display a checkboxlist of Bill objects. So I use:
<s:checkboxlist list="bills" name="selectedBills"
listKey="id" listValue="displayLabel"/>
my Bill.getDisplayLabel() prints out: "amount date billDescription"
40.00 5/1/2011 Electric Bill
1005.25 6/12/2012 Gas Bill
Problem is it is not aligned. I want to customize my displayLabel so that the amounts align up, the dates align up, and the billDescription aligns up. It should display as:
[ ] 40.00 5/1/2011 Electric Bill
[ ] 1005.26 6/12/2012 Gas Bill
with a checkbox in front of each. Essentially I want to generate this code:
<table>
<tr>
<td><input type="checkbox" name="selectedBills" value="9" id="selectedBills-1"/></td>
<td style="text-align: right">40.00</td>
<td>5/1/2011</td>
<td>Electric Bill</td>
</tr>
<tr>
<td><input type="checkbox" name="selectedBills" value="9" id="selectedBills-2"/></td>
<td style="text-align: right">1005.26</td>
<td>6/12/2012</td>
<td>Gas Bill</td>
</tr>
</table>
How do I do this? The first column should have the checkbox, but the last 3 columns are 3 different parts of the label. I tried putting in the <td> code inside my Bill.getDisplayLabel() but struts escapes it so that the actual <td> tags show up!
Any ideas will be appreciated.
updated: I already know how to vertically display the checkboxes by customizing the freemarker templates.
How about using struts iterator? I haven't tested this code properly. But I hope it could give you some idea :
<table>
<s:iterator value="bills" var="bill">
<tr>
<td><input type="checkbox" name="selectedBills" value="${bill.id}" id="selectedBills-2"/></td>
<td style="text-align: right">${bill.amount}</td>
<td>${bill.date}</td>
<td>${bill.description}/td>
</tr>
</s:iterator>
</table>
This is what I ended up using in the end:
<s:iterator value="bills" var="bill" status="rowstatus">
<tr>
<td>
<input type="checkbox" name="selectedBills" value="${bill.id}"
id="selectedBills-${rowstatus.index}" />
</td>
<td style="text-align: right">${bill.amount}</td>
<td >${bill.serviceDate}</td>
<td>${bill.label}</td>
</tr>
</s:iterator>
and this works. The problem with this is, if I have other fields on the JSP that fails validation, this will not work. I'll have to put in additional jstl to make checked="checked" at the end of the <input type="checkbox" ...>

Java - Calling onclick method on webpage

<tr id="data_11">
<td colspan="7" class="row">
<table class="table-inner">
<tbody>
<tr>
<td class="col-companyname" onclick="getDetails('11', 'ARBN=BN5321177&CompName=A+ACTIVE+PLUMBING&CompStat=DRGD&Type=BUSN&BusIDType=Number&BusID=65057196&=')" style="cursor:pointer;">A ACTIVE PLUMBING</td>
<td class="col-companytype">Business Name</td>
<td class="col-acn"> </td>
<td class="col-abn"> </td>
<td class="col-arbn">BN5321177</td>
<td class="col-state"> </td>
<td class="col-docimage"> </td>
</tr>
<tr id="row_11" style="display:none;">
<td colspan="7">
<div id="div_11"></div></td>
</tr>
</tbody>
</table></td>
</tr>
I need to call the onclick method getDetails with the above given parameters and get the result. I tried doing this with jsoup but i couldnt get it to work.
Thanks in advance
Simple Answer : You can't call a javascript function from Jsoup. Jsoup is primarily meant for extracting and manipulating html data.
If you are interested in knowing the response to the request made in getDetails, then you can simulate the same request in Jsoup and get the response. But, If its a business logic in javascript without any server interaction which changes the DOM then you can kiss-off your idea.

Categories