Problem in Get Request with Robot Framework

Hi All,

I am not sure what’s wrong in the below Get Request, when i run the script in Get Request link appended.

Issue : GET Request : url=http://127.0.0.1:5000//http://127.0.0.1:5000/index.html

Please see the below code and the Report screenshot.

I am stuck here! Really appreciate for the help.

*** Settings ***
Library Selenium2Library
Library Collections
Library RequestsLibrary

*** Variables ***

{url3} http://127.0.0.1:5000/ {BROWSER} chrome
*** Test Cases ***

BrokenLinksTest-ForPracticeSelenium-2ndPage
Open Browser {url3} {BROWSER}
Maximize Browser Window
VerifyAllLinksOn2ndPage
Close Browser

VerifyAllLinksOn2ndPage
Comment Count Number Of Links on the Page
{AllLinksCount}= get element count xpath://a Comment Log links count Log {AllLinksCount}
Comment Create a list to store link texts
@{LinkItems} Create List
Comment Loop through all links and store links value that has length more than 1 character
: FOR {INDEX} IN RANGE 1 {AllLinksCount}-1
\ Log ${INDEX}

\    ${link_text}=    Get Text    xpath=(//a)[${INDEX}]   #<-- for what ? -->
\    ${href}=    Get Element Attribute    xpath=(//a)[${INDEX}]     href

\    Log    ${link_text}
\    log to console  ("The link text is "${link_text}" & href is "${href}" ${INDEX})

\    ${linklength}    Get Length    ${link_text}  #<-- you are checking text not href ? -->
\    Run Keyword If    ${linklength}>1    Append To List    ${LinkItems}    ${href}
Log Many    ${LinkItems}
Remove Values From List  ${LinkItems}  javascript:void(0)     #<-- don't forget checking content on list -->
${linkitems_length}    Get Length    ${LinkItems}
Log Many    ${LinkItems}
@{errors_msg}    Create List
Create Session    secondpage   http://127.0.0.1:5000/
:FOR    ${INDEX}    IN RANGE   ${linkitems_length}
\    Log Many     ${LinkItems[${INDEX}]}
\    ${ret}    get request   secondpage     ${LinkItems[${INDEX}]}    \
\    log to console      ${ret}
\    log       ${ret}
\    ${code}    Run Keyword And Return Status    Should Be Equal As Strings    ${ret.status_code}    200
#\   log to console  "Gonna link"  ${LinkItems[${INDEX}]}

\ click link {LinkItems[{INDEX}]}

#\ Capture Page Screenshot
#\ Click Link link={LinkItems[{INDEX}]}
\ Run Keyword Unless {code} Append To List {errors_msg} error :{LinkItems[{INDEX}]} | {ret.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}
Sleep 1

Thanks,
Raj

Hi,

Hard to say, since I can only guess what is the value of LinkedItems[${INDEX}]. But I guess it contains https://127.0.0.1:5001/index.html? Then Get Request works exactly as expected. At that keyword you do not repeat the complete url that you had already provided at Get session. You provide the last part of the url that will be attached to the url from the session.

So I guess, when LinkItem[${INDEX}] contains the value index.html this should work.

Out of curiosity: you use a very old version of SeleniumLibrary. What version of RequestLibrary do you use? You might want to upgrade both.

Hi Jag,

With the modified following code, I am able to successfully verify the links.
For some reason, I am not able to get auto suggestions with the SeleniumLibrary, that’s why i am using Selenium2Library.

*** Settings ***
Library Collections
Library RequestsLibrary
Library Selenium2Library
Library String
Test Setup Open Browser {BASE_URL} Test Teardown Close Browser *** Variables *** {BASE_URL} http://127.0.0.1:5000/ #http://www.practiceselenium.com/

*** Test Cases ***
Broken links test
Comment Count Number Of Linkds on the Page
{AllLinksCount}= get element count xpath://a Comment Log links count Log {AllLinksCount}

${element_list}=      Get WebElements       xpath=//a
${href_list}=  Evaluate  [item.get_attribute('href') for item in $element_list]
Log    ${href_list}

 Remove Values From List  ${href_list}  javascript:void(0)     #<-- don't forget checking content on 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

Thanks,
Raj

When you create Requests session, you associate a baseurl into that session: “http://127.0.0.1:5000/” . Then, you are getting a link from your page, which looks like its a full url “http://127.0.0.1:5000/index.html”, these are then combined and result is what you get. Either you omit the session or split the url to base url and path from LinkItems… To split the url, you could use SeleniumTestability’s Spit Url To Host And Path keyword

PS. Dont use Selenium2Library. Its backwards compatibility layer. Latests release just SeleniumLibrary

2 Likes