Reputation: 163
I'm facing an issue with finding the broken links on the webpage. I'm using RequestsLibrary for the same. Right now I'm able to find all the links but now able to verify them against the response code.
Get All Link Tests
[Tags] Regression
[Setup] Log To Console Test for total links present on landing page started...
${base_url}= Get Base Url
open browser about:blank ${browser}
go to ${base_url}
${ALL_LINKS_COUNT} get element count xpath://a
log to console ${ALL_LINKS_COUNT}
@{LINK_ITEMS} create list
: FOR ${index} IN RANGE 1 ${ALL_LINKS_COUNT}+1
\ log ${index}
\ ${link_text} get text xpath:(//a)[${index}]
\ ${href} Get Element Attribute xpath:(//a)[${index}] href
\ log ${link_text}
\ log to console ("The link text is "${link_text}" & href is "${href}" ${INDEX})
\ ${link_length} Get Length ${link_text}
\ Run Keyword If ${linklength}>1 Append To List ${LinkItems} ${href}
Log Many ${LINK_ITEMS}
Remove Values From List ${href} javascript:void(0) \#
${link_items_length} Get Length ${LINK_ITEMS}
@{errors_msg} Create List
: FOR ${index} IN RANGE ${link_items_length}
\ ${resp} Get Request ${LINK_ITEMS[${index}]}
\ ${code} Run Keyword And Return Status Should Be Equal As Strings ${resp.status_code} 200
\ Run Keyword Unless ${code} Append To List ${errors_msg} error :${LINK_ITEMS[${index}]} | ${resp.status_code}
${check} Run Keyword And Return Status Lists Should Be Equal ${errors_msg} ${EMPTY}
Run Keyword Unless ${check} Fail Link \ assertion Failed with msg:\n@{errors_msg}
The error that I'm getting right now is "RequestsLibrary.Get Request expected 2 to 7 arguments, got 1." I've tried doing it with create session also, but then it fails with a message "**Link assertion Failed with msg: []"**
Upvotes: 0
Views: 2005
Reputation: 1062
Tried this and it works. However, now it works only for links starting with https://www.crediwatch.com. Also, the href list contains some mailto
links which should be filtered out.
*** 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
Upvotes: 0
Reputation: 1062
You need to do that Create Session
first and then pass the alias
as the first parameter for Get Request
. RequestsLibrary documentation says it quite clear.
The other error message shows that you have an empty list in @{errors_msg}
. I guess you need to pass the index number there.
Upvotes: 1