Soritng Ascending and descending order after clicking on table header? - sorting

I am using Selenium WebDriver with Java.
I have a table where I have to click on header of each column and want to validate whether sorting functionality is working correctly or not. I need to check for both Asc and Desc order.
So I have to take the count of the table first and the get the header text so that I click on each col 1-1 and then need to validate the sorting.
How can achieve my expected result. Below is the HTML source code:
<div>
<table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_gvClinicalTrait" style="border-color:appworkspace;border-collapse:collapse;">
<tr class="gridTitleBar">
<th scope="col">Date Collected</th><th scope="col">Clinical Trait Data</th><th scope="col">Source</th><th scope="col">Value</th>
</tr><tr class="gridBody" align="left">
<td>11/6/2008</td><td style="width:200px;">A1C</td><td style="width:200px;">d</td><td>6.00</td>
</tr><tr class="gridBody" align="left">
<td>9/17/2008</td><td style="width:200px;">BP</td><td style="width:200px;">e)</td><td>104/54</td>
</tr><tr class="gridBody" align="left">
<td>7/12/2008</td><td style="width:200px;">BP</td><td style="width:200px;">g</td><td>124/56</td>
</tr><tr class="gridBody" align="left">
<td>6/21/2008</td><td style="width:200px;">BP</td><td style="width:200px;">t</td><td>110/72</td>
</tr><tr class="gridBody" align="left">
<td>6/14/2008</td><td style="width:200px;">BP</td><td style="width:200px;">n</td><td>120/70</td>
</tr>
</table>
</div>

How can achieve my expected result. Below is the HTML source code:
you need to have list or array with expected result.
You need to create loop like this for every test:
List<string> expectedValues = new List<string>("12","13","14");
int expectedNumber = expectedValues.Count;
int tdNumber = 2;
for (int i=1; i< expectedNumber; i++)
{
string result = driver.Find(by.xpath("//div[#id ='ctl00_ContentPlaceHolder1_gvClinicalTrait']/tr["+ i +"]/td["+tdNumber+"]")).Text;
if (result != expectedValues[i])
Assert.Fail("Wrong value!")
}
I didnt try this code, but even if its wrong it's will be pretty close to code that you need.

Related

Unable to click on Select link present in last column of web table

