简体   繁体   中英

How to I identify the device on which android tests are currently running?

I have multiple devices connected and I run the gradle command 'connectedCheck' which runs tests on multiple devices.

ConnectedCheck executes tests on all the devices in an order. I want to get the serial number of the device the tests are currently being run on.

Does android provide a way to do that?

You can get lots of details of your android device like :

//Get the instance of TelephonyManager  
    TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);  

    //Calling the methods of TelephonyManager the returns the information  
    String IMEINumber=tm.getDeviceId();  
    String subscriberID=tm.getDeviceId();  
    String SIMSerialNumber=tm.getSimSerialNumber();  
    String networkCountryISO=tm.getNetworkCountryIso();  
    String SIMCountryISO=tm.getSimCountryIso();  
    String softwareVersion=tm.getDeviceSoftwareVersion();  
    String voiceMailNumber=tm.getVoiceMailNumber();  

    //Get the phone type  
    String strphoneType="";  

    int phoneType=tm.getPhoneType();  

    switch (phoneType)   
    {  
            case (TelephonyManager.PHONE_TYPE_CDMA):  
                       strphoneType="CDMA";  
                           break;  
            case (TelephonyManager.PHONE_TYPE_GSM):   
                       strphoneType="GSM";                
                           break;  
            case (TelephonyManager.PHONE_TYPE_NONE):  
                        strphoneType="NONE";                
                            break;  
     }  

And to get the device model name, we use:

public String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
    return capitalize(model);
} else {
    return capitalize(manufacturer) + " " + model;
}
}


private String capitalize(String s) {
if (s == null || s.length() == 0) {
    return "";
}
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
    return s;
} else {
    return Character.toUpperCase(first) + s.substring(1);
}
} 

I found the solution! In fact there wasn't a problem in the first place.

Issue: Gradle android plugin's task to run tests is 'connectedCheck'. It executes tests on how many ever android devices connected to a host. Now when I @Ignore a test it will be ignored on all those devices where as I wanted to do that only for the failing device/devices

Solution: Add a custom jUnit annotation that takes an array of String values for Device serial IDs. Write a custom test runner by extending BlockJUnit4ClassRunner override runChild to check if Build.Device is same as the one provided in the annotation if a method is annotated.

If it is fireTestIgnored to ignore so that it will be skipped only for the device you specified in the annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM