Can I run Appium for mobile with no target app? - android

Since I'm working in a platform which works with many apps (rather than a single target app), I find the selection of a target app inefficient for my needs. I wondered if I could do something to avoid it.
I'd like to run freely, sending UI commands to iOS and Android real devices, including installing an app from another app (like Play Store, Apple Store, Test Flight, etc.)
Thanks for the help,
David.

The rule is: 1 Webdriver instance per application.
You can run Appium's server with no --app argument by making sure auto-launch is set to false, and not setting bundleId or app.
Then, in your client/test framework, you use several webdrivers, configured to use different desired capabilities, to tie it all together under a single test case/suite.
The solution:
You may have a test suite that sets desired_capabilities to launch the Safari app, then you install the app, then you quit the webdriver.
Then you change the desired_capabilities to point to the bundle_id of the new app, launch another webdriver instance, do your tests, quit the webdriver..
Then you change the desired_capabilities to point to (etc., etc.)
driver = webdriver.new(url, desired_capabilities)
// do some stuff
driver.quit()
desired_capabilities['app'] = 'company.app.com'
driver = webdriver.new(url, desired_capabilities)
// do some stuff
driver.quit()
desired_capabilities['app'] = '/path/to/application.app'
driver = webdriver.new(url, desired_capabilities)
// do some stuff
driver.quit()

Related

How to set device owner on Android Things?

I would like to set my Android app as device owner on a device running Android Things.
(I know that Android Things is getting shut down, but still I would need to make it work)
I tried the usual command: adb shell dpm set-device-owner <package>/<component>
This command works well on a standard Android device, but on Android Things I get the following response:
java.lang.RuntimeException: Can't set package ComponentInfo{<package>/<component>} as device owner.
at com.android.commands.dpm.Dpm.runSetDeviceOwner(Dpm.java:149)
at com.android.commands.dpm.Dpm.onRun(Dpm.java:96)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:54)
at com.android.commands.dpm.Dpm.main(Dpm.java:41)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:285)
I tried running the command as root (after adb shell & su), but still get the same error.
I also tried to create manually device_owner.xml, device_policies.xml,... files in /system/data, as mentionned in topics related to AOSP, but it has no effect.
Is it somehow possible to set device owner on an Android Things device?
Android Things are already deprecated now, you can use smartphone OS or Android. That is based on this quote:
Android Things is a now-deprecated Android-based embedded operating system platform by Google, announced at Google I/O 2015, and launched in 2018. In 2019, Android Things dropped support for low-power hardware and refocused on smartphone-class devices.
Owner in android can be used to set in this document explanation, sample code :
val provisioningActivity = getActivity()
// You'll need the package name for the DPC app.
val myDPCPackageName = "com.example.myDPCApp"
// Set up the provisioning intent
val provisioningIntent = Intent("android.app.action.PROVISION_MANAGED_PROFILE")
provisioningIntent.putExtra(myDPCPackageName,
provisioningActivity.applicationContext.packageName)
if (provisioningIntent.resolveActivity(provisioningActivity.packageManager) == null) {
// No handler for intent! Can't provision this device.
// Show an error message and cancel.
} else {
// REQUEST_PROVISION_MANAGED_PROFILE is defined
// to be a suitable request code
startActivityForResult(provisioningIntent,
REQUEST_PROVISION_MANAGED_PROFILE)
provisioningActivity.finish()
}

Run an instrument test from within app and wait for result

I am developing a QA automation solution that can record/playback QA tests on Android. A key business requirement is to have no dependency on a connected PC while playing back the recorded test. To that end, I'm trying to run an Instrumentation test without a connected PC. (Specifically, an Appium UiAutomator2 test).
My current approach is trying to run the test programmatically from my app. If I were running the test normally from a connected PC, I would use the command adb shell am instrument -w. I tried accessing ADB Shell from my app and running am instrument -w, but this produces an error that I am missing the INTERACT_ACROSS_USERS_FULL permission.
To get around this issue, I am trying to run the test using startInstrumentation. This successfully starts the test. However, the test immediately crashes. Upon further investigation, I traced the crash to an NPE: the test is trying to retrieve InstrumentationRegistry.getInstrumentation.getUiAutomation(0), but this returns null.
How do I run the test programatically and give it access to the required UiAutomation instance?
This is how I start the test:
public void runTest() {
final String pm = getPackageName().replaceFirst(".test$", "");
final InstrumentationInfo info = getInstrumentationInfo(pm);
if (info != null) {
final ComponentName cn = new ComponentName(info.packageName,
info.name);
Bundle arguments = new Bundle();
arguments.putString("class", "io.testim.appiumwrapper.test.AppiumUiAutomator2Server");
//cn = {io.extension.test/android.support.test.runner.AndroidJUnitRunner}
startInstrumentation(cn, null, arguments);
}
}
see signature protection level - clarifying ...unless having the package white-listed by Google's release key, you won't be able to obtain the necessary permission. this is a security/integrity feature, with the purpose to limit what malware is able to do - and what you intend to do there, is typical malware behavior - no matter the actual intention of it; working against the system leads nowhere.
the only way I could imaging would be to run commands directly from a terminal emulator or the test application - against a custom build of AOSP, so that you could add android:protectionLevel="signature" to the Manifest.xml and then require android.permission.INTERACT_ACROSS_USERS_FULL. but with a stock ROM, there definitely is no chance to do so. it is not, that it would be "impossible", but building a custom ROM means quite some effort, in order to get there. at least for Nexus and Pixel devices, the required drivers are available here; for other devices, you'd have to find them at the device's vendor, if they're even available.
the trick is to sign off the ROM with the same key as the app - only then signature level permissions can be obtained - while for a stock ROM, you'd (theoretically) need Google's release key to sign off the package. one can enforce single user, as explained here, while this also is only available to system apps.

