Why am I unable to click this selectable option in my settings? - c#

http://puu.sh/mVDoM/069787ca8d.png
I am using teststack white and I tried using:
ListViewRow presetter = p7window.Get<ListViewRow>(SearchCriteria.ByText("Presetter"));
presetter.Click();
TableRow presetter = p7window.Get<TableRow>(SearchCriteria.ByText("Presetter"));
presetter.Click();
ListItem presetter = p7window.Get<ListItem>(SearchCriteria.ByText("Presetter"));
presetter.Click();
I also tried using "Name row 1" and "row 1" for the string that is in the argument.

When you specify and call it to find by text, it is saying the "text" property, not that it might contain text with that value. TableRow doesn't usually have text property, but rather uses things like "Value" "RowIndex" etc.
Also, you should be aware of the place of the element in the tree, you might have to pass it to a parent control.
i.e.
List list = p7window.Get<List>(SearchCriteria.ByValue("Presetter"));
ListItem li = list.Get<ListItem>(SearchCriteria.ByValue("Presetter"));

Try to get AutomationElement for it.
***.GetElement(SearchCriteria.ByText("Presetter"));
If it's not null find a point by getClicablePoint() and perform click by mouse like Mouse.Instance.Click()
If the element is not reachable by any SearachCriteria - try to use native MS UI Automation:
Get the closest root control to yours.
Take Autoelement property from it.
Find whole children list of it like AutomationElement.FindAll(Treescrope.Descendals, PropertyCondition.TrueCondion) _
Find your element in Autoelements Array by any property you need.
Get invoke pattern from it and use Invoke()

Related

How can you fill a table cell using Selenium?

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

How to get the last selected item or its index in html multiple select using VB.Net?

As given on msdn about HtmlSelect.SelectedIndex Property
If the Multiple property is set to true, indicating that multiple items can be concurrently selected, the SelectedIndex property contains the index of the first selected item.
I am using SHDocVw.InternetExplorer APIs in my Vb.Net WinForms application and I am trying to get the text of last selected item by the user but SelectedIndex is not working for me in case of multiple HTML Select.
Here's my code
Private Sub onMouseDown(ByVal pEvtObj As mshtml.IHTMLEventObj)
Dim srcElement = pEvtObj.srcElement
If TypeOf (srcElement) Is IHTMLSelectElement Then
Dim DropDownElement = CType(srcElement, IHTMLSelectElement)
Dim elementValue = CType(DropDownElement.item(DropDownElement.selectedIndex), IHTMLOptionElement).text
End If
End Sub
Can anybody let me know how to do it?
Not getting any straight forward way, To achieve the requirement I have used the following way:
1) Store the selected values on MouseDown and KeyDown events.
2) Also add handler to the DropDownElement for its onchange event within above two mentioned events.
3) Now when the value changes it will trigger the method on which the handler is added. Here we can get the current Selected Elements and calculate the de-selected and the last selected element.
I know its not a cleaner way but working like a charm.
Grumbler85 is correct; you want the SelectedIndices property. It's a collection that contains the zero-based indexes of all currently-selected items in the HTMLSelect control.
HtmlSelect.SelectedIndices Property (MSDN Link)

Reference control using string with value of name

Is there a way in C# to reference a control, in my case a TextBox, by using the value of a string variable? I am using the code below to make a single method that multiple control can use for the 'LostFocus' event. The sender TextBox then needs to calculate results based on the contents of other TextBoxes. The problem is that there are about 12 rows of TextBoxes, and while this code works to reuse the event method, I can't think of a way to reference the correct boxes that are not the sender. All of the boxes have similar names (ex - miCellSaturation, miCellRecords, orSaturation, orRecords), so my thinking was that if I can isolate part of the TextBox name with a Substring command, and then concatenate that with another string to form the complete TextBox name, this would work. I can do all that, but I don't know of a way to use the concatenated string to reference that box. Would this require iterating through all the boxes until it matches the correct name?
TextBox box = (TextBox)sender;
string boxName = box.Name;
if(boxName.EndsWith("Saturation"))
{
}
Not sure to understand you problem correctly, but if you need to find the references to a particular type of control with its name ending with a predefined string, then you could use
var list = YourForm.Controls.OfType<TextBox>()
.Where(x => x.Name.EndsWith("YourString"));
foreach(TextBox t in list)
{
Console.WriteLine(t.Name);
......
}
This could work only if your searched controls are directly included in the controls collection of the form. If these textboxes are included in some control container then you need to apply these lines to the appropriate control container instead of the form

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.

Display Clicked Item with Knockoutjs

I am trying to display information about the selected item in a list as well as highlight the row that is selected. This is what I have:
http://jsfiddle.net/blankasaurus/qUydu/6/
1) When I try to set the CurrentDisplayThing = item it doesn't do what I want it to do (which is update the Display div with the selected item). I also tried $.extend(CurrentDisplayThing, item); Am I going to have to set each property individually or something? I have about 30 of them on my real project.
2) The other thing i wanted to figure out is how to go about highlighting the row that I just clicked the proper way using Knockout.
PS: For what I am actually doing I am using the mapping plug-in and both Things and CurrentDisplayThing are coming from my .NET model I pass in. I am assuming what I have in JS Fiddle mirrors the way mapping would set this up?
Here is a fiddle that solves both problems.
http://jsfiddle.net/johnpapa/6FCEe/
1) The selected item property is actually an observable (which is a function). So the value must be passed into the observable function like this:
self.model.CurrentDisplayThing(item);
2) Highlighting the row can be done through the css binding in Knockout. The css binding accepts an object with the name of the css class (ex: selected) and an expression (ex: isSelected). In the example below, when isSelected is true, the class will be applied, otherwise it is removed.
<tr data-bind="click: $root.selectThing, css: { selected: isSelected} ">
Check the fiddle out for the full example.
You can't just assign Knockout observables using code like
CurrentDisplayThing = item
And you can't $.extend them either, because in the end that does assignments too.
Instead you have to use the function syntax:
self.model.CurrentDisplayThing(item);
Similarly you have to use the function syntax when accessing:
self.model.CurrentDisplayThing().ID
Working example: http://jsfiddle.net/HUjau/1/

Categories

Resources