Can i verify a list of xpaths at once? - selenium

I'm new using Robot Framework, so I created a Test Case to verify that a banch of elements exists on the target web page, so my question is instead of doing :
Page Should Contain Element ${text_field1}
Page Should Contain Element ${text_field2}
Page Should Contain Element ${text_field3}
....
can I do a list with these xpaths and verify if this list exists on the web page at once ?
is there any robotframework function to do that ? thanks

No, we don't have a keyword for multiple checking elements in pages. But we can create that logic in an user keyword. See below a fully working example.
*** Settings ***
Library SeleniumLibrary 15 2
Library Collections
*** Variables ***
${BROWSER} Chrome
#{elements} xpath=//div[#id="error"] id=searchInput xpath=//button[#type="submit"]
*** Test Cases ***
Simple Test
Open Browser http://www.wikipedia.org ${BROWSER}
Comment Wait Until Element Is Visible id=searchInput # search input field
${result}= Page Should Contain Elements ${elements}
IF not ${result}
Fail Page did not contained expected elements
END
Input Text ${elements[0]} Robot Framework
Wait Until Element Is Visible ${elements[1]}
Click Button ${elements[1]}
Wait Until Page Contains Robot Framework
Capture Page Screenshot
[Teardown] Close All Browsers
*** Keywords ***
Page Should Contain Elements
[Arguments] ${elements}
#{results}= Create List
FOR ${element} IN #{elements}
${result}= Run Keyword And Ignore Error Page Should Contain Element ${element}
IF "${result[0]}" == "FAIL"
Append To List ${results} ${element}
END
END
${ret}= Evaluate len(${results}) == 0
Return From Keyword If ${ret} True
FOR ${element} IN #{results}
Log Fail: Page should have contained element ${element} but it did not WARN
END
Return From Keyword False

Related

How do I iterate through verifying different texts on a page in Robot Framework?

I've got a functioning Robot Framework test that checks for different texts on a page. It's pretty basic. Scans the page for a specific string, then logs a PASS/FAIL if the string is found. Here's my code.
Test Keyword
${p1}= Run Keyword And Return Status Page Should Contain Element xpath=//*[contains(text(), "A")]
Run Keyword If ${p1} Log To Console "(A) Present" ELSE Log To Console "(A) Not Present"
${p2}= Run Keyword And Return Status Page Should Contain Element xpath=//*[contains(text(), "B")]
Run Keyword If ${p2} Log To Console "(B) Present" ELSE Log To Console "(B) Not Present"
${p3}= Run Keyword And Return Status Page Should Contain Element xpath=//*[contains(text(), "C")]
Run Keyword If ${p3} Log To Console "(C) Present" ELSE Log To Console "(C) Not Present"
This runs perfectly fine, but I'm having trouble making this into a list. Or maybe an array? I'm not sure.
Do I make the xpaths variables inside of the list? Would I make the Run Keyword If statements their own keyword and then just pass those? I'm not sure. Please let me know where I'm going wrong here. Thanks!
What you have in the sample is a repetition of the same code with only one variable - and that is always a good candidate for a loop. This way you'll just pass a different value, doing the same checks.
FOR ${a} IN A B C
${p}= Run Keyword And Return Status Page Should Contain Element xpath=//*[contains(text(), "${a}")]
IF ${p} Log To Console "(${a}) Present" ELSE Log To Console "(${a}) Not Present"
END
If the values you're checking are more (or dynamic) and it's not convenient to list them as the looped over, you could put them in a list, and iterate over it:
${values}= Create List A B C
FOR ${a} IN #{values}
# the same code follows here

How to catch white screen or black screen using robotframework or selenium

