Dynamic Use of Global Variable in Robot Framework - robotframework

I am trying to utilize a global variable that I set by passing it in as an argument to a keyword but it is not working for some reason. Here's the code:
*** Variables ***
${ENV} qa
${MOBILE} 0
${BROWSER} Chrome
${DELAY} 0
${VALID USER} username
${VALID PASSWORD} 123456
&{SERVER} qa=https://${PREFIX}.qa.myapp.com
... staging=https://${PREFIX}.staging.myapp.com
... prod=https://${PREFIX}.myapp.com
${LOGIN URL} ${SERVER.${ENV}}/
${WELCOME URL} ${SERVER.${ENV}}/Profile
*** Keywords ***
Begin Web Test
[Arguments] ${pf}
[Tags] Critical
Set Global Variable ${PREFIX} ${pf}
Open Browser ${LOGIN URL} ${BROWSER}
Run Keyword If ${MOBILE} == 1
... Set Window Size 50 800
... ELSE Maximize Browser Window
Set Selenium Speed ${DELAY}
Login Page Should Be Open
Input Text username ${VALID USER}
Input Text password ${VALID PASSWORD}
Click Button btn_login
Location Should Be ${WELCOME URL}
Page Should Contain Update Profile
When I run this I receive an error:
[ ERROR ] Error in file '/path/to/Common.robot': Setting variable '&{SERVER}' failed: Variable '${PREFIX}' not found.
Could someone explain why this doesn't work?

The variables in the *** Variables *** section are static, and only set once prior to the start of the first test. You cannot expect the variables to automatically update when you change the value of ${PREFIX}.
One solution would be to move the setting of the variables into a keyword that you can call either after defining the global variable, or by passing in the value of ${PREFIX}.

Related

robot framework doesn't see user defined keywords if they are in the same file as testcase

Hi ' I'm newbie in robot framework. I try to declare keywords in the same file as testcases. But robotframework doesn't see them. I use robotframework 3.2.2 Please advice:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
*** Test Cases ***
User must sign in to check out
[Documentation] This is some basic info about the test
[Tags] Smoke
Begin Web Test
Search for Products
Select Product from Search Results
Add product to Cart
Begin checkout
End Web Test
*** Keywords ***
Begin Web Test
Open Browser about:blank ie
Search for Products
Go To http://amazon.com
Wait Until Page Contains Today's Deals
Input Text Ferrari 458
Click Button xpath=//*[#id="nav-search-submit-text"]/input
Wait Until Page Contains results for "Ferrari 458"
Select Product from Search Results
Click Link xpath=//*[#id="search"]/div[1]/div[2]/div/span[3]/div[2]/div[2]/div/span/div/div/div[2]/h2/a
Wait Until Page Contains Back to results
Add product to Cart
Click Button id=add-to-cart-button
Wait Until Page Contains Added to Cart
Begin checkout
Click Link Proceed to checkout (1 item)
Wait Until Page Contains Continue
End Web Test
Close Browser
[Documentation] and [Tags] need to be indented.
There should be at least 2 spaces between [Documentation] and "This is some basic info about the test"
There should also be at least 2 spaces between [Tags] and Smoke
User must sign in to check out
[Documentation] This is some basic info about the test
[Tags] Smoke
Begin Web Test
Search for Products
Select Product from Search Results
Add product to Cart
Begin checkout
End Web Test

Return in Robot Framework

I am trying to return a value from a keyword to my test case, but I am getting error
Variable 'xxx' not found.
This is my code.
*** Keywords ***
Click Dispatched check box from Status
${dispatchOptionPresent}= run keyword and return status click element ${selectDispatchedCheckbox_xpath}
set global variable ${g_dispatchOptionPresent} ${dispatchOptionPresent}
run keyword if '${dispatchOptionPresent}' == 'False' NoDispatchedStatusFoundforSelection
... ELSE Set Test Message *HTML* Applied <b>'Dispatched'</b> status filter
click element xpath://body
sleep 2
NoDispatchedStatusFoundforSelection
Fail 'Dispatched' status option not found...
[Return] ${g_dispatchOptionPresent}
*** Test Cases ***
Click 'Dispatched' check box for Status
[Tags] Filter-From Date/Status for LOA
[Documentation] Test to select 'Dispatched' check box for Status
${dispatchStatusFound}= Click Dispatched check box from Status
set global variable ${g_dispatchStatusFound} ${dispatchStatusFound}
pass execution if '${g_dispatchStatusFound}' == 'False' *HTML* <b>'Dispatch'</b> status found for selection!!!
For some reason, the test case is not seeing the returned value from the keyword section.
I checked for any formatting issues also, but no luck.
A similar scenario in one of my another test case works correctly with [Return]
But this one throws me :
Variable '${g_dispatchStatusFound}' not found.
Any help is much appreciated.
Thank you.

