TestMethod in Coded UI Test - coded-ui-tests

I am creating a TestMethod in Coded UI Test that will access UI Control in my Windows Phone 8.1 apps. I used the async method to perform multi-thread tasking but I got the exception for my code like this:
[TestMethod]
async public Task CodedUITestMethod1()
{
XamlWindow.Launch("{556EE9D4-5640-4120-9916-44B1CA27352F}:App:556ee9d4-5640-4120-9916-44b1ca27352f_tpza89sffjg1j!App");
await ExecuteOnUIThread(() =>
{
MainPage mainPage = new MainPage();
Microsoft.VisualStudio.TestTools.UITest.Input.Point point = new Microsoft.VisualStudio.TestTools.UITest.Input.Point(mainPage.getX(), mainPage.getY());
Gesture.Tap(point);
}
);
}
and
public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action)
{
return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action);
}
but I got the exception message is: Test Name: CodedUITestMethod1
Test FullName: CodedUITestProject2.CodedUITest1.CodedUITestMethod1
Test Source: c:\Users\Thanh\Desktop\Calculator\CodedUITestProject2\CodedUITest1.cs : line 28
Test Outcome: Failed
Test Duration: 0:00:02.3440269
Result Message:
Test method CodedUITestProject2.CodedUITest1.CodedUITestMethod1 threw exception:
System.InvalidOperationException: A method was called at an unexpected time.
A method was called at an unexpected time.
Result StackTrace:
at Windows.ApplicationModel.Core.CoreApplication.get_MainView()
at CodedUITestProject2.CodedUITest1.ExecuteOnUIThread(DispatchedHandler action)
at CodedUITestProject2.CodedUITest1.d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
So how can I solve this problem? Please help me.

Coded UI tests are intended to be called by the Coded UI test control mechanisms within Visual Studio or the associated programs such as mstest.exe. All the Coded UI tests I have seen have code such as:
[TestMethod]
public void CodedUITestMethod1() { ... }
They may also have a [DataSource(...)] attribute. The method you tried has a very different type:
[TestMethod] async public Task CodedUITestMethod1() { ... }
I have not seen any documentation or examples suggesting that such a method can be used with Coded UI.
Having the call of XamlWindow.Launch(...) within the [TestMethod] suggests you are trying to build the Coded UI tests and application together into one program and then get the test to invoke the application by calling a method within that program. That is not how Coded UI is intended to be used. A Coded UI test suite should be created as separate program (actually a DLL that is called by Visual Studio, by mstest.exe or by similar) from the application being tested. Coded UI then runs the application as a program (not by calling a method within the application). The Coded UI test then interacts with the program by reading the screen and by sending keyboard input and mouse clicks to the program.

Related

.Net MAUI Windows simulator Geolocation.Default.GetLocationAsync raises exception Permission request must be invoked on main thread

When running a .Net MAUI app using the Windows simulator, I have the following code (stripped down to include the relevant statements):
task = Task.Run( async () => await do_something( cancellation_token ), cancellation_token );
The do_something() method calls Geolocation.Default.GetLocationAsync, as shown below:
do_something( CancellationToken ct )
{
try
{
location = await Geolocation.Default.GetLocationAsync( geo_location_request, ct );
}
catch ( Exception )
{
/*
At this point, an exception is raised:
Microsoft.Maui.ApplicationModel.PermissionException: Permission request must be invoked on main thread
at Microsoft.Maui.ApplicationModel.Permissions.LocationWhenInUse.RequestLocationPermissionAsync()
*/
}
}
As per the comments above, an exception is raised that states the call to Geolocation.Default.GetLocationAsync() must be invoked on the main thread.
However, when using the Android simulator, no exception is raised and everything works fine. I did not see anything mentioned in the documentation that the call to Geolocation.Default.GetLocationAsync() must be invoked on the main thread.
Apologies if such information does exist and I missed it -- if that is the case, please provide the relevant link.
Questions
Is this an undocumented "feature"?
Is this a bug in the Windows simulator?
I can get around the issue (by invoking the task on the MainThread), but I want to know if it is really necessary to do so given that it works on Android, but not on Windows.
Thanks in advance.
The system automatically checks permissions when you get the location, whereas in Windows the location permission is checked on the main thread. For more details, you can refer to the following issue:https://github.com/Xamarin/Essentials/pull/1777

Migrating the code from Bot Framework V3 to V4

I have more question while migrating the dialog from V3 to V4. below is our code.
In v3, we were using
Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(conversationContext.CurrentActivity, new RootDialog());
public class RootDialog : IDialog {
public RootDialog()
{
.....
}
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
}
In the MessageReceivedAsync, we used the context.Wait(), context.Done() and context.PostAsync().
Can you recommend how to replace in the V4? And what's the alertnative for Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync in V4?
These APIs are all gone. Here are the explanations of their replacements in V4:
context.Wait(…)
This method was used to tell the dialog system what method to invoke next on your class when a new activity arrived and is now gone. Instead you now subclass Dialog and override several methods for various lifecycle events:
BeginDialogAsync - called when the dialog is first pushed on the stack by bot code or another dialog calling BeginDialogAsync on the DialogContext.
ContinueDialogAsync - called when a new activity comes in and the bot calls ContinueDialog on the DialogContext.
ResumeDialogAsync - called when another dialog on the stack has completed and a dialog that was previously on the stack is now at the top of the stack.
RepromptDialogAsync - called when an explicit request has been made to reprompt the user. This is basically a way to tell the dialog that nothing has changed, but that it should pick up from where it left off again by sending whatever activity it last sent.
EndDialogAsync - called when the dialog has indicated its done and is being popped off the stack.
context.Done()/.Fail()
This was one of the way you reported the status of your dialog, but this is now accomplished by returning a DialogTurnResult from most of the aforementioned lifecycle methods. One of the properties is named Status and is of type DialogTurnStatus which has values that indicate the current state of the dialog. For example:
Waiting - the dialog sent some activities and is awaiting more input and should remain at the top of the stack.
Complete - the dialog has completed it's work and should be ended and popped off the stack. When this state is returned, callers can also investigate the output of the dialog (if it has one) which is passed back via the DialogTurnResult::Result property.
Cancelled - the dialog was cancelled part of the way through its work.
context.PostAsync()/Conversation.SendAsync
These were both used to respond back to the user. Both are now replaced by calling SendActivityAsync on the ITurnContext that is accessible via the Context property of the DialogContext instance that is passed into most of the aforementioned lifecycle methods as a parameter. NOTE: a couple of the lifecycle methods actually receive an ITurnContext parameter directly and then you just use that.

