How to get Ajax response in TestMethod CUIT - asp.net

I have my test method in a test class. I use Coded UI.
Everything I tested so far seems good. Now I want to await a Ajax call on clicking the Save button, but I don't know how.
Is it possible to do that? And if my Ajax response is above 500ms the test should fail.
This is my sample class:
[TestInitialize]
public void TestInitialize()
{
string serviceName = "xxxxxxx.exe";
var binPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)));
string servicePath = #"\ServiceProjects\vvvvvv\bin\Debug\";
string absolutePath = String.Format("{0}{1}{2}", binPath, servicePath, serviceName);
Process.Start(absolutePath);
BrowserWindow.CurrentBrowser = "ie";
BrowserWindow.Launch(baseAddress);
}
[TestMethod]
public void ManagerTestMethod()
{
this.UIMap.ManagerTestMethod_MobilePrefix();
}
[TestCleanup]
public void TestCleanup()
{
this.UIMap.TestCleanup();
}
And in this ManagerTestMethod_MobilePrefix() method I click on the save button:
public void ManagerTestMethod_MobilePrefix()
{
#region Variable Declarations
HtmlDiv uISljedećizahtjevNoPane = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument.UISljedećizahtjevNoPane;
HtmlButton uIItemButton = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument.UIItemButton;
HtmlCustom uIItemCustom = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument1.UIŠifarniciCustom.UIItemCustom;
HtmlHyperlink uIMobilniprefiksHyperlink = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument1.UICodelistsSubMenuCustom.UIMobilniprefiksHyperlink;
HtmlDiv uIItemPane = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIAps_mng_mobilephonepPane.UIItemPane;
HtmlSpan uIItemPane1 = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIAps_mng_mobilephonepPane.UIItemPane1;
HtmlEdit uIApsmngmobilephonepreEdit = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIApsmngmobilephonepreEdit;
HtmlEdit uIApsmngmobilephonepreEdit1 = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIApsmngmobilephonepreEdit1;
HtmlEdit uIApsmngmobilephonepreEdit2 = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UIApsmngmobilephonepreEdit2;
HtmlButton uISačuvatiButton = this.UIAddikoAPSInternetExpWindow.UIAddikoAPSDocument2.UISačuvatiButton;
#endregion
// Click '+ Sljedeći zahtjev + No' pane
Mouse.Click(uISljedećizahtjevNoPane, new Point(1651, 234));
// Click '+ Sljedeći zahtjev + No' pane
Mouse.Click(uISljedećizahtjevNoPane, new Point(1778, 308));
// Click '+' button
Mouse.Click(uIItemButton, new Point(51, 23));
// Click custom control
Mouse.Click(uIItemCustom, new Point(48, 30));
// Click 'Mobilni prefiks' link
Mouse.Click(uIMobilniprefiksHyperlink, new Point(87, 18));
// Set flag to allow play back to continue if non-essential actions fail. (For example, if a mouse hover action fails.)
Playback.PlaybackSettings.ContinueOnError = true;
// Mouse hover pane at (1, 1)
Mouse.Hover(uIItemPane, new Point(1, 1));
// Reset flag to ensure that play back stops if there is an error.
Playback.PlaybackSettings.ContinueOnError = false;
// Click pane
Mouse.Click(uIItemPane1, new Point(6, 5));
// Type 'test1' in 'aps.mng.mobilephoneprefix.details.Code' text box
uIApsmngmobilephonepreEdit.Text = this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEditText;
// Type '{Tab}' in 'aps.mng.mobilephoneprefix.details.Code' text box
Keyboard.SendKeys(uIApsmngmobilephonepreEdit, this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEditSendKeys, ModifierKeys.None);
// Type 'test1' in 'aps.mng.mobilephoneprefix.details.CoreCode' text box
uIApsmngmobilephonepreEdit1.Text = this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEdit1Text;
// Type '100' in 'aps.mng.mobilephoneprefix.details.Value' text box
uIApsmngmobilephonepreEdit2.Text = this.ManagerTestMethod_MobilePrefixParams.UIApsmngmobilephonepreEdit2Text;
// Click 'Sačuvati' button
Mouse.Click(uISačuvatiButton, new Point(52, 20));
}
The last step is crucial to me: Mouse.Click(uISačuvatiButton, new Point(52, 20)); in that step I am doing a Ajax call to my API controller in order to insert the data.
I want to be able to catch validations from my form, also I want to be able to catch the response from the Ajax call that is triggered by clicking on the save button.

I may have a idea you could try. I never had a situation like yours, so I can't confirm it will work.
You could provide a hidden control on that form with a value that will only be set if the Ajax call is completed. If you are able to find that control with your Coded UI test, you could run a timer for 500ms after clicking that save button.

Related

Xamarin Form passing parameters between page keep a hard link to it