Robot framework, chrome new tab issue

I have a simple Robot Framework script
*** Settings ***
Documentation Simple Amazon.in demo
Library SeleniumLibrary
*** Variables ***
${MESSAGE} Hello, World
*** Test Cases ***
User must sign in to check out
[Documentation] This is some basic info about the test
[Tags] Smoke
Open Browser http://www.amazon.in chrome
Input text id=twotabsearchtextbox Ferrari 458
Click Button xpath=//div[#class='nav-search-submit nav-sprite']/input[#class='nav-input' and 1]
Wait until page Contains results for "Ferrari 458"
Click Link css=#result_0 a.s-access-detail-page
Wait until Page Contains Back to search results for "Ferrari 458"
Click Button id=add-to-cart-button
Wait Until Page Contains 1 item added to Cart
But whenever chrome reaches Click Link css=#result_0 a.s-access-detail-page it opens a new tab, and my robot script fails. How can I rectify it. Please help
You can use the select window keyword and Get Window Titles keyword to navigate between them Get Window Titles keyword will return a list of titles the last index in that list is the new tab that has been opened, to access it from the list you can do the following ${Tabs[1]} (as in this code there is only 2 values in the list)
*** Settings ***
Documentation Simple Amazon.in demo
Library SeleniumLibrary
*** Variables ***
${MESSAGE} Hello, World
*** Test Cases ***
User must sign in to check out
[Documentation] This is some basic info about the test
[Tags] Smoke
Open Browser http://www.amazon.in chrome
Input text id=twotabsearchtextbox Ferrari 458
Click Button xpath=//div[#class='nav-search-submit nav-sprite']/input[#class='nav-input' and 1]
Wait until page Contains results for "Ferrari 458"
Click Link css=#result_0 a.s-access-detail-page
${Tabs} = Get Window Titles
select window title=${Tabs[1]}
Wait until Page Contains Back to search results for "Ferrari 458"
Click Button id=add-to-cart-button
# Wait Until Page Contains 1 item added to Cart
Wait Until Page Contains Added to Cart
Results:
==============================================================================
Amazon :: Simple Amazon.in demo
==============================================================================
User must sign in to check out :: This is some basic info about th...
DevTools listening on ws://127.0.0.1:29864/devtools/browser/75b8be3c-6e76-474f-b391-d340fb322895
User must sign in to check out :: This is some basic info about th... | PASS |
------------------------------------------------------------------------------
Amazon :: Simple Amazon.in demo | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: C:\development\robot-scripts\sssss\output.xml
Log: C:\development\robot-scripts\sssss\log.html
Report: C:\development\robot-scripts\sssss\report.html
I changed the last line of your code as it wasn't a valid text. See the comment in the code.

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

Unable to startup test. Unknown tests gets called for Robot Framework Test Execution

