How can you fill a table cell using Selenium? - c#

I'm trying to fill a table using the selenium drivers and all the documentations I could find only shows how to retrieve data from the cells. I'm able to access my table cells using:
var rows = Driver.FindElement(By.Id("Products")).FindElements(By.XPath("id('Products')/tbody/tr"));
var cells = tableRows[1].FindElements(By.XPath("td"));
But I couldn't find any way to update the data that's in it. The "Text" property only has a Get method and the SendKeys() function doesn't work. How can I edit the cell's value?
As a side note, my cell contains an html "input", I've tried to access it with the FindElement function of the cell but for some reasons it cannot find it.

Generally speaking, SendKeys should work if the cell indeed contains the input element. But because you're also saying that you fail to find the input element, I suspect that the input element does not exist in each cell all the time. You should probably first click on the cell in order for the input element to appear on that cell. You should be able to verify it using the Dev Tools if you inspect the element before clicking it.

IWebElement doesn't provide a method to change text, but you could use a little bit of JS - something little like :
((IJavaScriptExecutor)Driver).ExecuteScript("document.getElementByXXXXX.innerHTML = "VALUE";");
https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IJavaScriptExecutor_ExecuteScript.htm

Related

EPPLUS: how to check if all cell are valid?

I'm using Excel's data validation lists, checking if the cell value is into a dynamic range (the cell values of another column).
The problem is that if I add a value into the source range (eg. "A"), I can input that value on the cell with data validation. But then, if I change the source (eg from "A" to "B") the cell with data validation is still "A".
In Excel there's a button that sets a red circle around invalid data, but client does not always check if everything is good.
Can I perform that check with EPPLUS?
The documentation doesn't suggest that this check can be performed by EPPlus:
https://www.epplussoftware.com/Developers/Features mentions "Create, read, modify, delete Data validations" and links to sample code in their wiki, which is all about designing the sheet and inspecting its design, no content validation.
I've searched the source code and looked at ExcelDataValidation.cs and its various sub-types. Their "Validate" methods only check if the validation is properly defined according to Excel's rules.
To achieve what you're asking for, you'll have to use EPPlus to get the validation from the cell, interpret it depending on its type and evaluate the cell content. There is an example that touches some of the cases and could give you an idea where this is heading.

In DataGridView how to enable and disable bitmap Image

I have DataGridView with Four columns.
1st column contains "Text",
2nd, 3rd and 4rt column contains Bit Image.
Now this image are conditionally get rendered inside the columns and if condition is false it will contain "null" i.e. nothing in that column. However this blank column is part of Tab key, means when user navigate using Tab key in the grid, cursor traverse across all the column irrespective of column contains anything or not in it.
I would like to traverse cursor only to the column which contains value with in it. So how can I achieved this?
Following are the approaches I tried -
1. I filled all the column with respective images by removing the condition. now all the columns have some image in it, but I am unable to Enable/Disable those image conditionally. and because of that this all images are actionable and user can perform the operation which it should not. And when I applied Visible true/False so that it will apply across all rows for that column index and resultant the valid cell also invisible.
2. I placed button with in that column and tried to applied that bit image to that button and tried to enable/disable button as button has the property. but Bit Image is not getting applied to button and when I assign gridCell.value = btn; it will throw error since it is not text it is complete control. So not valid approach.
3. Using ProcessCmdKey() Method group, but not acceptable approach, though it has started performing as expected.
Please find the screen shot for more detail.enter image description here
Layout images and behaviord0sAI.png

Turning individual cells of DataGridView into Combobox

Environment: Web Development in Visual Studio
Language: ASP.NET
I'm afraid I already know the answer, but it can't hurt to ask. I have an application that gets data from SAP and can return specific results or a list of possible results for each row of a DataGridView. So the user might enter "abcd" and get "1234" as a result or he might get "1234 or 2345 or 3456" as a result. For each row I would like to have each column where multiple results were returned as a combobox. The rest should stay fixed though, since having the table littered with comboboxes that only have one result would be annoying.
Is there any trick to do this? I know you can "convert" an entire column into a column of comboboxes using DataGridViewComboBoxColumn like in this blog:
http://mytactics.blogspot.de/2014/01/convert-datagridview-column-to-combo-box.html
.Is there any way to have a combobox only for specific cells though? I know I could put a combobox next to each row of the table and let the user choose from that one or I could have a combox in every single row and maybe lock the ones with only one result. I'm not happy about either of those solutions though.
You can try and add a ComboBox to yourDataGridViewCell.Controls and make the label.Visible = false; or remove it. It would be something like:
yourDataGridViewCell.Controls.Clear();
yourDataGridViewCell.Controls.Add(new ComboBox());
yourDataGridViewCell.Controls[0].DataSource = yourList;
//set the value member etc.

ASPX GridView edit clicked cell only