My project is to catch certain error message that can possibly show on website using robot framework and selenium. such as 400 - 500 error messages.
ex: 404. page not found
But we cannot catch empty page like white screen or black screen.
Is there a way for robot/selenium to tell if the page is just all white or all black ? whith no text visible or any element visible.
Thanks
Just get the source and check any unique text or tag and use it in the place of "application-footer". In my case i used application-footer which is available in my source. So Use this below code to find blank page, this is the workaround I used
${Source}= Get Source
Log ${Source}
${Blank_status} = Run Keyword And Return Status Should Contain ${Source} application-footer
Run Keyword If "${Blank_status}" == "False" FAIL Displayed Empty Page
If you want to Handle Failure in your own way, then instead of FAIL keyword write your own keyword to Handle and call the keyword
Run Keyword If "${Blank_status}" == "False" Error_Handle
*** Keywords ***
Error_Handle
# write your handling code

How to check the links are broken for a particular xpath using robot framework

I have xxx url, where
its has A element=== Links, B element===Links,
I want to validate Links are broken under A element,
I need to do under Robot framework please help
*** Settings ***
Library Collections
Library RequestsLibrary
Library SeleniumLibrary
Library String
Test Setup Open Browser ${BASE_URL}
Test Teardown Close Browser
*** Variables ***
${BASE_URL} https://www.crediwatch.com
*** Test Cases ***
Broken links test
${element_list}= get webelements xpath=//a[#href]
${href_list}= Evaluate [item.get_attribute('href') for item in $element_list]
Log ${href_list}
Create Session testing ${BASE_URL}
FOR ${element_href} IN #{href_list}
${uri}= Remove String ${element_href} ${BASE_URL}
${contains_base_url}= Evaluate "${BASE_URL}" in "${element_href}"
${response}= Run Keyword If ${contains_base_url} Get Request testing ${uri}
Run Keyword If ${contains_base_url} Should Be Equal As Strings ${response.status_code} 200
END

How to resolve "Using 'Get Element Attribute' without explicit attribute is deprecated" in robot framework using selenium library

This works and returns exactly what I want:
*** Variables ***
${HOME_LOGO}= css=#header > nav > div.header-gutter > div > img
*** Keywords ***
Home logo is visible
Element Should Be Visible ${HOME_LOGO}
${logo_src}= Get Element Attribute ${HOME_LOGO}#src
log ${logo_src}
However, I am getting a WARN when I run it: Using 'Get Element Attribute' without explicit attribute is deprecated
I have tried several approaches, but have not been able to resolve the warn message AND get the information I want into ${logo_src}. I am looking for the img src.
What is the best way to either handle the warn or get the img src from the element in the xpath?
Thank you - I'm very new to robot framework and selenium but not new to test automation. Also new to stackoverflow.
They have changed how this keyword works from when it was originally designed. Instead of appending the attribute to the locator, provide it as an argument:
${logo_src}= Get Element Attribute ${HOME_LOGO} src
(note: there needs to be two or more spaces before src)

Robot Framework: How to make Wait until keyword from selenium library return true or false

I would like to Run a keyword if an element is present on page.
I try using Selenium library's Wait Until Page Contains Element keyword, but it will always return "None", whether or not the element is present. I tried setting custom error, but that won't work either:
${condition} = Wait Until Page Contains Element ${locator} timeout=5 error=false
Run Keyword if '${condition}'=='false' click element ${some_refreshButton_locator}
Keyword click element ${locator} will run only when I make condition '${condition}'=='None'
Am I doing something wrong and how can I make a Selenium Library Wait until... keyword return true or false.
Thanks!
Wait Until Page Contains Element does not return anything but will raise error if element is not found. One workaround is to wrap Run Keyword And Ignore Error around it.
*** Settings ***
Library Selenium2Library
*** Test Cases ***
Test wait
Open Browser http://www.google.com/ gc
${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Stackoverflow timeout=2 error=false
Run Keyword if '${condition}'=='false' Log clicking
Close All Browsers
"Run Keyword And Return Status" can also be used to get True/False status as below:
${condition} = Run keyword And Return Status Wait Until Page Contains Element ${locator} timeout=5 error=false