I want to search Seller and have to click on select link for selected one. When I type seller name, it shows only record for selected seller.
I tried with following code, its not working. Can anyone please help
cy.get('input[name="search"]',{ timeout: 10000 }).type(this.data1.vehicle1_seller1)
//cy.wait(6000)
Cypress.config('defaultCommandTimeout', 10000);
cy.get('td[class="span-3"] div').each(($el, index, $list) => {
if ($el.text().includes('STB002')) {
// cy.contains("Select").eq(index).click()
cy.get('.span-1-5 > div > a > span').contains('select').eq(index).click({force:true})
}
}
this is the DOM structure >
<table>
<tbody>
<tr class="even">
<td class="span-3">
<div title="06V001">06V001</div> == $0
</td>
<td>
<div title="06 Vauxhall Ormskirk">06 Vauxhall Ormskirk</div>
</td>
<td class="span-1-5">
<div>
<a id="link57" href="./wicket/page?7-1.-seller-table-body-rows-10-cells-3-cell-link">
<span>select</span>
</a>
</div>
</td>
</tr>
<tr class="odd">
</tr>
<tr class="even">
</tr>
</tbody>
</table>
The HTML table is set out in rows and cells, exactly as you see it on the screen.
Your test is searching for the cell containing the text, but really you want to search for the row containing the text, then get the select button of that row.
The basic test would be
cy.contains('tr', 'STB002')
.within(() => {
// now inside the row
cy.contains('span', 'select').click()
})
The next problem is the car STB002 isn't on the first page, so you won't find it straight after loading.
Maybe use the search box to load that row (as you have in one screen-shot). I can't say what that code is, because the DOM picture doesn't include the search box.

Selenium performance in converting an HTML table into a .NET DataTable

Using Selenium with VB.NET using the ChromeDriver. I am using XPath to get a collection of TR nodes and then iterating through each row to get the TD data.
Source looks like this
<table width="100%" border="0" cellpadding="2" cellspacing="1">
<tbody>
<tr bgcolor="#E7E7BD">
<td class="content">RUSSIA</td>
<td class="content">ABCD</td>
<td class="content">13-APR-18</td>
<td class="content">26-APR-18</td>
<td class="content"> 01234567</td>
<td class="content"/>
<td class="content"/>
</tr>
<tr bgcolor="#E7E7BD">
<td class="content">ZURICH</td>
<td class="content">XYZS</td>
<td class="content">09-NOV-18</td>
<td class="content"/>
<td class="content"> 98765432</td>
<td class="content">Z</td>
<td class="content">DAA</td>
</tr>
</tbody>
</table>
I use this to get the rows which works well and returns some 9500 rows:
Dim trNodes As IReadOnlyCollection(Of IWebElement) = driver.FindElements(By.XPath("//tbody/tr[preceding-sibling::tr/td/a/#name='MAIN' and following-sibling::tr/td/a/#name='APPX']"))
Then I iterate through each HTML row and add it to a .NET DataTable
For Each elem As IWebElement In trNodes
'get the cells in the row (ie the TD elements in the TR element)
Dim webCells As IReadOnlyCollection(Of IWebElement) = elem.FindElements(By.XPath("td"))
'create a new DataRow and add data
Dim row As DataRow = dt.NewRow
'now cycle through all the fields and add them to the DataRow
For i As Integer = 0 To webCells.Count - 1
row(i + 1) = webCells(i).Text
Next i
'add the DataRow to the DataTable
dt.Rows.Add(row)
Next elem
Currently it is working as designed BUT it takes a very long time to process. 9500 rows with 7 fields is taking about an hour and a half to insert into a DataTable. Is this expected performance? Is there something I can do to speed up performance?
From what I have gathered, Selenium is great at automation but parsing HTML is not a strong suit. So for the node parsing I am using HTML Agility Pack and that does it in under 5 minutes.

Fetch parent of a specific row in a table without iteration

Consider the below table structure contains many rows with multiple column values. I need to identify the parent of specific row, which has to be identified using the cell .
<table class = 'grid'>
<thead id = 'header'>
</thead>
<tbody>
<tr>
<td>
<span class="group">
<span class="group__link"><a class="disabledlink"">copy</a>
</span>
</span>
</td>
<td class="COLUMNNAME">ACE</td>
<td class="COLUMNLONGNAME">Adverse Childhood Experiences</td>
<li>Family Medicine</li>
<li>General Practice</li>
</td>
<td class="COLUMNSEXFILTER">Both</td>
<td class="COLUMNAGEFILTERMIN">Any</td>
<td class="COLUMNTYPE">Score Only</td>
</tr>
<tr>
<td class="nowrap" showactionitem="2">
<span class="group">
<span class="group__link"><a onclick="Check()" href="#">copy</a>
</span>
</span>
</td>
<td class="COLUMNNAME">AM-PAC</td>
<td class="COLUMNLONGNAME">AM-PAC Generic Outpatient Basic Mobility Short Form</td>
<td class="COLUMNNOTE"></td>
<td class="COLUMNRESTRICTEDYN">No</td>
<td class="COLUMNSPECIALTYID"></td>
<td class="COLUMNSEXFILTER">Both</td>
<td class="COLUMNAGEFILTERMIN">Any</td>
<td class="COLUMNTYPE">Score Only</td>
</tr>
<tr></tr>
<tr></tr>
</tbody></thead>
</table>
Likewise this table contains around 100 rows. I did the same using iteration and it is working fine.
Is it possible to find the parent of specific row without iteration?
You can use the parent method to find the parent of an element. Assuming that you have located a table cell, let's call it cell, you can get its row using parent and then the parent of the row with another call to parent:
cell.parent
#=> a <tr> element
cell.parent.parent
#=> the parent of the specific row - a <tbody> element in this case
Chaining multiple parent calls can become tedious and difficult to maintain. For example, you would have to call parent 4 times to get the table cell of the "copy" link. If you are after an ancestor (ie not immediate parent), you are better off using XPath:
cell.table(xpath: './ancestor::table')
#=> the <table> element containing the cell
browser.link(text: 'copy').tr(xpath: './ancestor::tr')
#=> the <tr> element containing a copy link
Hopefully Issue 451 will be implemented soon, which will remove the need for XPath. You would be able to call:
cell.parent(tag_name: 'table') # equivalent to `cell.table(xpath: './ancestor::table')`
There's no need for anything fancy, Watir has an Element#parent method.
You can use this one:
parent::node()
The below example will selects the parent node of the input tag of Id='email'.
Ex: //input[#id='email']/parent::*
the above can also be re-written as
//input[#id='email']/..
XPath tutorial for Selenium

Finding only required links on a webpage using xpath

I have a webpage that has more than hundred links on a webpage. I am trying to see if i can only store the required number of web links and store them in a array and run a loop for verifying data within each individual links.
The number of links are dynamic and change as per the user, so each time the page loads it has to find the specified links.
This is what i currently have under the for loop for each user:
List<WebElement> linksize = driver.findElements(By.xpath(obj.getProperty("totalbookings")));
int linksCount = linksize.size();
String[] linksENR = null;
linksENR = new String[linksCount];
System.out.println(linksCount);
if (linksCount > 0)
{
System.out.println("We have enrollment");
System.out.println(linksCount);
for (int i = 1; i <= linksCount; i++)
{
driver.navigate().to(linksENR[i]);
System.out.println("here");
}
}
else
{
System.out.println("No enrollment record found");
}
The xpath for the elements that i am trying to click for user 1 is and there is one element:
.//*[#id='006g0000007RH1V_00Nb0000009Yc1K_body']/table/tbody/tr[2]/th/a
The xpath for the elements that i am trying to click for user 2 is as follows and there are two elements:
.//*[#id='006g0000007RH1V_00Nb0000009Yc1K_body']/table/tbody/tr[2]/th/a
.//*[#id='006g0000007RH1V_00Nb0000009Yc1K_body']/table/tbody/tr[3]/th/a
This is what i am using to search with the xpath:
totalbookings = .//*[contains(#id, "7RH1V_00Nb0000009Yc1K_body") and contains(#tagName , "a")]
Output:
No enrollment record found
Is there way to uniquely identify only few chosen links with the xpath? Please assist.
Here is the HTML information for the element:
<table class="list" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="headerRow"></tr>
<!--
ListRow
-->
<tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}">
<td class="actionColumn"></td>
<th class=" dataCell " scope="row">
<a class=" firepath-matching-node" href="/a1Bg0000001ih6M"></a>
</th>
<td class=" dataCell CurrencyElement"></td>
<td class=" dataCell "></td>
<td class=" dataCell "></td>
<td class=" dataCell "></td>
<td class=" dataCell numericalColumn"></td>
<td class=" dataCell "></td>
</tr>
<!--
ListRow
-->
<tr class="dataRow odd last" onmouseover="if (window.hiOn){hiOn(this);}" onmouseout="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}" onblur="if (window.hiOff){hiOff(this);}"></tr>
</tbody>
</table>
I would do it more like this:
List<WebElement> pl = driver.findElements(By.xpath(".//a[contains(#id,'totalbookings')]"));
if (pl.size() > 0)
{
System.out.println("We have enrollment. Looping through links...");
for ( WebElement we : pl )
{
we.click();
System.out.println("Clicked link: " + we.toString() );
thisPageObject.verifyPageLoaded();
driver.executeScript("window.history.go(-1)");
org.openqa.selenium.browserlaunchers.Sleeper.sleepTight(1000);
}
} else {}
System.out.println("No enrollment record found");
}
I guess contains(#tagName , "a") is intended to mean that the current element has a descendent element a. But it actually means that the current element has an attribute named tagName, and this attribute's string value contains the letter "a".
This says to me that you are trying to do something moderately complex with a tool that you really know little about. There's no shame in not knowing XPath, but a few hours spent learning the fundamentals will spare you many hours of trial and error and trying to communicate your problem on web forums.
To do what I think you intended to do, you can use the following XPath expression:
.//*[contains(#id, "7RH1V_00Nb0000009Yc1K_body")]//a
That is if you're trying to select the links themselves. If you're trying to select the element whose id you're testing, as your totalbookings XPath expression implies, you could do
.//*[contains(#id, "7RH1V_00Nb0000009Yc1K_body") and .//a]
but that would not select the links, which was your stated goal.
Also, both of the above look for descendants of the current element (whatever that is). With driver.findElements() you don't seem to have a current node, so it would make more sense to remove the . from the beginning of the XPath and just use //*... to make it clear that you're looking for nodes anywhere in the web page, not just in some chosen subtree.

How can I create a dynamic image source with MVC3 and Razor?

I have a view that attempts to build a table off of the passed in Model. I'd like to generate an image with a source that is changed based on the iteration of a loop but can't seem to get it to work. I can do this in JQuery but I would like to do it with razor. I have the following table body:
<tbody>
#{for (var ix = 0; ix <= Model.Value.Count - 1; ix++)
{
<tr>
<td style="width: 10%"><img src="http://gmap.com/marker" + #ix + 1 + ".png" /></td>
<td style="width: 75%">#Model("fullname")</td>
</tr>
}
<tbody>
That first TD is the problem area. I attempted it but... Thanks for the help.
Since it is razor you do not need to add the rendered output to your url. Try this:
#{for (var ix = 0; ix <= Model.Value.Count - 1; ix++)
{
<tr>
<td style="width: 10%"><img src="http://gmap.com/marker#(ix + 1).png" /></td>
<td style="width: 75%">#Model("fullname")</td>
</tr>
}
I haven't tested it but I believe it will fix your issue.

Resources