Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

robotframework - I want to Automatized entry on HTML page

I have two problems, I want to automatized a login and password entry on an HTML page I wrote this code on RFW :

*** Variable ***
${USER}
${PSW}

*** Test Case ***
ENTER_ID    ${USER}    ${PSW}
Input Text       //input[@name="j_name"]    ${USER}
Input Text       //input[@name="j_password"]      ${PSW}


*** Keywords ***
ENTER_ID
[arguments]    ${myUSer}    ${myPSW}

${myUSER}=    Get Value From User    Please enter Name
${myPSW}=    Get Value From User    Please enter password

But on the HTML page, nothing is wrote by Robot. Log :

INFO : Typing text '' into text field '//input[@name="j_name"]'.
INFO : Typing text '' into text field '//input[@name="j_password"]'.

And also the log send me that

INFO : ${myUSER} = Paul
INFO : ${myPSW} = MyPassword

Variables used are myUSer and myPSW, it should be USER and PSW and I dont understand why

Thanks for helping me :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This:

*** Variable ***
${USER}
${PSW}

means that two variables are creates, variable with name USER and variable with name PSW. But they are empty.

This:

*** Test Case ***
ENTER_ID    ${USER}    ${PSW}
Input Text       //input[@name="j_name"]    ${USER}
Input Text       //input[@name="j_password"]      ${PSW}

seems like incorrectly formatted test case. But even if you gave it a name and formatted it properly, you still aren't assigning to those variables USER and PSW by the time Input Text keywords are executed. That's because you're using them as arguments and in keyword:

ENTER_ID
    [arguments]    ${myUSer}    ${myPSW}
    ${myUSER}=    Get Value From User    Please enter Name
    ${myPSW}=    Get Value From User    Please enter password

there's no assignment into these two variables you're later using with Input Text keywords.


A working code would be:

*** Variables ***
${USER}
${PWD}

*** Test Cases ***
Input User And Pwd
    Ask For Credentials
    Input Text       //input[@name="j_name"]    ${USER}
    Input Text       //input[@name="j_password"]      ${PWD}

*** Keywords ***
Ask For Credentials
    ${user}=    Get Value From User    Please enter Name
    ${pwd}=    Get Value From User    Please enter password
    Set Suite Variable    ${USER}    ${user}
    Set Suite Variable    ${PWD}    ${pwd}

Set Suite Variable or Set Global Variable is necessary here, because simple Set Variable will create only local variables, but you want to assign to USER and PWD in a keyword but use them in a test case elsewhere.

Personally, I'd rather go for an option with a return value, that seems a bit more secure than working with suite or global variables:

*** Test Cases ***
Input User And Pwd
    ${credentials}=    Ask For Credentials
    Input Text       //input[@name="j_name"]    ${credentials.user}
    Input Text       //input[@name="j_password"]      ${credentials.pwd}

*** Keywords ***
Ask For Credentials
    ${user}=    Get Value From User    Please enter Name
    ${pwd}=    Get Value From User    Please enter password
    ${credentials} =    Create Dictionary    user=${user}    pwd=${pwd}
    [Return]    ${credentials}

By this time, you realise, you don't really need those USER and PWD variables at all (unless you wan't to use them for something more like setting default values).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...