How to do automated test with dynamically created controls? - c#

I'm trying to automate a web application, which generates controls in pages dynamically, and the web controls's ID are never the same.
I'm using Visual Studio 2012 Ultimate, MTM and Coded UI Tests. The application is being built with C# and Visual Studio 2012
The problem is that as the control's ID changes with every new run, the tests always fail.
Is there any way to solve this?

If the control id is changing you may use any other identifier to get the controls, like classname, tagname etc.
If classname is also dynamic then you can switch to tagname and get inner HTML of the control in order to confirm that the control is same as expected.
Then you would be able to work with the issue.

I am not very sure about this situation but I can given you an example and probably you can simulate this with the actual situation:
example: Like Loadrunner tool (used for performance testing), when it records the user action, it captures everything including all dynamic values of session id, so now if you replay your script it will fail because at time time of rerun, session ids are changed because of dynamic nature.
How we handle this this: We take the page source and mark the left and right boundary between which dynamic value appears and store the dynamic value in some variable so next time you run, you don't ned to worry about dynamic value.
Not to confuse you, but similarly, you can take the page source, mark the right and left string, store the value between these marker in a variable and handle that. For marking you can use some regular expression ... hope this helps. !!

Lets say you have a button on your web app, and you want to click on that button. In your situation where the ID is dynamic, I would not record the click on the button. You need to create the button dynamically, and add search properties on the fly. See code below.
// Create instance of a html button as a test step.
HtmlInputButton myButton = new HtmlInputButton(someUIMap.UISomeWindow.UISomeDocument);
// Search for the button in the specified UI document as a test step. Here we can search by the DisplayText property.
myButton.SearchProperties.Add("DisplayText", "Your buttons Display Text");
// Click the button if it exists.
if(myButton.Exists)
{
Mouse.Click(myButton);
}
I hope this helps.

Related

Call code behind c# function from javascript