AWS Device Farm - Pass parameters to a Robotium Test Script - Android

I want to automate the Robotium tests running on my Android application, however, I need to pass in parameters to my tests in order to allow them to run with that configuration.
I can start my tests from the command line as such:
adb shell am instrument -r -w -e PARAM PARAM_1,PARAM_2 com.company.product.application.test/android.support.test.runner.AndroidJUnitRunner
I grab these parameters as such in my code:
Bundle extras = InstrumentationRegistry.getArguments();
if (extras.containsKey("PARAM")) {
this.param1= new ArrayList<>();
this.param1= Arrays.asList(extras.getString("PARAM").split("\\s*,\\s*"));
}
I am intending to call the Device Farm API (https://docs.aws.amazon.com/devicefarm/latest/APIReference/Welcome.html) to run the tests, but I am struggling to find anywhere where I can declare my arguments/parameters. Or find a way of uploading them in a file somewhere in AWS for the Device farm to read from.
I have found some articles from 4-5 years ago claiming that passing launcher parameters was not supported but it was being looked into, in case some claim me lazy, but no one has responded to any questions there, or on the Amazon forums so I thought I would place the question here.
Can anyone help me with the above?
The tests are executed by Device Farm internally so there's no way to control the arguments that get sent to it when running, currently. However, it should be possible to include additional files which could hold that same info for the tests. Here are two ideas I have regarding this:
The tests could use the extra data feature of Device Farm. The extra data zip file gets uploaded to the /sdcard on the device. Then the tests could get the information from the device itself. This is part of the schedule run API so the script running the tests can do this programmatically.
There could be a file that's included in the test apk that's uploaded to Device Farm. I've done something similar with Java Appium tests but not with instumention. In theory it should work.
Other than that, I have not found other ways of including arguments or files for Device Farm.
Hth
-James

Appium, running two phones at once for user and agent

I have a situation where I need to run one browser as a client and another browser as an agent. On desktop this is easy and we use the browser of choice for the user and phantom for the agent.
Using Appium and running on the Android emulator I am having problems trying to replicate the desktop setup. I first tried to have the computer run the phantom and then run the Appium tests but I was getting told by Appium that it didn't recognize phantom. I couldn't find a way to have phantom run on the desktop while still using Appium for the phone.
Next I thought I would just automate the agent side directly in Chrome on the phone to avoid the split between desktop and phone. However, I still ran into problems as I can't seem to create new tabs in Chrome to switch to and even with running two different phones I can't switch between them.
We decided to use RobotFramework and AppiumLibrary to code this due to readability for possible end users of this automation.
These are the first keywords run:
*** Keywords ***
Specifying a Known Mobile Device
API Login regression password
sleep 10
Agent Login
Open Application http://localhost:4723/wd/hub platformName=Android browserName=Chrome platformVersion=7.1.1 deviceName=Nexus6P app=com.android.chrome automationName=appium appPackage=com.android.browser appActivity=com.google.android.apps.chrome.Main
go to url blahblah.com
Check Availability Regression Flow HSSMobile
API login and Agent login are:
API Login
[Arguments] ${UN} ${PW}
Set Global Variable ${ACCOUNTNAME} ${UN}
Set Global Variable ${ACCOUNTPWD} ${PW}
Open Application http://localhost:4725/wd/hub platformName=Android browserName=Chrome platformVersion=7.1.1 deviceName=Pixel app=com.android.chrome automationName=appium appPackage=com.android.browser appActivity=com.google.android.apps.chrome.Main
go to url blahblahapi.com
Agent Login
MobileLibrary.Wait Until Element Is Visible ${USERNAME} 25
MobileLibrary.Input Text ${USERNAME} ${ACCOUNTNAME}
MobileLibrary.Input Text ${PASSWORD} ${ACCOUNTPWD}
MobileLibrary.Click Element ${LOGIN}
I imagine I am doing something wrong in the Open Application keyword but I'm not sure. I am running two instances of Appium in the command line.
appium
and
appium -p 4725
Yet, everything runs on only one of the phones.

How to instantiate Appium Driver based on platform?

I am a beginner in automation testing. I am trying to write common test project for my android and ios app. Both the apps on ios and android have almost same UI and flow. I want to know how can I instantiate Appium driver for based on the platform.
As of now, I am thinking to have a boolean variable isAndroid which I would have to manually change in the code before running the tests.Based on isAndroid I instantiate AppiumDriver to AndroidDriver or IOSDriver and elements will be located as per #AndroidFindby or #IOSFindBy
When I start Appium from the terminal, I mention the parameter platformName, is there a way to fetch that information in my code so that I do not have to manually change isAndroid variable and exactly same test code runs on both platforms.
Are you able to try one platform, catch the exception and then try the other?
try:
driver = webdriver.Remote('http://localhost:4723/wd/hub', ios_caps)
run_iOS_tests(driver)
except selenium.common.exceptions.WebDriverException:
driver = webdriver.Remote('http://localhost:4723/wd/hub', android_caps)
run_android_tests(driver)
Otherwise you'll need to use some external method to see what devices are connected (android, ios).

Categories

Resources