Is there an alternative way to my below code? - robotframework

I am looking for an alternative and smarter way to achieve the below action
***Keywords***
Insert Values
[Documentation] Keyword is used to insert value
${Status1} Run Keyword and Return Status Wait Until Element is Visible ${ONPDonorLocator} 1s
${Status2} Run Keyword and Return Status Wait Until Element is Visible ${CustomerScoreLocator} 1s
${Status3} Run Keyword and Return Status Wait Until Element is Visible ${ContractDurationLocator} 1s
${Status4} Run Keyword and Return Status Wait Until Element is Visible ${OptionsInstalledLocator} 1s
${Status5} Run Keyword and Return Status Wait Until Element is Visible ${OrderAddsDeletesLocator} 1s
${Status6} Run Keyword and Return Status Wait Until Element is Visible ${SiteCategoryLocator} 1s
Run Keyword If '${Status1}'=='True' Wait and Click ${ONPDonor} ${LocatorWaitTime}
Run Keyword If '${Status2}'=='True' Wait and Click ${CustomerScore} ${LocatorWaitTime}
Run Keyword If '${Status3}'=='True' Wait and Click ${ContractDuration} ${LocatorWaitTime}
Run Keyword If '${Status4}'=='True' Wait and Click ${OptionsInstalled} ${LocatorWaitTime}
Run Keyword If '${Status5}'=='True' Wait and Click ${OrderAddsDeletes} ${LocatorWaitTime}
Run Keyword If '${Status6}'=='True' Wait and Click ${SiteCategory} ${LocatorWaitTime}
Expected:
Is it possible to click the locator that has '${Status}'=='True' instead of writing Status1, Status2, Status3 and so on?
I do not want to write the way I have written above. Tomorrow if I have to check status of few more locators then the lines will keep on getting added.
Note: I am still learning, so any suggestions are welcome.

Yes, by using a loop and storing only the locators that are visible; then you loop over the stored ones, and click them:
${visible}= Create List
FOR ${locator} IN ${ONPDonorLocator} ${CustomerScoreLocator} # etc, the others
${Status} Run Keyword and Return Status Wait Until Element is Visible ${locator}
Run Keyword If ${Status} Append To List ${visible} ${locator}
END
FOR ${locator} IN #{visible}
Wait and Click ${locator}
END
When you have more elements to wait for visibility and then click, you just add them in the first loop.

Related

I wane step by step testcase nname in terminal

describe('open popup', ()=>{
it('click minimize button',()=>{
cy.visit('url')
cy.get('minimized-button').click() //it is minimize tab click
var iframesrc=cy.get('#modal-iframe').invoke('attr','src')
cy.get('email').type('jhdsj#gamil.com') //here I want it block
for confirmation of
entering email
})
})
when I am trying to enter in Iframe,not able to locate any element even i use wrap also...what i want is entering in loop then want step by step it block with testcase name that confirm in terminal that which step is passed in popup and which one is fail.

For Loop is not looping untill the given range in robot framework

sleep till Rest api
FOR ${i} IN RANGE 20
Sleep 5s till rest api is available
${result} = Check Rest Api
Exit For Loop If ${result} == True
Log to Console try again in 5 sec
END
I am using For loop to sleep for some time until the api is available and exit from loop once it has got the response. For this I had stored the result of the keyword in a varibale and gave exit from loop when result becomes true. But, whats happening is first it waits for 5 seconds and checks the result and fails. After failing once it is not iterating and fails at checking the result itself.
Here when it goes through the second line in for loop ( ${result} = Check Rest Api ). The sleep time is not sufficient and it fails at that statement itself. What I am expecting is the loop should continue until it gets the response.

How to use values from one keyword to another keyword in robot framework

