Rajeev Dixit Rajeev Dixit - 6 months ago 8
Python Question

How to iterate over the next element in the map in the inner loop, while coming from the next element of the map in the outer loop?

I want to replace the new link by the old one, which can be random, but both link should not be the same.

The logic I have written is iterating over the inner loop again, which can cause the link to be set as the same as previous one.

In the function I am passing the value in dictionary format-

${oldLinks}= Set Variable {"Facebook":"https://www.facebook.com/","Stack Overflow":"http://stackoverflow.com/"}
${newLinks}= Set Variable {"Discovery":"http://www.discovery.com/","HowStuffWorks":"http://www.howstuffworks.com/"}


This is the function I have written in python + Robot framework -

def edit_favorites(d,oldLinks,newLinks):
oldLinks=json.loads(oldLinks)
newLinks=json.loads(newLinks)
BookmarkLinks=oldLinks
NewBookarks=newLinks
d.press.back()
d(hamBergBtn).click()
d(favorites).click()
for key,val in BookmarkLinks.iteritems():
logger.info("Editing the favorites",html=False,also_console=True)
logger.info("key url"+key,also_console=True)
# if(d({'textContains':key}))==None:
# d({'scrollable':True}).scroll.toEnd()
for key1,val1 in NewBookarks.iteritems():
d({'textContains':key}).long_click()
if d({'textContains':"Copy link URL"}).exists & d({'textContains':"Edit"}).exists & d({'textContains':"Remove"}).exists & d({'textContains':"Cancel"}).exists:
logger.info("coming here, to edit",also_console=True)
d({'text':'Edit'}).click()
d({'resourceId':'com.ex.browser:id/title'}).click()
if d({"focused":"true"}):
d({'resourceId':'com.ex.browser:id/title'}).set_text(key1)
d({'resourceId':'com.ex.browser:id/address'}).click()
if d({"focused":"true"}):
d({'resourceId':'com.ex.browser:id/address'}).set_text(val1)
d({'text':'Save'}).click()
break
else:
raise RuntimeError,"Pop up doesn't contain the required fields"


Is there any other way to achieve what I am trying to do?

Answer

You can try this-

def edit_favorites(d,oldLinks,newLinks):
oldLinks=json.loads(oldLinks)
newLinks=json.loads(newLinks)
OldBookmarkLabel=oldLinks.keys()
OldBookmarkURL=oldLinks.values()
NewBookarksLabel=newLinks.keys()
NewBookarksURL=newLinks.values()
# d.press.back()
for i in range(len(OldBookmarkLabel)):
    logger.info("Editing the favorites",html=False,also_console=True)
    logger.info("key url "+OldBookmarkLabel[i],also_console=True)
    # if(d({'textContains':key}))==None:
    #     d({'scrollable':True}).scroll.toEnd()
    d({'textContains':OldBookmarkLabel[i]}).long_click()
    if d({'textContains':"Copy link URL"}).exists & d({'textContains':"Edit"}).exists & d({'textContains':"Remove"}).exists & d({'textContains':"Cancel"}).exists :
        logger.info("coming here, to edit",also_console=True)
        d({'text':'Edit'}).click()
        d({'resourceId':'com.citrix.browser:id/title'}).click()
        if d({"focused":"true"}):
            d({'resourceId':'com.citrix.browser:id/title'}).set_text(NewBookarksLabel[i])
        d({'resourceId':'com.citrix.browser:id/address'}).click()
        if d({"focused":"true"}):
            d({'resourceId':'com.citrix.browser:id/address'}).set_text(NewBookarksURL[i])
        d({'text':'Save'}).click()
     else:
        raise RuntimeError,"Pop up doesn't contain the required fields"