2

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)
5
  • 1
    Have you considered something like el = find_element_by_id(mediatype+"_playbutton"); ... ; el.click()? In ..., you'd want to check whether you actually found something, of course. Commented Jun 13, 2016 at 14:21
  • sorry should have mention that. The problem is the ID's are not very accurate. I will give a real example: video playbutton is called "imgPlayPause" while image playbutton is called "imgPlayPause_mif" but then document playbutton is called "docPlay_mif" Commented Jun 13, 2016 at 14:27
  • OK, but if you know those things ahead, you could still populate a dict() in one place, e.g., ids = dict(); ids['video'] = 'video_playbutton'; ids['audio'] = 'audio_playbutton'; ... ; and then later do el = find_element_by_id[ids[mediatype]]; ...; el.click(). Then you the lookup code is simple, and the only tedious part is generating that mapping. That mapping has to exist somewhere, and it's probably much more flexible to keep it as a mapping than as a bunch of if/elif/else statements. Commented Jun 13, 2016 at 14:31
  • dicts sound great. But im getting headaches. From my understanding I need a multidimensional dict. Like ids['video'] = [ 'playbutton' , 'stopbutton' , 'nextbutton' ] is that right? If yes at least I know im going to the right direction and will help me alot. Commented Jun 13, 2016 at 15:47
  • Sure, some kind of nested dict, or a record type, etc. Commented Jun 13, 2016 at 16:26

1 Answer 1

2

You can separate out the selectors and operations in two dictionaries which scales better. Otherwise the mapping eventually gets huge. Here is the example.

dictMedia = {'video':['video_playbutton', 'video_stopbutton','video_nextbutton'], 'audio':['audio_playbutton', 'audio_stopbutton', 'audio_nextbutton']}
dictOperations = {'play':0, 'stop':1, 'next':2}
def get_selector(mediatype, operation):
    return dictMedia[mediatype][dictOperations[operation]]

print get_selector('video', 'play')

PS: The above operation doesn't check for key not found errors. However, I still feel, if the media specific operations grow, then a page object model would be better.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks to Joshua Taylor and Vinayak Kolagi I could solved the issue. I was already doing something similiar to the solution above except that I didn't think of the second dict. Now I got it running and works like a charm :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.