How to run Xamarin UI tests on Android emulator - android

How to run Xamarin UI tests on Android emulator?
Always I run my test on real devices, but for CI I need tests on emulator, but I don't know how and google didn't give me a specific answer
public class AppInitializer
{
private const string ApkPath = #"..\..\..\mob\mob.Droid\bin\Release\myApp.apk";
private const string AppPath = "mob.iOS.app";
public static IApp StartApp(Platform platform)
{
if (platform == Platform.Android)
{
return ConfigureApp
.Android
.EnableLocalScreenshots()
.ApkFile(ApkPath)
.StartApp();
}
return ConfigureApp
.iOS
.EnableLocalScreenshots()
.StartApp();
}
}

In the documentation and as #tequilaslammer mentions briefly:
Unlike testing on iOS, Xamarin.UITest will not automatically start the Android Emulator. It is necessary to have an Android emulator running or an Android device already connected. If there is more than one device or emulator connected, it will be necessary to provide the serial ID of the device/emulator that should be used to run the test.
Source: https://developer.xamarin.com/guides/testcloud/uitest/intro-to-uitest/#Initializing_AndroidApp
I highly recommend that you read through the complete documentation on this topic as there are a few "gotchas" that will need to be considered for your situation:
https://developer.xamarin.com/guides/testcloud/uitest/intro-to-uitest/

Related

App crashes on Launch when made as system application

I developed an application that uses DJI SDK. When installed as an system application it crashes on Launch because its not able to load the libraries. It works fine when installed as normal application.
Steps followed to make system application
1)Rooted the device
2)Copied the .apk file to system/app directory
3)Rebooted the device
4)App was installed as default application but crashed on Launch
After commenting out the following line related to DJI SDK and followed the same steps as above. It worked fine as system app.
Helper.install(context);
The implementation of Helper class which is in DJI SDK is as below:
public static void install(Application app) {
Object var1 = null;
try {
System.loadLibrary("DexHelper_sdk");
if (PPATH != null) {
System.load(PPATH);
}
} catch (Error var3) {
;
}
String app_dataDir = app.getApplicationInfo().dataDir;
DexInstall.install(app, new File(app_dataDir + "/.cache_sdk/sdkclasses.jar"));
}
As far as my understanding the crash is because the system is failing to load the libraries.
My gradle looks similar as in the following link:
https://github.com/dji-sdk/Mobile-SDK-Android/blob/master/Sample%20Code/app/build.gradle
this issue has been fixed since 4.6 version of Android MSDK, pls try to update to the later version, right now the 4.7.1 has been released.

Launching android emulator using specflow

I'm trying to use specflow to create automated testing for a Xamarin project in Visual Studio 2017. I've created a project which uses Nunit and specflow and created a feature file. I've seen it's possible to start a iPhone and do some automated testing, but I'm having problems on how to do this with android. I've read this BDD Test with xamarin where, as I understand, I should add this:
public static IApp StartApp(Platform platform, string simulator)
{
if (platform == Platform.Android)
{
return ConfigureApp
.Android
.StartApp();
}
return ConfigureApp
.iOS
.StartApp();
}
However I cannot figure out how this will help me launch the simulator, and what I should pass as the string to start an android simulator.

SetUp : System.Exception : ApkFile or InstalledApp has not been configured

i'm Working on xamarin forms simple app (CreditCardValidator) i downloaded the app from [https://developer.xamarin.com/samples/test-cloud/Quickstarts/CreditCardValidator.Droid/]
Now is it possible to start Running Test Method without providing path to (.apk File) ... if not i want to know how to start VS emulator just after Running Test method ( running test method to automatically launch VS emulator ) ?
public class Tests
{
AndroidApp app;
[SetUp]
public void BeforeEachTest()
{
app = ConfigureApp.Android.StartApp();
}
[Test]
public void CreditCardNumber_TooShort_DisplayErrorMessage()
{
app.WaitForElement(c => c.Marked("action_bar_title").Text("Enter Credit Card Number"));
app.EnterText(c=>c.Marked("creditCardNumberText"), new string('9', 15));
app.Tap(c => c.Marked("validateButton"));
app.WaitForElement(c => c.Marked("errorMessagesText").Text("Credit card number is too short."));
}
}
If there is only one device attached to the computer, and no running simulators, then UITest will automatically run the tests on the device.
This would also apply to an emulator in the sense that it would have to be already up and running.
Source: https://developer.xamarin.com/guides/testcloud/uitest/working-with/testing-on-devices/android/#Programmatically_Declaring_the_Device
However since you might need to boot an emulator up, you can do the following:
There is a tool called emulatorcmd.exe that you can use. It is found in the following directory on Windows:
C:\Program Files (x86)\Microsoft Emulator Manager\1.0
You can use emulatorcmd.exe list /sku:Android /type:device /state:installed to get a list of available emulators that are installed.
You can then use emulatorcmd.exe launch /sku:Android /id:<id> which will launch that specific emulator.
https://blogs.msdn.microsoft.com/visualstudioalm/2014/11/12/introducing-visual-studios-emulator-for-android/

Xamarin.UITests Android Device Not Detected or Not Working

