Selenium Webdriver - How to select record from grid view - selenium-webdriver

How can I select specific record from grid view using selenium webdriver (JAVA)
Suppose I want to select the highlighted record in this snapshot. How I can do that:

You should systematically break down the grid into sections, loop through each row's cell to find a match using a unique key and then select the row. In this case, the unique key is the Employee Number column.
In Java:
public void selectRow(String expEmpNo) {
// Get the grid
WebElement grid = driver.findElement(By.id("gpUsers"));
// Get all the rows
By locAllRows = By.xpath(".//*[contains(#class,'x-grid3-body')]//*[contains(#class,'x-grid3-row')]");
List<WebElement> allRows = grid.findElements(locAllRows);
// Loop through each row and compare actual emp. no. with expected emp. no.
for(WebElement row : allRows) {
// Emp No. is 4th column
By locEmpNo = By.xpath(".//*[#class='x-grid3-cell-inner x-grid3-col-4']");
// Get the Emp. No.
String actEmpNo = row.findElement(locEmpNo).getText();
// Compare actual vs expected
if(actEmpNo.equals(expEmpNo)) {
row.click(); // Select row
System.out.println("Selected row " + (allRows.indexOf(row) + 1) + " having Emp. No. " + expEmpNo)
break; // exit the for loop
}
}
}

I believe that you need something like this
WebElement element = driver.findElement(By.xpath("use the xpath here"));
Select oSelect = new Select(element);
oSelect.selectByVisibleText("enter the visible text you want to select");

Related

How to update 2nd column when enter value in st column wpf

have Two column First column have " Textbox" , and in 2nd column bind qty column by using DataMemberBinding="{Binding Path=Qty}".
Now what i need to do ,
if i enter 40 in column 1st then it will automatically get update in 2nd column.
means any update value in 1st column need to show in 2nd column.
withdout rebind Grid row.
var row = objPosItem.ParentOfType<GridViewRow>();
if (row != null)
{
row.Cells[6].Content = model.FinalPriceForShow.ToString();
row.Cells[6].HorizontalContentAlignment = HorizontalAlignment.Right;
}

How to fetch last row data from table using selenium webdriver

I have used this below code to get all row from table but I wanted only last row data
WebElement webtable=driver.findElement(By.id("requisitionsTable"));
List <WebElement> rowCollection=webtable.findElements(By.xpath("//*[#id='requisitionsTable']/tbody/tr"));
System.out.println("No. of Rows in the WebTable: "+rowCollection.size());
for(WebElement rowElement:rowCollection)
{
System.out.println("rowElement"+rowElement.getText());
List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
for(WebElement colElement:colCollection)
{
System.out.print(colElement.getText());
System.out.print(" ");
}
System.out.println("");
}
Use below cssSelector to get the last row data.
By lastRow=By.cssSelector("table#requisitionsTable tr:last-child");
driver.findElement(lastRow).getText();

Sort Group summary in Xaml Infragistics grid

