lundi 13 juin 2016

Python How to avoid many if statements

I'll try to simplify my problem. I'm writing a test program using py.test and appium. Now:

In the application I have 4 media formats: Video, Audio, Image and Document. I have a control interface with previous, next, play , stop buttons. Each media formats has a unique ID like

video_playbutton, audio_playbutton, document_playbutton, image_playbutton, video_stopbutton audio_stopbutton ...etc etc.

But the operation I have to do is the same for all of them e.g press on playbutton.

I can address playbutton of each when i give them explicitly like this

find_element_by_id("video_playbutton")

And when i want to press on other playbuttons I've to repeat above line each time. Like this:

find_element_by_id("video_playbutton")
find_element_by_id("audio_playbutton")
find_element_by_id("image_playbutton")
find_element_by_id("document_playbutton")

And because I'm calling this function from another script I would have to distinguish first what string I got e.g:

def play(mediatype):
    if mediatype == "video"
          el = find_element_by_id("video_playbutton")
          el.click()
    if mediatype == "audio"
          el = find_element_by_id("audio_playbutton")
          el.click()
    if .....

What is the best way to solve this situation? I want to avoid hundreds of if-statements because there is also stop, next , previous etc buttons.

I'm rather searching for something like this

def play(mediatype)
    find_element_by_id(mediatype.playbutton)

Aucun commentaire:

Enregistrer un commentaire