I know this has been asked, but none of the numerous answers fit with my situation. So I will humbly ask if someone can walk me through this again gently.
Environment: .Net 4 web app using c#, javascript, jquery, sql server, and entity framework.
Question: I need to be able to allow a user to "quick add" a value into a table that is bound to a drop down box without losing values a user has already entered onto current page. I don't care if I have to do a postback or use jquery or use ajax, etc. I need the functionality first - prettiness later ... the scenario is described below if you care to keep reading. With all that being said, I would love for this to be smooth and not "clunky" for the user.
Scenario:
I have a page where the user is able to enter many pieces of information into textboxes, static drop down boxes, and drop down boxes that are bound to other tables that will all get combined and inserted into one table. Well, it never fails, a user could be filling in the page and when they get to one of the table bound drop down boxes - a value is missing or they haven't added something item yet. I would like to add a button or link or something next to that drop down box that would allow the user to "quick add" an item to the table that fills that drop down box and then refresh the drop down box so they can choose that new value from the list ... all while not losing the other data on the screen they already entered.
What I've tried:
-Tried WebMethod option. Currently, this web page calls some custom "bind" methods in the code behind to fill drop down boxes so they are not filled directly from objects from entity framework. So that means using a static method will not work for a solution because part of the static function would need to call the bind method to refresh the drop down box with the newly inserted values.
-Tried PageMethod option. Some of the other samples used Page Method settings, but my site uses master and content pages and the posts seem to state that the page method route was not going to work.
Make sense?
Add an UpdatePanel to the page. The update panel should wrap the drop down to be added, as well as the textbox/button for adding a new entry to that drop down. You would want to have a different UpdatePanel for each dropdown/button pair, if applicable.
When they click the button inside of the update panel you can read the textbox, add an extra item to the drop down, and send an update to the database. (I'd manually add the new value to the drop down rather than updating the database and re-binding, if possible, but it may not be.)
The magic of UpdatePanels will make sure that this is all asynchronous and so doesn't disturb the user working on the page.

Telerik mvc combobox: Prevent write value different from the list

I have a telerik combobox in mvc3 application and I want to prevent the user writing a value different from the list being loaded from the controller.
A thing that partial helped is to set the textbox input to be readonly but then:
1. The user can filter the list by the textbox.
2. The user can erase his selection.
Another thing that I thought about is: What the user types in the select is the value not the description, so I need it to prevent writing text different from the description while the value remains encapsulated.
Are you still looking to have the user be able to input custom text? If not you can use the DropDownList functionality as seen on this demo page.
If you still want the user to be able to type into the component but somehow have it aware of when they either misspell something, or type one character more than necessary, this can be extremely difficult. You would essentially have to have some clever JavaScript to be triggered with every key press and check the current value against a list. With users being pretty quick with typing, or their browsers being old, this can be very unreliable. Plus, a user can easily disable JavaScript at any time making this functionality obsolete.
I think your best options is to have it as is, where the user can type whatever they want. You can always have validation on the item they have typed and upon blur() or a POST (whatever fits your application) have a message appear to warn them of an invalid entry.

dynamic window focus MVC2

I am working on an MVC2 project, on a view we display a large data set which refreshes every minute with latest data..some specific records are updated every minute in this data set. I want the browser to focus on these specific records..Not sure how to use javascript focus() here dynamically...
any clue?
thanks,
You need to provide more detail as to exactly what you are attempting to do.
If you are attempting to focus just ensure each record has a unique identifier then you can focus on individual records.
Alternatively you could simply keep the focus at the top or bottom of wherever you are displaying them.
EDIT:
In that case I would suggest something like the following
var currEle = document.getElementById("Record123");
currEle.focus();
Suppose you know how to issue an Ajax call and return either partial view or JSON data and how to use that data on the client afterwards...
You can only focus to a point in the document when
there's only one change or
all changes are summarised together in the same document area
We're also not talking about focusing as in document perspective, but rather about focusing of users attention to document specific content (or part of it).
First option
You can always use something like jQuery.scrollintoview() plugin (link to blog post that describes the plugin here) that will scroll document to the record that changed and highlight it using jQuery UI effect highlight. Linked blog post also describes the purpose of visual animated scrolling instead of simple jumping within the document.
Second option
Put changes at the top and keep your document scrolled at the top when content gets updated. You can blink a few times some icon informing the user of the changed content in the changes area.

Events in an InfoPath 2010 Add-in

I'm currently trying to implement a simple Add-In for InfoPath 2010 Filler/Editor mode, which adds a few buttons on the "Insert" ribbon, which upon clicking inserts some "template" data from another source.
It works fine but the thing that's annoying me is that when the user has no field selected, it remains enabled while all of the other buttons on the insert toolbar somehow "know" that nothing can be inserted in the current context and are therefore disabled.
The MSDN documentation on how to interact with the underlying XDocument is perfectly adequate, but what I'm struggling to figure out is how to interact or get information from the editing UI.
This is what I'd like to have:
An event that gets fired when the user changes from field to field on the form
Then be able to tell what kind of field the user is currently entering data into (i.e. rich text, plain text etc), so the extra buttons can be enabled or disabled as needed.
I thought this would be fairly simple but I've spent nearly a day looking through everything I can find, and have come up empty!
Or have I completely missed the point here?
Several months later I can finally answer my own question. Not that anyone uses InfoPath filler, but just in case anyone does, here's my solution:
There's several points to my original question.
1) Event that gets fired when the user changes from field to field:
This turns out to be specified in the Ribbon Button XML as the "getEnabled" attribute. InfoPath calls the specified function each time it thinks the button may need to be enabled or disabled. On mine I specified: getEnabled="OnButtonGetEnabled", then implemented a small function:
public bool OnButtonGetEnabled(Office.IRibbonControl control)
{
ribbon.Invalidate();
return HaveRichTextFieldSelected(GetContextXPath());
}
in my case 'ribbon' is my instance of Office.IRibbonUI. Calling 'Invalidate()' is pretty important otherwise InfoPath only ends up calling this once.
2) How to determine the type of the field the user has selected.
I'm still not happy with my solution for this but at least I now have something that works.
I've written two functions:
1: GetContextXPath() which calls Globals.ThisAddIn.Application.ActiveWindow.XDocument.View.GetContextNodes(), builds an XPath string from the result (walking backwards through the DOM tree)
2: HaveRichTextFieldSelected() which checks if the specified XPath is of type 'rich' in the manifest (whose DOM tree is under Globals.ThisAddIn.Application.ActiveWindow.XDocument.Solution.DOM)
Anyway I'm not posting all of the code involved here as it's too much for an SO answer, but this should give someone with some common sense a clue as to how to implement this.

Ideas - storing jquery parameters for later use in partial view

Ok, let me try to clearly explain what I'm attempting to accomplish here.
Basically, I have a site that is using a liberal dose of jquery to retrieve partialviews into a consolidated 'single view'. So far so good - it all works great and is very performant.
However, I would like to have the ability to 'flag' (using a button) any such set and as a consequence of flagging it, add it to a functional area that I have dubbed 'active-tasks'. What I'd like to do is to be able to then goto that 'active-tasks' panel and see a range of ui tabs that represented the consolidated views that I had added. Clicking on any tab would then re-invoke that consolodated view afresh with the parameters that had been used at the time of flagging it. This would therefore mean that I'd have to store the parameters (?) for creating that consolidated view, rather than the generated html (this part i can do at the moment).
So, any thoughts on how to elegantly store the code required to generate the consolidated view on clicking a tab button - no pressure :)
cheers - jim
Actually, after a minimal amount of research, it looks like the newly updated .data() jquery method (with the ability to add an object to the payload) may work for the above.
http://api.jquery.com/data/
basically, this allows you to add hash type keyed data to an id element for use later, so in my scenario above i could simply attach the parameters required to invoke the action method that related to my consolidated view on the tab.
I'll let you know how i progress with this...
jim

Categories