I have requirement to sort group summary field.
Ex. I have 3 columns in the grid.
Step 1 : I have group by Id by dragging the Id column in Group by area.
Step 2: Add Sum,Count,Average on column.
Now i want to sort sum or count or average by clicking on that ,so that the whole grouped is sorted by sum like 100,200,300.
please help
The sort order is controlled by the GroupByComparer of the FieldSettings class and this can be accomplished by creating a custom IComparer for the field that is grouped. Note that grouping is actually also a sort so I am going to assume that you still want the default sort to happen when the column is first grouped.
In the following example group by records can be sorted by a single summary result when it is clicked on. This was accomplished by using a custom IComparer for the groups that sorts by the value of the tag if it is set and if not set falls back to the value of the group by record:
public class SummarySortComparer : IComparer
{
public int Compare(object x, object y)
{
GroupByRecord xRecord = x as GroupByRecord;
GroupByRecord yRecord = y as GroupByRecord;
IComparable xValue = xRecord.Value as IComparable;
object yValue = yRecord.Value;
if (xRecord.Tag != null)
{
xValue = xRecord.Tag as IComparable;
yValue = yRecord.Tag;
}
return xValue.CompareTo(yValue);
}
}
This is set on the grid using the following:
this.XamDataGrid1.FieldSettings.GroupByComparer = new SummarySortComparer();
Use the PreviewMouseLeftButtonDown of the grid to get the summary that was clicked on if there was one and set the tag of the group by records to be the value of that summary and refresh the sort of the grid:
void XamDataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
SummaryResultPresenter summaryResultPresenter =
Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof (SummaryResultPresenter), false) as
SummaryResultPresenter;
if (summaryResultPresenter != null)
{
GroupBySummariesPresenter groupBySummariesPresenter =
Utilities.GetAncestorFromType(summaryResultPresenter,
typeof(GroupBySummariesPresenter), false) as GroupBySummariesPresenter;
if (groupBySummariesPresenter != null)
{
SummaryResult summaryResult = summaryResultPresenter.SummaryResult;
int summaryResultIndex = summaryResult.ParentCollection.IndexOf(summaryResult);
foreach (GroupByRecord groupRecord in groupBySummariesPresenter.GroupByRecord.ParentCollection)
{
groupRecord.Tag = groupRecord.ChildRecords.SummaryResults[summaryResultIndex].Value;
}
this.XamDataGrid1.Records.RefreshSort();
}
}
}
Note that there are a few limitations in this example in that I haven't implemented any way to clear what summary is sorted so that is something that if desired would still need to be implemented by you. I also didn't include logic to change the sort direction and used the direction that the field is currently sorted by so if you also want to update the direction this will need to be added as well.

How to get the selected column count in datagrid

I want selected column's index of the DataGrid. For example, if I select the first column, I want the first column's index (index = 0).
I tried it in the DataGrid SelectionChanged event, but I can't seem to get the particular column index. If any one knows how to do it, help me with some sample code.
The DataGrid.Items property returns a DataGridItemCollection representing the DataGridItems in the DataGrid.
Each DataGridItem is representative of a single row in the rendered table. Also, the DataGridItem exposes a Cells property which represents the no. of tablecells (in other words, the columns) in the rendered table.
// Get the Row Count
int rowCount = myGrid.Items.Count;
// Get the no. of columns in the first row.
int colCount = myGrid.Items[0].Cells.Count;
I am assuming that you want the indexes of any columns that are selected. Here is the code I've come up with:
List<int> selectedColumnIndexes = new List<int>(dataGrid.SelectedCells.Count);
for (int i = 0; i < dataGrid.SelectedCells.Count; i++)
{
foreach (DataGridColumn column in dataGrid.Columns)
{
if (column.DisplayIndex == dataGrid.SelectedCells[i].Column.DisplayIndex)
{
if (!selectedColumnIndexes.Contains(column.DisplayIndex))
{
selectedColumnIndexes.Add(column.DisplayIndex);
}
}
}
}
Thus you will have a list of all the indexes of the columns currently selected. This question gives some nice clues in what direction to head here.
Obviously, if you want just the number of columns actually selected, then that value is simply selectedColumnIndexes.Count after the for loops have run through.

Datagridview Column width

I have problem related to datagridview in Winform.
I have a list of table names in my left panel. When I click on Table I show the table content in right panel. I am showing data in a datagridview by fetching data and assigning datasource to dgv.
I am setting following property to dgv.
dgTemp.Dock = DockStyle.Fill;
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dgTemp.AutoSize = true;
dgTemp.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dgTemp.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
dgTemp.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dgTemp.ReadOnly = true;
dgTemp.AutoGenerateColumns = true;
dgTemp.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgTemp.AllowUserToAddRows = false;
My problem is there can be any number of columns in Datasource I am assigning to dgv. So if there are very few number of columns (say 1 or 2) the dgv size stands very small and the empty space on right side give very ugly look.
I can't use auto autosizecolumnmode to fill since when there is more columns all columns get shrink and expanding columns dont give me Scroll at bottom
so My requirement is
All space in datagridview should be filled. (should cover all area)
When there is more column, there should appear scroll, so it give better look
are there any events or properties which I can use ??
Thanks in anticipation.
Try this :
dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
Update :
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
Update 2 :
int rows = dataGridView1.Rows.Count;
int columns = dataGridView1.Columns.Count;
if (rows < 5 && columns < 10)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
else
{
dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5
//you can have a horizontal scroll bar with this code :
dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column
}

Resources