I am trying to write a automation script in python whose steps are described in a xml document. but when i tried to run the parsed xml data as command in python , it is treating parsed data as string and not executing it. I am using xml.etree.ElementTree to parse data from xml.
my xml code is
<Setting >
<device.startActivity name="('com.android.settings/.Settings')" />
<device.press name="('KEYCODE_DPAD_DOWN')" />
<vc.dump name="()" />
<vc.findViewWithText name="('About phone')">.touch(</vc.findViewWithText>
<device.press name="('KEYCODE_DPAD_DOWN')" />
<device.press name="('KEYCODE_DPAD_DOWN')" />
<vc.dump name="()" />
<vc.findViewWithText name="('Android verion')">.getParent().getChildren()[1].getText()</vc.findViewWithText>
</Setting>
and i am using following code to parse and execute it.
Settings = ET.ElementTree(file = configration_file_name).getroot()
length = 0
for ui_application in Settings:
if length == len(Settings) - 2:
break
else:
length +=1
if ui_application.text != None :
ui_application.tag+ui_application.attrib['name']+ui_application.text
elif ui_application.attrib['name'] !=None:
ui_application.tag + ui_application.attrib['name']
else:
ui_application.tag
Is there any better way to achieve this task?
Indeed concatenating string will just result in another string. To run string containing python codes as python expression, you need to use exec()
function, for example :
....
else:
exec(ui_application.tag)
or if you expect the expression to return value, you can use eval()
instead of exec()
:
....
else:
result = eval(ui_application.tag)