0

I've been looking and looking and can't find an answer. I use java .properties files for my test data. I need a way to iterate through the .properties file and create a map of only certain keys. The structure of the section of the .properties file I want is:

emulator.Android.driver.URL = https://emulator.com:443/wd/hub
emulator.Android.driver.appiumVersion = 1.7.1
emulator.Android.driver.deviceOrientation = portrait
emulator.Android.driver.browserName = ""
emulator.Android.driver.app = myApp.apk
emulator.Android.driver.platformName = Android
emulator.Android.driver.platformVersion = 6.0
emulator.Android.driver.name = sSuiteName
emulator.Android.driver.deviceName = Android Emulator
emulator.Android.driver.appPackage=com.app.android.debug
emulator.Android.driver.appActivity=com.app.android.LaunchActivity
emulator.iOS.driver.URL = https://emulator.com:443/wd/hub
emulator.iOS.driver.appiumVersion = 1.7.1
emulator.iOS.driver.deviceOrientation = portrait
emulator.iOS.driver.browserName = ""
emulator.iOS.driver.app = myApp.zip
emulator.iOS.driver.platformName = iOS
emulator.iOS.driver.platformVersion = 10.2
emulator.iOS.driver.deviceName = iPhone Simulator
emulator.iOS.driver.bundleId=com.qa
live.Android.driver.URL = https://live.com/wd/hub
live.Android.driver.apiKey = myKey
live.Android.driver.appiumVersion = 1.7.1
live.Android.driver.deviceOrientation = portrait
live.Android.driver.browserName = ""
live.Android.driver.app = myApp.apk
live.Android.driver.platformName = Android
live.Android.driver.platformVersion = 6.0
live.Android.driver.appPackage=com.app.android.debug
live.Android.driver.appActivity=com.app.android.LaunchActivity
live.iOS.driver.URL = https://live.com/wd/hub
live.iOS.driver.apiKey = myKey
live.iOS.driver.appiumVersion = 1.7.1
live.iOS.driver.deviceOrientation = portrait
live.iOS.driver.browserName = ""
live.iOS.driver.app = myApp.zip
live.iOS.driver.platformName = iOS
live.iOS.driver.platformVersion = 10.2
live.iOS.driver.name = sSuiteName
live.iOS.driver.bundleId=com.qa

NOTE: There are other properties in this file, but I only want the above. My goal is to extract all the properties such as emulator.Android.driver.* remove the emulator.Android.driver. and wind up with a map. For example, let's say I want to extract the properties for the emulator running on iOS. I would wind up with a map that would contain:

URL = https://emulator.com:443/wd/hub
appiumVersion = 1.7.1
deviceOrientation = portrait
browserName = ""
app = myApp.zip
platformName = iOS
platformVersion = 10.2
deviceName = iPhone Simulator
bundleId=com.qa

Next run I might select live with Android and should wind up with the map containing:

URL = https://live.com/wd/hub
apiKey = myKey
appiumVersion = 1.7.1
deviceOrientation = portrait
browserName = ""
app = myApp.apk
platformName = Android
platformVersion = 6.0
appPackage=com.app.android.debug
appActivity=com.app.android.LaunchActivity

Can anyone point me in the right direction?

GregMa
  • 740
  • 2
  • 10
  • 25
  • See https://stackoverflow.com/questions/17209260/converting-java-util-properties-to-hashmapstring-string – PM 77-1 Dec 18 '17 at 18:14
  • Two possible approaches. (1) Load all the properties first, then iterate through the resulting `Map`, copying the properties you need to a new `Map`. (2) Write a wrapper class that implements `Map`, stores all the properties, but adds `emulator.Android.driver` when it does the lookup. – Dawood ibn Kareem Dec 18 '17 at 18:27

1 Answers1

0

Got it figured out. Here is the resulting code.

    sSearchString = "emulator.Android.driver.";
    mDriverProperties = new HashMap<String, String>();
    Set<String> sKeys = pTestProperties.stringPropertyNames();
    for (String sKey : sKeys)
    {
        if (sKey.startsWith(sSearchString))
        {
            sKey = sKey.substring(sKey.lastIndexOf(".") + 1);
            mDriverProperties.put(sKey, pTestProperties.getProperty(sSearchString + sKey));
        }
    }
GregMa
  • 740
  • 2
  • 10
  • 25