I try the new Shell of Xamarin Form 4 for a small project.
I have a list of order, then someone chooses an order and start picking some inventory for this order with barcode. To be simple, I use 2 views and 2 viewmodel.
The process is:
1. User select an order the click a button "pickup"
2. I use ViewModelLocator (TinyIoc) to resolve the correct ViewModel of the pickup view
3. I call Initialize on the new ViewModel and pass some parameters needed. Like a list of items needed to be pickup.
4. Open the view in modal state.
I don't understand that if I change some qty in the list I pass on the second viewmodel, then hit the back button (modal view close). The quantity changed is now in the original viewmodel. How this is possible? I was thinking that passing parameter to a function do not share the same variable but just copy it??
Viewmodel of the first view (look at the Initialize function the List passed and the JobEnCours object)
private async Task GoPickup()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (this.CodeJob != null && this.CodeJob != "")
{
this.IsBusy = true;
PrepChariotSP3ViewModel vm = ViewModelLocator.Resolve<PrepChariotSP3ViewModel>();
await vm.InitializeAsync(this._jobEnCours, this.ComposantesPick.ToList()) ;
await Shell.Current.Navigation.PushAsync(new PrepChariotSP3Page(vm));
this.IsBusy = false;
}
});
}
the the Initialize code on the second Viewmodel (look I set the JobEnCours.Description=Test)
public async Task InitialiseAsync(Job jobEnCours, List<ComposantePick> composantePick)
{
Title = "Ceuillette: " + jobEnCours.CodeProd;
this._jobEnCours = jobEnCours;
this.ComposantesPick = new ItemsChangeObservableCollection<ComposantePick>();
foreach(ComposantePick c in composantePick)
{
this.ComposantesPick.Add(c);
}
jobEnCours.Description = "test";
So, If I do the back button, then in the first VM the JobEnCours.Description is now set to "test" how this is possible?!
Any idea?

Switching between dynamically added user controls showing old values

Edit (more brief):
I have a control "configuration" with a tree view on the left and a list of panels on the right to which i add my controls. These elements are placed inside a table and surrounded by an ajax panel.
Let's say i have a tree node cars with two elements car1 and car2. When i click an item i save the ID and Type of the selected tree node inside hidden fields to be able to load the right user control on Page_Init
protected void Page_Init(object sender, EventArgs e)
{
var type = this.Request.Form["ctl00$MainContent$ctl00$typeId"];
var id = this.Request.Form["ctl00$MainContent$ctl00$entityId"];
if (!string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(id))
{
this.LoadUserControl(type, id);
}
}
private void LoadUserControl(string type, string id)
{
if (Convert.ToInt32(type) != 0)
{
switch (Convert.ToInt32(type))
{
case (int)SkTopics.Product:
this.AddUserControl("../Modules/Product.ascx", this.Product, type, id);
break;
}
}
}
private void AddUserControl(string controlName, Panel panel, string type, string id)
{
// Init new user cotntrol
control = (BaseControl)this.LoadControl(controlName);
control.LoadEntityItem(Convert.ToInt32(id));
// Setting the user control ID
var replace = controlName.Replace("../Modules/", string.Empty);
string userControlId = replace.Split('.')[0] + id;
var targetControl = panel.FindControl(userControlId);
if (object.Equals(targetControl, null))
{
control.ID = userControlId;
control.DataBind();
panel.Controls.Add(control);
}
}
On LoadEntityItem the data for this car are collected from the database and the values are set as Text of textboxes. When i now switch to the second car, the new values are displayed.
Problem: I change the values inside the textboxes and press the save button to call my save method of the user control. From now on, when i switch to the car 1 again, the values for car 2 are still displayed, although the data for car1 are filled. tO see the data for car1 i have to switch to a complete different type of control and then back to the car1.
Another problem is, that i have a binary image inside the user control which Data i fill on init. When i swith to the the next car, where there i no image configured, i set the image data to null, but the image from the previous car i show. Onyl if i set a different image data the new image is displayed.
There is something else i noticed:
Inside the user control i call a javascript funtion but this one is only called on clicking the the save button. It seems that there is a different between the switch of the tree node and the save button click ?
ScriptManager.RegisterStartupScript(this, this.GetType(), "InitScrewPointsForDragging", "InitScrewPointsForDragging();", true);
I hope this was more brief than before.
It seems that the reason for the issue is some improper viewstate handling and after I disable the view state it is working correctly at my side. Here is the code that I updated to have it work at my side:
this.control.EnableViewState = false;
this.control.ID = userControlId;

Activity Indicator is not visible on xaml page in xamarin.forms?

I have an activity indicator on xaml page. Initially its IsVisible property is false. I have a button on page. When user click on button it calls a web service to get data. I change the value of IsVisible property to true before calling the service so that activity indicator starts to display on page and after successful calling of service I change its value to again false so that it doesn't show any more on page.
But it is not working. I know the actual problem. When we call the web service the UI thread gets block and it doesn't show the activity indicator.
How I can enable the UI thread when web service gets called so that activity indicator can show on page until we get the data?
Try making your webservice call into an async and await it.
Depending on how you've structured things you may have to use a TaskCompletionSource as the following example demonstrates.
In this example when the button is clicked, the button is made invisible, and the ActivityIndicator is set to IsRunning=True to show it.
It then executes your long running task / webservice in the function ExecuteSomeLongTask using a TaskCompletionSource.
The reason for this is that in our button click code, we have the final lines:-
objActivityIndicator1.IsRunning = false;
objButton1.IsVisible = true;
That stop the ActivityIndicator from running and showing, and also set the button back to a visible state.
If we did not use a TaskCompletionSource these lines would execute immediately after calling the ExecuteSomeLongTask if it was a normal async method / function, and would result in the ActivityIndicator not running and the button still being visible.
Example:-
Grid objGrid = new Grid()
{
};
ActivityIndicator objActivityIndicator1 = new ActivityIndicator();
objGrid.Children.Add(objActivityIndicator1);
Button objButton1 = new Button();
objButton1.Text = "Execute webservice call.";
objButton1.Clicked += (async (o2, e2) =>
{
objButton1.IsVisible = false;
objActivityIndicator1.IsRunning = true;
//
bool blnResult = await ExecuteSomeLongTask();
//
objActivityIndicator1.IsRunning = false;
objButton1.IsVisible = true;
});
objGrid.Children.Add(objButton1);
return objGrid;
Supporting function:-
private Task<bool> ExecuteSomeLongTask()
{
TaskCompletionSource<bool> objTaskCompletionSource1 = new TaskCompletionSource<bool>();
//
Xamarin.Forms.Device.StartTimer(new TimeSpan(0, 0, 5), new Func<bool>(() =>
{
objTaskCompletionSource1.SetResult(true);
//
return false;
}));
//
return objTaskCompletionSource1.Task;
}
You need to do your work in an asynchronous way. Or in other words: Use Asnyc & Await to ensure, that you UI works well during the call.
You can find more informations in the Xamarin Docs.
async and await are new C# language features that work in conjunction
with the Task Parallel Library to make it easy to write threaded code
to perform long-running tasks without blocking the main thread of your
application.
If you need further asistance, please update your question and post your code or what you have tried so far.

How to handle pressing any keys in the user control in visual studio vspackage

I added usercontrol (textbox) in code editor control. But textbox not handle any keys (arrow, backspace). Keys handle code editor only.
Example:
get IWpfTextView
var textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
IVsTextView textView;
textManager.GetActiveView(1, null, out textView);
var userData = (IVsUserData)textView;
if (userData == null)
return null;
Guid guidWpfViewHost = Microsoft.VisualStudio.Editor.DefGuidList.guidIWpfTextViewHost;
object host;
userData.GetData(ref guidWpfViewHost, out host);
return ((IWpfTextViewHost)host).TextView;
Add control with textbox
Canvas.SetLeft(_control, _view.ViewportRight - 255);
Canvas.SetTop(_control, _view.ViewportTop + 10);
(((System.Windows.Controls.ContentControl)_view).Content as Canvas).Children.Add(_control);
But there was a problem when processing keys, their intercepts (the main window as the editor),
keys are not processed. For example arrows, HOME and other.
If so add a textbox, then the letters are printed there. But the other key is handled by the editor, rather than this text box even backspace.
Who knows how to catch and handle correctly?

ASP.Net MVC 3 Controller Action and Open New Window

i have a controller and an action. this action is to save data into database. and now, i want when i submit a button, my controller do an action and open new window.
public ActionResult New(FormCollection collection)
{
data.Population_Code = collection["Countrys[0].CountryCode"];
data.Population_Desc = collection["Countrys[0].CountryDesc"];
data.Population_Grouping = collection["Countrys[0].CountryGroup"];
data.Population_Type = "CNTRY";
data.Population_Redudant = "N";
data.Population_Modified_At = officeCode.User_Office.ToString();
db.SaveChanges();
//example for new window
//window.open('/Report/New.aspx')
return RedirectToAction("index");
}
so my controller do an action and open a new window.
anyone can help me ?
thanks
Technically, this can be done by returning javascript that will open the new window.
However, most browsers will kill a new window called in this manner (i.e. popup blocker).
You would be better off, if possible, by opening the link to your action in a new window from the start;
#Html.ActionLink("New report", "New", "Report", null, new {target = "_blank"})
Edit
I can see from your action, that it is probably a form that creates the report; you can also use the attribute target='_blank' on a form as well.

Resources