2014年10月14日 星期二

robotframework demo 1 (selenium2library)

啟動 robotframework IDE


$ ride.py

1. Create a Google_Demo project
File -> New Project


Enter  ->  Google_Demo 
Type -> Directory
Format  ->  TXT

左邊視窗將會多一個 google demo 資料夾

2.  Create a New Suite (resource folder)

On Google_Demo, select this suite, right click on it and then click on New Suite

Enter  ->  resource
Type -> Directory
Format  ->  TXT

3. Create two Resource file  (parameter.txt and keyword.txt) 

On resource, select this suite, right click on it and then click on New Resource

Enter  ->  parameter
Format  ->  TXT

select resource again


Enter  ->  keyword
Format  ->  TXT

The Folder structure will like this picture



4. Edit parameter.txt ->  this file put parameter of your define
Content will like this


*** Settings ***
Documentation     A resource file with reusable keywords and variables.
...
...               The system specific keywords created here form our own
...               domain specific language. They utilize keywords provided
...               by the imported Selenium2Library.

*** Variables ***
${SERVER}         http://www.google.com.tw
#${BROWSER}       Firefox
${BROWSER}        Chrome
${DELAY}          0.5
${WIDTH}          1024
${HIGHT}          768
${KEYWORD}        robotframework

*** Keywords ***


To Explain :

${SERVER}     =>  you defind web
${BROWSER} =>  you open browser
${DELAY}       =>  For run speed seconds
${WIDTH}       =>  Defind you browser window size
${HIGHT}        =>  Defind you browser window size
${KEYWORD}  =>  Search Key word

5. Edit keyword.txt ->  this file put you define keywords


*** Settings ***
Library           Selenium2Library
Resource          parameter.txt

*** Keywords ***
Open Google web
    Open Browser    ${SERVER}    ${BROWSER}
    Set Window Size    ${WIDTH}    ${HIGHT}
    Set Selenium Speed    ${DELAY}


To Explain :

Open Google web  => you define key word

Open Browser 、 Set Window Size 、 Set Selenium Speed =>  selenium2library key word

PS: if you don't know selenium2library key words、 you cau press F5, will pop a window, you can search key word and how to use the key word.


6. Create search_demo1 file

On Google_Demo, select this suite, right click on it and then click on New Suite

Enter  ->  search_demo1
Type ->   File
Format  ->  TXT

7. Edit search_demo1


*** Settings ***
Resource          resource/keyword.txt

*** Test Cases ***
Search Test
    Open Google Web
    Click Element    xpath=//*[@id="lst-ib"]
    Input Text    lst-ib    ${KEYWORD}
    Press Key    xpath=//*[@id="lst-ib"]    \\27
    Click Button    xpath=//*[@id="tsf"]/div[2]/div[3]/center/input[1]
    Sleep    3
    [Teardown]    Close Browser


To Explain:

Open Google Web  =>  Define from keyword.txt


Clilck Element  =>  define from library and xpath you can open chrome browse and use F12 function   to  search xpath and copy xpath。


Input Text  =>  define from library,  lst-ib is the element id,  ${KEYWORD} define from parameter.txt


Press Key => use key board function, xpath same as Click Element, \\27 is ASCII code, 27 is keybord esc.

Click Button => use mouse click the search button










8. Run Search_demo1
$ pybot Search_demo1.txt

沒有留言:

張貼留言