I can't run Xamarin.UITests on any Android simulator/emulator. Any suggestions? I get the following errors when I try:
Google Emulator:
SetUp: System.Exception: Unable to run on a physical device without activation.
The full version is available for Xamarin Test Cloud customers, for more information contact sales#xamarin.com
If you are already a Xamarin Test Cloud customer, you can provide your api in one of the following ways:
* Adding it ConfigureApp using the ApiKey method
* Setting the XTC_API_KEY environment variable
* Adding the following attribute to your Properties/AssemblyInfo.cs file: [assembly: Xamarin.UITest.TestCloudApiKey(YOUR_API_KEY)]
* Place an xtc_api-key file containing your api key in an upstream directory from the test assembly
Xamarin Android Player:
SetUp: System.Exception: No devices connected
GenyMotion:
SetUp : System.Exception : Failed to execute: /users/erikandersen/Library/Developer/Xamarin/android-sdk-macosx/platform-tools/adb devices - exit code: 1
cannot bind 'tcp:5037'
ADB server didn't ACK
* failed to start daemon *
error:
adb server is out of date. Killing…
After updated GenyMotion to 2.3.1, I now get the following error:
SetUp: System.Exception: App Installation failed with output: 12050 KB/s (11199602 bytes in 0.907s)
pkg: /data/local/tmp/final-xxxxxx.apk
Failure [INSTALL_FAILED_CPU_ABI_INCOMPATIBLE]
NUnit Code
AndroidApp _app;
public string PathToAPK { get; set; }
[TestFixtureSetUp]
public void BeforeAll ()
{
var path = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
var info = new FileInfo(path);
var directory = info.Directory.Parent.Parent.Parent.FullName;
PathToAPK = Path.Combine(directory, "Android", "bin", "Debug", "Demo.Android.apk");
}
[SetUp]
public void BeforeEach ()
{
_app = ConfigureApp.Android.ApkFile (PathToAPK).StartApp ();
}
[Test]
public void TestInvalidEmail ()
{
_app.EnterText (c => c.Class ("UITextField"), "");
}
TestInvalidEmail() is never called because NUnit fails on
_app = ConfigureApp.Android.ApkFile (PathToAPK).StartApp ();
Background:
I'm using Xamarin.UITests for an iOS/Android application I'm developing and we're having issues with the Android side of things. iOS works fine. I've written each test twice, once in C# and once in Ruby using calabash, in order to isolate the issue. Calabash works fine, but any C# NUnit Test Project fails to connect to any emulator I try launching.
What I've tried:
Making sure an emulator is already running before I run the tests
Making sure only 1 emulator is running
Restarting the adb server
Trying multiple types of emulators (i.e. Xamarin Android Player, Gennymotion, and the Google Emulator)
So, after several hours of working on this, this fixed it:
Upgrade GennyMotion to 2.3.1
Delete my old GenyMotion Simulator (Nexus 4) and create a new one
Add support for all ABIs in my Android Build settings
Android Project > Options > Android Build > Advanced Tab > Supported ABIs > Check "armeabi, armeabi-v7a, x86" > OK
Generate a new .apk file via the terminal (while inside the Android project directory)
/usr/bin/xbuild /t:Package /p:Configuration=Release .csproj
Resigned the .apk via calabash-android
calabash-android resign ./bin/Release/.apk
In Xamarin Studio, set my Startup Project as the my UITest project
Launch GenyMotion simulator
Ran the tests under the Debug configuration with only the GenyMotion simulator active
And it worked! I still can't run UI tests with the Google Emulator or the Xamarin Android Player. Funny thing is, I also can't debug my Android project using GenyMotion. It's only good for UITests. SO here's my current setup:
Xamarin Android Player - Normal debug
GenyMotion - UITests
I had the same problem and fixed by setting the ApiKey when setting ConfigureApp.
[SetUp]
public void SetUp()
{
_app = ConfigureApp.iOS.AppBundle(PathToIPA).ApiKey("YOUR_API_KEY_HERE").StartApp();
}
As in here. Just put your ApiKey there.
Note: I didn't have this problem with Xamarin Android Player. I didn't have this problem with AVD running an Android 5.0 emulator. I only had this problem with AVD running an Android 4.0.3 emulator.

Android Unit Test Not Reporting Failing with fail()

I've written a unit test that simply extends TestCase and I have the following:
public class MetricParserTests extends TestCase {
#Override
protected void setUp() throws Exception {
super.setUp();
}
#Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testFailure() {
fail("This needs to fail");
}
}
When I run my tests using ant test or adb shell am instrument I get the following results:
... [exec] OK (1 tests) ...
I'd expect to see a failure on the command line.
I believe I know what the issue is. I was able to reproduce the issue and solve it. The command you use does not rebuild and re-install your test project onto a device. When you call ant test it will just execute the tests which are already installed on that device.
What you need to call is the three commands in your test project's directory:
ant debug
ant installd
ant test
Then all tests will be rebuild and re-installed and latest tests will be executed. If you don't call debug and installd, the changes you did to the tests do not get applied.
I haven't had recent experience in Android testing, but here is what I have found...
You can use normal JUnit tests if your code is totally decoupled from Android (see here
for an example). This would run on your JVM using the JUnit runner.
However, if you are trying to run these tests on an Android device (either via ant, or the command line tools) then you need to create a full android test project (See here).
To test "on device" your test cases need to extend one of the Android test classes like ActivityInstrumentationTestCase2<T>
and are run using the InstrumentationTestRunner in the Dalvik VM on the Android device.
Using an IDE or the command-line tools to create a test project should create a sample test for you to work from.
This blog post linked from the comments of the post above is a good source of information, as is the Android Testing Fundamentals doc.
The method testFailure() does not have a #Test annotation. Is that correct?

Categories

Resources