I am binding data in a DataTable to a GridView.
The result is a simple table with 5 columns and 3 rows.
What I want to achieve is:
If an user clicks on a cell in the GridView, the selected cell should become editable (with a textbox). In that textbox the user should have to possibility to enter new value and save it to the database.
What I've achieved so far is:
I followed this tutorial: http://www.dotnetlogix.com/Article/aspnet/80/How-to-make-GridView-Individual-Cells-Selectable.html
I managed to make the cells clickable, when clicking the cell the background color becomes red:
With that tutorial I also managed to get the ColumnIndex and RowIndex.
So I was able to do: GridView.SetEditRow(RowIndex);
Then my result became:
This is almost what I want, but not 100%. The SetEditRow edits the whole row, but I need to edit the clicked cell which is based on a row and column index.
Can anyone help me further please?
I've followed some other tutorials as well, but without result.
I tried this a few years ago and ran into a few issues, I don't recall all of them off the top of my head right now. What I ended up doing was using a repeater instead of a gridview to build a table. Since I'm writing the HTML I have more control over things. The table contained both a dig tag for the static value and a text input for the edit value. I used jquery to show and hide when needed. I then used ajax to post the changes back. This was a lot of work, but it ended up working out ok. Hope this helps.

Coded UI Test - Click Control Based On Other Things On Table Row

im making a Coded UI test in Visual Studio 2010 for a web application in C# and i wish to click a button in the 6th column of a table based on the inner text of column 1? is this possible?
So for instance i have a table with Names in column one and other info and then a button in column 6. All auto generated.
Im assuming if i can get the Row number from the cell with say "John Smith" in it i can then press the button for this row in column 6.
Any ideas? i have tried googling and looking at what parameters i can pass in but im coming up stumped.
Something based on the following may help.
Access the HTML table by copying code from that generated by the Coded UI recorder for an assertion or a click on the cells. You should have something like:
HtmlCell theTable = new HtmlCell(this.UItheWindow.UItheTable.UIItemTable);
Cells of the table can be accessed by adding properties such as:
theTable.FilterProperties[HtmlCell.PropertyNames.RowIndex] = "0";
theTable.FilterProperties[HtmlCell.PropertyNames.ColumnIndex] = "6";
UITestControl cell = theTable.Find();
HtmlCell wantedCell = (HtmlCell)cell;
The above might be used as the body of a method returning the value of wantedCell. The name should now be available as:
wantedCell.InnerText;
Accessing the button to be clicked should follow a similar approach.
Another approach uses the GetChildren method to traverse the table. Start by getting theTable as above. Then have something like
UITestControlCollection children = theTable.GetChildren();
Loop through the children checking row properties. When the required row is found, call the GetChildren of that row and get the loop through them to find the required column. Some points: You may need to loop on columns before looping over rows. You may be able to directly index into the UITestControlCollection for the rows and the columns rather than needing to loop and check values. Depending on exactly how the table was written there may be additional levels between the table and the wanted cells, so you may need to examine children, grand children, great grand children, great great ..., and so on.
I have a couple of extension methods that I use to tackle content in tables(Hand coding, rather than using the recorder) -
This extension method for a table gets the first row in the table that contains the requested text in one of its cells or controls
public static HtmlRow GetRow(this HtmlTable table, string cellContent)
{
if((UITestControl)table == (UITestControl)null)
throw new ArgumentNullException("table");
if(cellContent.Length > 80)
cellContent = cellContent.Substring(0, 80); //Our table cells only display the first 80 chars
//Locate the first control in the table that has the inner text that I'm looking for
HtmlControl searchControl = new HtmlControl(table);
searchControl.SearchProperties.Add(PropertyNames.InnerText, cellContent);
//Did we find a control with that inner text?
if(!searchControl.TryFind())
{
//If not, the control might be an HtmlEdit with the text
HtmlEdit firstTableEdit = new HtmlEdit(table);
//Get all of the HtmlEdits in the table
UITestControlCollection matchingEdits = firstTableEdit.FindMatchingControls();
//Iterate through them, see if any have the correct text
foreach (UITestControl edit in matchingEdits)
{
if(cellContent == ((HtmlEdit)edit).Text)
searchControl = (HtmlControl)edit;
}
}
//We have(hopefully) found the control in the table with the text we're searching for
//Now, we spiral up through its parents until we get to an HtmlRow
while (!(searchControl is HtmlRow))
{
searchControl = (HtmlControl)searchControl.GetParent();
}
//The control we're left with should be an HtmlRow, and should be an Ancestor of a control that has the correct text
return (HtmlRow)searchControl;
}
Once you're able to get the correct row, it becomes relatively easy to get the correct control in that row(Or the correct control in a given cell in that row)
In your example, you've got a button in the 6th column of the row. The button probably has some properties associated with it, but even without them if we can correctly limit our search it will still work. Assume our table is named UITableCustomers - Declare a new button and limit it to only the 6th(Index 5) cell in the row containing "John Smith"
Mouse.Click(new HtmlInputButton(UITableCustomers.GetRow("John Smith").Cells[5]));
Obviously, this call is going to fail if the given text doesn't exist in a control in the table.
Your question is not very clear to me but check out the documentation on jQuery.trigger
This method will let you emulate a clickevent. I hope this is what you need.

Categories

Resources