Using Camel Processor as custom component

I want to make a custom camel processor to behave as a custom component.I read it as it as possible from http://camel.apache.org/processor.html - section--> Turning your processor into a full component.
Here the Custom Processor created will have to do the job when i call
someComponent://action1?param1=value1&param2=value2
in the route.
For this i created a sample component using maven catalog.This created Endpoint,Consumer, Producer and Component classes.
The link says that the component should return ProcessorEndpoint which i have done.
So, Endpoint looks as below
public class SampleEndpoint extends ProcessorEndpoint{
// Automatically Generated code begins
public Producer createProducer() throws Exception{
return new SampleProducer(this, processor);
}
public Consumer createConsumer() throws Exception{
throw new UnsupportedOperationException("This operation is not permitted....");
}
// Automatically generated code ends here
//added below to make custom processor work for custom component
public Processor createProcessor(Processor processor){
return new SampleProcessor();
}
}
But, here the code in the processor is not getting executed instead the code in the SampleProducer gets executed.
Here i want the processor to be excuted.How do i do that?
When extending ProcessorEndpoint, the Producer from createProducer() will handle the exchange, i.e. Producer.process(Exchange exchange).
This is why you are seeing SampleProducer being used. But if you wanted to delegate to a processor, you could probably just change your code to be:
return new SampleProducer(this, new SampleProcessor());
My best advice would be to attach a debugger and put breakpoints in your SampleEndpoint, SampleProducer and SampleProcessor methods to see what gets called and when.

How do I keep the browser open after a coded ui test finishes?

I'm using Visual Studio 2012 Coded UI tests for a web application. I have a test for logging into the app which starts the browser, locates the login dialogue, enters credentials, and then clicks ok. I have an assertion which checks for the correct url after the login. This test appears to function correctly. My problem is that it closes the browser after the test runs. I need to keep the browser open, so I can run the next test in my sequence. How do I do this?
At the moment, I don't have anything in my [TestCleanup()] section. I'm assuming that what I'm looking for goes here, but so far I haven't had a lot of luck figuring out what that is supposed to be.
I don't have the original source where I found this solution :(
You can have a method like the one showed below. This method needs to be called in TestSetup. Also declare a class level variable _browserWindow of the tyep BrowserWindow
private void SetBrowser()
{
if(_browserWindow == null)
{
BrowserWindow.CurrentBrowser = "ie";
_browserWindow = BrowserWindow.Launch("http://www.google.com");
_browserWindow.CloseOnPlaybackCleanup = false;
_browserWindow.Maximized = !_browserWindow.Maximized;
}
else
{
BrowserWindow.CurrentBrowser = "ie";
_browserWindow = BrowserWindow.Locate("Google");
_browserWindow.Maximized = !_browserWindow.Maximized;
}
}
Ok, so what I needed to have happen was the launch and login before each test. I thought what I wanted was to run the browser and login test first, and then each additional test. After reading more, I've decided what I actually wanted was to run this logic as initialization code for each test. I've done that by adding this code to the default [TestInitialize()] generated when I started the coded ui project in Visual Studio 2012.
I have found the following method to work for my data driven coded UI test in Visual Studio 2015.
You will want to use [ClassInitialize] and get your browser open and direct it according to where your [TestMethod] begins.
Use [ClassCleanup] to release the resources after all the methods in the test class have been executed.
You can redirect test methods different after the class has been initialized by using the [TestInitialize] and clean-up test using the [TestCleanup]. Be careful with those though because they will occur for each test method and if it closes your browser instance your following test will fail.
private static BrowserWindow browserWindow = null;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Playback.Initialize();
browserWindow = BrowserWindow.Launch(new Uri("http://198.238.204.79/"));
}
[ClassCleanup]
public static void TestCleanup()
{
browserWindow.Close();
Playback.Cleanup();
}

Invalid cross thread access in silverlight

i am making an application in silverlight 3.0. In that application i have one public method as
public void DrawWavform()
{
Line[,] line = new Line[10,200];
line[i,j]= new Line();//i am getting error here of invalid thread access
//some operation
}
In application i am creating different threads as per user inputs and calling DrawWaveform method from that newly created thread. I want to parallel opeation.Please suggest me solution.Thanks in advance.
Any operation changing the GUI must be executed on the UI thread. This can be done using the dispatcher:
Deployment.Current.Dispatcher.BeginInvoke( () =>
{
// update ui
});
or
(SomeDependencyObject).Dispatcher.BeginInvoke( () => { /* ... */ } );
Anyway, this code should be used very rarely and contain only UI related code. Executing expensive operations on the UI thread will cause your application to hang.

Resources