I'm surprised to see this error message when executing Test Automation using RobotFramework with RED editor. Plz guide me on this issue getting rectified.
I dont have any test case called "Filter the customer". When running my suite it shows up the below error.
Note: I did the below, even then I'm having this issue.
1. I cleaned the Project
2. I also deleted and re-added the modified libraries.
3. Saved all the updated files.
Error:
The source is as below,
*** Setting ***
Resource ../KCC_Automation/ResourceFiles/KCC_Resource.txt
*** Test Cases ***
Login with valid credentials
Logon to KCC browser
Enter User Name
Enter Password
Click Login
Show Customers available in Customer List Page
Get the customer names and click them from Customer List Page
Begin the New Assessment for an opportunity
Filter by Customer Search
Expand the customer details and begin the New Assessment
Stop Testing
Logout of KCC
And the resource file is as below,
*** Keywords ***
Welcome1
${message} say hi
Log ${message}
Welcome2
${message} say hi Testers
Log ${message}
Welcome3
${message} type of 42
Log ${message}
Logon to KCC browser
Open Browser ${LOGIN URL} ${BROWSER}
Wait Until Page contains Element ${UserName_Field}
Enter User Name
Input Text ${UserName_Field} ${USERNAME}
sleep 2s
Enter Password
Input Text ${Password_Field} ${PASSWORD}
sleep 2s
Click Login
Click Element ${LoginButton}
sleep 5s
Get the customer names and click them from Customer List Page
set selenium speed 5s
wait until element is visible ${customers_grid}
wait until page contains element ${customers_grid}
sleep 3s
#{customers} get webelements ${customer_list}
${items} Store Texts #{customers}
#${items} storeTexts #{customers}[2]
${store} get text ${items}
Log ${store}
Filter by Customer Search
set selenium speed 5s
input text ${Customer_Search} ${Search_Text}
sleep 3s
${Entered_text}= get value ${Customer_Search}
should be equal ${Search_Text} ${Entered_text} The given message is incorrect true
Expand the customer details and begin the New Assessment
wait until page contains element ${customer_list}
click element ${customer_list}
sleep 2s
click element ${opportunity-status}
sleep 2s
click element ${forward_button}
sleep 2s
click element ${Btn_Add_Building}
sleep 2s
Logout of KCC
In the source file, I re-placed the test case name "Begin the New Assessment for an opportunity" by "filter the customers" as because earlier i named this testcase like this, executing it showing up below issue now, Is this RED editor issue?
[ ERROR ] Unexpected error: NoClassDefFoundError:
org/openqa/selenium/WebElement
java.lang.NoClassDefFoundError:
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetPublicMethods(Class.java:2902)
at java.lang.Class.getMethods(Class.java:1615)
at robot.utils.importer$py.import_$32(C:\jython2.7.0\Lib\site-packages\robot\utils\importer.py:274)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\utils\importer.py)
at robot.utils.importer$py._import_class_or_module$5(C:\jython2.7.0\Lib\site-packages\robot\utils\importer.py:77)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\utils\importer.py)
at robot.utils.importer$py.import_class_or_module$4(C:\jython2.7.0\Lib\site-packages\robot\utils\importer.py:74)
at robot.utils.importer$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\utils\importer.py)
at robot.model.visitor$py.visit_suite$2(C:\jython2.7.0\Lib\site-packages\robot\model\visitor.py:88)
at robot.model.visitor$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\model\visitor.py)
at robot.model.testsuite$py.visit$19(C:\jython2.7.0\Lib\site-packages\robot\model\testsuite.py:161)
at robot.model.testsuite$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\model\testsuite.py)
at robot.model.itemlist$py.visit$11(C:\jython2.7.0\Lib\site-packages\robot\model\itemlist.py:75)
at robot.model.itemlist$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\model\itemlist.py)
at robot.model.visitor$py.visit_suite$2(C:\jython2.7.0\Lib\site-packages\robot\model\visitor.py:88)
at robot.model.visitor$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\model\visitor.py)
at robot.model.testsuite$py.visit$19(C:\jython2.7.0\Lib\site-packages\robot\model\testsuite.py:161)
at robot.model.testsuite$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\model\testsuite.py)
at robot.utils.application$py._execute$10(C:\jython2.7.0\Lib\site-packages\robot\utils\application.py:94)
at robot.utils.application$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\utils\application.py)
at robot.utils.application$py.execute_cli$5(C:\jython2.7.0\Lib\site-packages\robot\utils\application.py:49)
at robot.utils.application$py.call_function(C:\jython2.7.0\Lib\site-packages\robot\utils\application.py)
at runpy$py._run_code$9(C:\jython2.7.0\Lib\runpy.py:73)
at runpy$py.call_function(C:\jython2.7.0\Lib\runpy.py)
at runpy$py._run_module_as_main$14(C:\jython2.7.0\Lib\runpy.py:161)
at runpy$py.call_function(C:\jython2.7.0\Lib\runpy.py)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebElement
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 191 more
Looking at the error it seems to me that you don't have the Selenium2Library loaded Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebElement You can load a library by using the syntax:
*** Settings ***
Library Selenium2Library
Please keep in mind that this library requires either a pip install robotframework-selenium2library or to install the Java Port of the Selenium2Library
Although I can't be sure, I suspect that when this issue is resolved your other problem is also fixed.

Resources