I'm beginner in Xamarin Test Cloud and I want to write tests for Xamarin Test Cloud.
I have Xamarin UITests in my solution and I tried to launch REPL, but UITest REPL window didn't open.
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;
namespace MurakamiKiev.UITests
{
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.StartApp ();
}
[Test]
public void TestLaunch ()
{
app.Repl();
}
}
}
Where is the error?
Also, what I need to write to launch specified activity?
If you don't have the application source code in the same solution then you'll need to specify the prebuilt app by pointing to it via a full path.
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.ApkFile("<path-as-string>").StartApp ();
}
Related
I try to unit-test a Gui application code that uses
Application.Current.Dispatcher.Invoke() and would like to use the solution provided by #informatorius in the similar thread Using the WPF Dispatcher in unit tests. The code is listed below.
The problem I have is that Application is not resolved, even if I add using System.Windows. Is there some special mechanism to resolve
Application from within a class library that defines the testcases ?
I have the MSTest.TestFramework and MSTest.TestAdapter packages installed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class ApplicationInitializer
{
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
var waitForApplicationRun = new TaskCompletionSource<bool>();
Task.Run(() =>
{
var application = new Application();
application.Startup += (s, e) => { waitForApplicationRun.SetResult(true); };
application.Run();
});
waitForApplicationRun.Task.Wait();
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
Application.Current.Dispatcher.Invoke(Application.Current.Shutdown);
}
}
[TestClass]
public class MyTestClass
{
[TestMethod]
public void MyTestMethod()
{
// implementation can access Application.Current.Dispatcher
}
}
Answer pointed me into the right direction:
using System.Windows is not enough, I also needed to add reference to PresentationFramework to the project. Dont really understand the auto magic behind that.
I made an android application in Visual Studio, using Xamarin. I have an emulator installed in visual studio. The applaction uses txt files, which I made on the emulator earlier (File.WriteAllText). The problem is that I can't run unit tests. DependencyService.Get throws an error.
Test
Result Message: Unable to create instance of class (...). Error: System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it..
I'm not sure where I'm wrong, and what I should do here.
There's an IPersistence class which gives the loading from file method.
public interface IPersistence
{
Task<Player[,]> Load(int x);
}
I implemented it for android in another class:
public class AndroidDataAccess : IPersistence
{
public async Task<Player[,]> Load(int x)
{
/*...*/
}
}
Unit test Code:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Model;
using Persistence;
using Xamarin.Forms;
namespace UnitTestProject1
{
[TestClass]
public class Test1
{
private Model _model;
private IPersistence _dataAccess = DependencyService.Get<IPersistence>(); //It throws an error.
[TestInitialize]
public void Initialize()
{
_model = new Model(_dataAccess);
_model.GameWon += new EventHandler<GameWonEventArgs>(Model_GameWon);
_model.GameOver += new EventHandler(Model_GameOver);
_model.FieldChanged += new EventHandler<FieldChangedEventArgs>(Model_FieldChanged);
_model.GuardChanged += new EventHandler<GuardChangedEventArgs>(Model_GuardChanged);
}
[TestMethod]
public void BasicTest10()
{
_model.NewGame(10);
Assert.AreEqual(4, _model.S);
}
DependencyService requires a Forms context to operate. This is normally created when creating the MainActivity for Android or the AppDelegate for iOS. For your test I would recommend creating a test fixture for each platform and then create a method that manually initialises dataAccess based on the platform.
[TestFixture(Platform.Android)]
[TestFixture(Platform.iOS)]
public class Test1 {
...
...
private IPersistence InitialisePersistance(Platform platform) { ... }
}
I'm writing UITests on Xamarin
I try to launch the Repl window, but it doesn't launch.
My code:
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xamarin.UITest.Queries;
namespace MurakamiKiev.UITests
{
[TestFixture]
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest ()
{
app = ConfigureApp.Android.StartApp();
}
[Test]
public void ClickingButtonTwiceShouldChangeItsLabel ()
{
app.Repl();
}
}
}
This is, how I try to launch Repl:
That is, what I have in Console.
What wrong with my code??
I tried breakpoints, but nothin happens.
I tried to update references, but it didn't help.
Or if issue not in code, how I can launch Repl window?
Help me please I wrote Xamarin forums, but don't have answer.
UPDATE
I try to use Debug and x86
Have this error
Severity Code Description Project File Line Suppression State
Error java.lang.OutOfMemoryError. Consider increasing the value of $(JavaMaximumHeapSize). Java ran out of memory while executing 'java.exe -jar C:\android-sdk\build-tools\23.0.1\\lib\dx.jar --no-strict --dex --output=obj\x86\Debug\android\bin obj\x86\Debug\android\bin\classes "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v6.0\mono.android.jar" C:\Users\nemes\Documents\GitHub\Murakami_kiev\MurakamiKiev\obj\x86\Debug\__library_projects__\Square.OkHttp\library_project_imports\okhttp.jar C:\Users\nemes\Documents\GitHub\Murakami_kiev\MurakamiKiev\obj\x86\Debug\__library_projects__\Square.OkIO\library_project_imports\okio-1.6.0.jar C:\Users\nemes\Documents\GitHub\Murakami_kiev\MurakamiKiev\obj\x86\Debug\__library_projects__\Square.Picasso\library_project_imports\picasso-2.5.2.jar C:\Users\nemes\Documents\GitHub\Murakami_kiev\MurakamiKiev\obj\x86\Debug\__library_projects__\UrlImageViewHelper\library_project_imports\bin\classes.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.Animated.Vector.Drawable\23.3.0.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0\embedded\libs\internal_impl-23.3.0.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.v7.AppCompat\23.3.0.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.v7.MediaRouter\23.3.0.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.v7.MediaRouter\23.3.0.0\embedded\libs\internal_impl-23.3.0.jar C:\Users\nemes\AppData\Local\Xamarin\Android.Support.Vector.Drawable\23.3.0.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\GooglePlayServices.Analytics\8.4.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\GooglePlayServices.Base\8.4.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\GooglePlayServices.Basement\8.4.0\embedded\classes.jar C:\Users\nemes\AppData\Local\Xamarin\GooglePlayServices.Maps\8.4.0\embedded\classes.jar' MurakamiKiev
My Heap size is set to 1G
Any answers how I can launch Repl?????
Look into adding platform specification for initializer
[TestFixture(Platform.Android)]
public class Tests
{
IApp app;
Platform platform;
public Tests(Platform platform)
{
this.platform = platform;
}
[SetUp]
public void BeforeEachTest()
{
app = AppInitializer.StartApp(platform);
}
[Test]
public void AppLaunches()
{
app.Repl();
}
}
Also I've noticed you have your project in Release mode - put it to Debug.
I have coded a simple test in visual studio using selenium which works in Firefox. However, I'm trying to run the same test on multiple browsers but I keep getting the same error that the drivers are not found in the directory or the PATH environment variable.
I have them downloaded and they are in the project I am working on. I've been trying all the different ways that I have found but nothing is working.
Can anyone help with this? Thanks :)
Here's a snippet of the code:
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTest2
{
//1 test multiple browsers
[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class ClickTestMetaLearning3TestUser<TWebDriver> where TWebDriver : IWebDriver, new()
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
private bool acceptNextAlert = true;
[SetUp]
public void SetupTest()
{
this.driver = new TWebDriver();
//Runtime.getRuntime().exec("RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255");
baseURL = "http://url";
verificationErrors = new StringBuilder();
}
We have had to explicitly tell the ChromeDriver where it is located when constructing it :
_chromeDriver = new ChromeDriver(#"<path to the chromedriver.exe");
I am trying to use ManagementEventWatcher in a service to keep track of when a computer goes in and out of sleep mode. I am new to .NET and C# so I am struggling quite a bit to come up with syntax to make this work.
I have found a blog post that details how he used ManagementEventWatcher to keep track of this status, but he did not post up his entire code. I am trying to go through and make a simple service that creates a .txt log file stating that the computer has been suspended/resumed but am running into problems with the namespaces and types.
Here is the code to the service.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Management;
namespace SleepNotifierService
{
public class WqlEventQuery : EventQuery { }
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WqlEventQuery query = new WqlEventQuery("Win32_PowerManagementEvent");
_watcher = new ManagementEventWatcher(query);
_watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
_watcher.Start();
}
protected override void OnStop()
{
_watcher.Stop();
}
void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
try
{
int eventType = Convert.ToInt32(e.NewEvent.Properties["EventType"].Value);
switch (eventType)
{
case 4:
Sleep();
break;
case 7:
Resume();
break;
}
}
catch (Exception ex)
{
//Log(ex.Message);
}
}
public void Sleep()
{
}
public void Resume()
{
}
}
}
Again, this is the first time that I am programming with .NET and C# so I apologize for my ignorance.
I am getting namespace errors such as:
The type or namespace name
'ManagementEventWatcher' could not be
found (are you missing a using
directive or an assembly reference?)
Thanks,
Tomek
You need the System.Management namespace, which is included in the code sample provided by you. I believe you need to reference the System.Management library in your project settings. Follow the following steps to do this( I am assuming you are suing Visual Studio):
Go to the Solution Explorer, and expand your project, right click on the References folder/option and select Add References from the context menu. Now select the .Net tab and select the System.Management from the list and click OK.