Condition under if-elif is not working.

Asked by Atul Desai

Hi,
I am trying to select a radio button option for a question using if-elif condition block but the program control is never transferred to if-elif block.Below is my code.

Sript1:

def FeedbackQuestion(question):
    find(question)
    num = random.randint(1,5)
    print(num)
    if num==1:
       click(radioOption1)
    elif num==2:
      click(radioOption2)
    elif num==3:
      click(radioOption3)
    elif num==4:
      click(radioOption4)
    elif num==5:
      click(radioOption5)
    print("question and option selection was successful")

Script2:
import script1

script1.FeedbackQuestion(script1.question2)

Can you please help me to resolve this issue.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

supposing script1 at least contains:

from sikuli import *
# …
question2 = <some image or pattern>
# …
radioOption1 = <some image or pattern>
# same for the other radioOptionN
# …
 def FeedbackQuestion(question):
    find(question)
    num = random.randint(1,5)
    print(num)
    if num==1:
       click(radioOption1)
    elif num==2:
      click(radioOption2)
    elif num==3:
      click(radioOption3)
    elif num==4:
      click(radioOption4)
    elif num==5:
      click(radioOption5)
    print("question and option selection was successful")

then in script2

import script1
script1.FeedbackQuestion(script1.question2)

should either click one of the five buttons or should throw a FindFailed exception on question2 or the respective button image.

BTW: a shortcut:
def FeedbackQuestion(question):
    find(question)
    num = random.randint(1,5)
    print "button to click:", num
    eval("click(radioOption%d)" % num)

Revision history for this message
Atul Desai (atuld) said :
#2

Still the option is not selected though the scripts runs successfully.

Revision history for this message
Atul Desai (atuld) said :
#3

Hi RaiMan,

Also the code doesn't throw any exception. and Yes script1 contains

question2 = <some image or pattern>
radioOption1 = <some image or pattern>

but then too it doesn't work.

Tried with your suggested code, but didn't work.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

from sikuli import *

in script 1?

Revision history for this message
RaiMan (raimund-hocke) said :
#5

this works as expected:

import random
def FeedbackQuestion(question):
# find(question)
    num = random.randint(1,3)
    print "button to click:", num
    eval("hover(radioOption%d)" % num)

radioOption1 = "1393859382905.png"
radioOption2 = "1393859401498.png"
radioOption3 = "1393859424499.png"

for i in range(5):
    FeedbackQuestion(None)

*** output:
button to click: 2
button to click: 1
button to click: 3
button to click: 2
button to click: 2

… and the mouse moves to the respective match

I ran it in slow motion to see the hover targets

Revision history for this message
Atul Desai (atuld) said :
#6

Hi RaiMan,
Thanks for your help the above code worked and is selecting the checkboxes randomly.
But now I am facing one more issue in we have optional field (a textarea) with each question and now my script is not able to distinguish between first question's textarea and second question's textarea, both have same title and size because of which the script is failing.

Example :

Q1 : Please rate
radioButton1 : Excellent
radioButton2 : Very Good
..
..
..
..
radioButtonn: poor
Comment (Textarea): ____________

Q2 : Please rate 2
radioButton1 : Excellent
radioButton2 : Very Good
..
..
..
..
radioButtonn: poor
Comment (Textarea): ____________

I wrote below script but the code never enters if-elif condition block.

question1 = "1393923818615.png"
question2="1393843893700.png"
question3="1393843916859.png"
question4="1393843940775.png"
question5="1393843970900.png"
question6="1393843997667.png"

radioOption1 = "1393923605578.png"
radioOption2 = "1393923615559.png"
radioOption3 = "1393923628368.png"
radioOption4 = "1393923639294.png"
radioOption5 = "1393923648294.png"
radioOption6 = "1393936753939.png"
radioOption7 = "1393936763960.png"
radioOption8 = "1393936775294.png"
radioOption9 = "1393936785976.png"
radioOption10 = "1393936797938.png"

commentsField = "1393935801391.png"
commentFieldRegion1 = Region(235,220,970,314)

commentFieldRegion2 = Region(232,79,928,305)

options = {1 : radioOption1,
                2 : radioOption2,
                3 : radioOption3,
                4 : radioOption4,
                5 : radioOption5,
}

def FeedbackQuestion(ques, min, max):
    find(ques)
    num = random.randint(min,max)
    print "button to click:", num
    eval("click(radioOption%d)" % num)
    test = ''.join(random.choice(string.ascii_lowercase) for x in range(4))
    if ques == question2:
        q = 1
        print "entered block2"
        type(Key.HOME)
        loc = SCREEN.getCenter()
        wheel(loc, WHEEL_DOWN, 6)
        eval("click(commentFieldRegion%d.find(commentsFields))" %q)
        type(commentsField,test)
    elif ques == question3:
        q = 2
        print "entered block2"
        type(Key.HOME)
        loc = SCREEN.getCenter()
        wheel(loc, WHEEL_DOWN, 6)
        eval("click(commentFieldRegion%d.find(commentsFields))" %q)
    type(commentsField,test)
    print "radiobutton selected"

Can you please help me to resolve this issue.

Revision history for this message
Atul Desai (atuld) said :
#7

Please ignore following code from previous comment

options = {1 : radioOption1,
                2 : radioOption2,
                3 : radioOption3,
                4 : radioOption4,
                5 : radioOption5,
}

and I am using below code to call FeedbackQuestion method in another script.

script1.FeedbackQuestion(script1.question1,1,5)
script1.FeedbackQuestion(script1.question2,6,10)
script1.FeedbackQuestion(script1.question3,6,10)

Revision history for this message
RaiMan (raimund-hocke) said :
#8

possible to get a screenshot? (if you do not want to post it public, just silently send a mail to the mail at https://launchpad.net/~raimund-hocke)

Revision history for this message
Atul Desai (atuld) said :
#9

Hi RaiMan,
I've sent you an email with screenshot, did u get it?

Revision history for this message
Best RaiMan (raimund-hocke) said :
#10

sorry, I replied, but did not attach anything ....
just resent it

Revision history for this message
Atul Desai (atuld) said :
#11

Thanks RaiMan, that solved my question.