I would like to use value from keyword 1 to keyword 2. Tried searching on net but i could not able to solve it.
Verify that apps are listed
wait until element is visible ${AppMenuGrid} ${Timeout} "Apps NOT listed. Step execution failed"
log "Apps listed"
${APPSCOUNT} = GET ELEMENT COUNT ${AppMenuGrid}
log "Number of apps loaded are ${APPSCOUNT}"
[Return] ${APPSCOUNT}
Click on Refresh button
wait until element is visible ${Refresh} ${Timeout} "Refresh button is not visible"
click element ${Refresh}
log "click on refresh button successful"
Verify that same apps are listed
wait until element is visible ${AppMenuGrid} ${Timeout} "Apps list not refreshed. Step execution failed"
log "Apps list refreshed"
${APPSRECOUNT} = GET ELEMENT COUNT ${AppMenuGrid}
${Count} = verify that apps are listed ${APPSCOUNT}
log "Number of apps before refresh ${Count}"
log "Number of apps after refresh ${APPSRECOUNT}"
run keyword if "${APPSRECOUNT}" == "${Count}" log "Number of apps matching after refresh"
... ELSE fail "All apps not loaded after refresh"
I want to use AppsCount value (ex .10) from keyword "Verify that apps are listed" into "Verify that same apps are listed" keyword. But in the 2nd keyword, APPSCOUNT value is always blank.
Change the keyword Verify that same apps are listed to accept arguments:
Verify that same apps are listed
[Arguments] ${expected appscount}
# the rest of its code
And then, in the case where it's used, pass the value from the first keyword:
A case
${the count}= Verify that apps are listed
Verify that same apps are listed ${the count}
I agree with Todor Minakov's approach, to share the value via return clauses. Here is another approach:
Robot Framework (as described in the User Guide) has notion of variable scope: Local (Keyword) level, Test case level, Test suite level and Global. By default, the variables defined in the keywords have local scope.
To share the value of the variable between two keywords, just add a test case scope to the variable, like this:
Verify that apps are listed
wait until element is visible ${AppMenuGrid} ${Timeout} "Apps NOT listed. Step execution failed"
log "Apps listed"
${APPSCOUNT} = GET ELEMENT COUNT ${AppMenuGrid}
Set Test Variable ${APPSCOUNT}
Then, you can call ${APPSCOUNT} inside any other keyword in the same test case and it will have the stored value.
i tried the following and it worked.
In the test case file, i added a variables with the same name ${APPSCOUNT} and set the variables to the keyword like below,
Verify that apps are listed ${APPSCOUNT}
After this i can see value from keyword 1 in keyword 2.
Is this the correct approach?

How to run keywords into multiple times

My scenario:
There is field called "search first value" i have to click on Then click on Submit.
I have to repeat this step more than 100 times. How can i do in Robot framework?
MY code:
[Documentation] Resubmit
wait until element is enabled ${CLICK_RESUBMIT}
click element ${CLICK_RESUBMIT}
sleep 2s
confirm action
repeat keyword 5times Go to Previous Page
Standard library have keyword Repeat Keyword
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Repeat%20Keyword

Select window created using JavaScript

In My Robot Script , After clicking on Edit Description link a window get opens (i.e. Java Script Window) Images here
Image 1::
Image 2:: Window image Along with Page Source
What i need is a. Select the window (java script)
b. Enter text into Text Area and click Ok.
I tried to use Select Window new/ Select window Description But control is waiting infinite # Select Window key word its not moving forward. I have to kill Control forcefully.
*** Settings ***
Documentation Suite description
Library Selenium2Library run_on_failure=Nothing
Library Collections
*** Test Cases ***
Log into Command center
[Tags] DEBUG
open browser http://11.8.180.***/BCC ie
input text xpath=//*[#id='LoginName'] User
input password Password Pwd#123
click element LoginBtn
wait until keyword succeeds 30 sec 5 sec page should contain element AppTitle
sleep 3s
${Title} Get Page Title
log ${Title}
Navigation to Usrs
Click on Window popup and enter text
CloseBrowser
close all browsers
*** Keywords ***
Click on Window popup and enter text
Click Link //*[#id="aspnetForm"]/table/tbody/tr[1]/td[2]/a
Sleep 5s
Select Window By Unique Identifier Element With Delay //*[#id="descript"]
Select Window By Unique Identifier Element With Delay
[Arguments] ${element} ${delay_time}=10
sleep ${delay_time}
#{wins}= Get Window Handles
Log ${wins}<--these are window names console=true
:FOR  ${windowName}  IN  #{wins}
\   Log ${windowName} console=true
\ run keyword and ignore error Select Window  ${windowName}
\   ${found_flag}= run keyword and return status page should contain  ${element}
\   Run keyword if ${found_flag} input text ${element} this is text
\   exit for loop if ${found_flag}
Get Page Title
Log Control came to GET PAGE TITLE
run keyword and return Get Title
Navigation to Usrs
Go to http://11.8.180.***/BCC/role.aspx
Any Idea how to select new window? and enter text hit okay.
A.Get Window Identifiers --> Jus hangs it neither moves nor throw any error
B. Get Window Names gives this error {URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>..}.
C. Get Window Handles returns this { [u'b0ce0f28-f6d4-4943-93f7-f65fd5f1de2a', u'c0442110-4ea9-40a4-b126-eee09d9f3eb0']}
I am not sure how to identify which window have these id's
Thanks in Advance
It should be recognized as new window in your browser, so you can use:
Select Window Description

Resources