0

I am currently automating an application that is available across multiple platforms. We have a common JSON file per page denoting the elements locator as below:

{
"XYZ": {
    "default": "xpath=//*[@id='root']",
    "iOS": "css=#name",
    "android": "name=//*[@class='android.widget.']",
    "web": "id=testing",
    "mobileWeb": "class=mobileLogin",
    "test":""
},
 "ABC": {
    "default": "xpath=//*[@id='root']/cyz",
    "android": "name=//*[@class='android.widget.']",
    "web": "id=testing",
    "mobileWeb": "class=mobileLogin",
    "test":""
} }

Assuming the platform is iOS, I can retrieve the locator for XYZ, data['XYZ']['iOS]. I cannot do the same for ABC because the locator for iOS is not defined. In this case, I need to pick the value for the default one.

The challenge here is to find out if iOS is defined for ABC or not. How should I approach?

  • You get it just like the one with a platform, my_dict["xyz"]["default"]. What's that different from my_dict["xyz"]["iOS"]? – pavelsaman Oct 15 at 20:42
  • 1
    @pavelsaman I have edited the question for more clarity. The idea is to first see if there is a locator defined for the platform(e.g iOS) and if not get the default value. – Ranadheer Oct 16 at 9:00
  • Ok, I posted one option as an answer. See for yourself if this works for you. – pavelsaman Oct 16 at 10:06
1

If you are certain that XYZ and the default value will always be in the dict, you can use get() method like so:

my_dict = {
    "XYZ": {
        "default": "xpath=//*[@id='root']",
        "iOS": "css=#name",
        "android": "name=//*[@class='android.widget.']",
        "web": "id=testing",
        "mobileWeb": "class=mobileLogin",
        "test":""
    }
}

my_dict_default = {
    "XYZ": {
        "default": "xpath=//*[@id='root']",        
        "android": "name=//*[@class='android.widget.']",
        "web": "id=testing",
        "mobileWeb": "class=mobileLogin",
        "test":""
    }
}

print(my_dict.get("XYZ").get("iOS", my_dict["XYZ"]["default"]))
print(my_dict_default.get("XYZ").get("iOS", my_dict_default["XYZ"]["default"]))

The first print statement will give you css=#name, the second will give you the default value xpath=//*[@id='root'].

There are multiple other approaches you can find e.g. here: https://stackoverflow.com/questions/16003408/how-to-use-dict-get-with-multidimensional-dict

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.