当前位置:网站首页>File upload and download of robot framework

File upload and download of robot framework

2022-04-23 16:36:00 Sink the wine cup and fleeting time

RobotFramework And File upload and download

In the course of the project , We often encounter file upload and download operations , Various ways can also be found online , This is about RobotFramework In the framework of UI automation File uploading and downloading in the process

Upload files

RobotFramework frame Selenium2Library There is one in the library choose file keyword , It can upload files

grammar :

choose file          Upload button element location           File path 

Here we still take the email upload file as an example . For operations on Web pages , Can pass selenium IDE( install ) Recording script , Save yourself time locating elements .
 Insert picture description here
Here is the button for uploading files , It's actually bound to input On the property
test.robot

*** Settings ***
Library           Selenium2Library

Suite Setup        Log in to Netease mailbox 
Suite Teardown     Close the browser 


*** Keywords ***
 Log in to Netease mailbox 
	open browser    https://mail.163.com/   chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    xpath=//iframe[contains(@id,'iframe')]
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    xpath=//*[@name="email"]      Your account number 
	sleep    2
	#  Enter... In the password input box   password 
	input text    xpath=//*[@name="password"]    Your password 
	sleep    2
	#  Click the login button 
	click element    xpath=//*[@id="dologin"]
	sleep    5

 Close the browser 
	close browser

*** Test Cases ***

 Test upload file 
    click element       xpath=//span[contains(.,' Write   Letter ')]
    sleep    5
    choose file         xpath=//input[@type='file']         F:/RFstudy/filepath/upload/1.jpg
    sleep    10

File upload takes time , So set the waiting time as long as possible , Avoid file upload failure due to network reasons

File download

It's easy to download files , There is usually a download button on the page , Just click the download button
Chrome The browser can download files without pop-up boxes , Just set it in the browser ( It seems that by default, if you control the browser to download files through the driver file, there will be no pop-up box ) The file will be downloaded to by default C:\Users\Admin\Downloads Under the table of contents
 Insert picture description here

test.robot

*** Settings ***
Library           Selenium2Library

Suite Setup        Log in to Netease mailbox 
Suite Teardown     Close the browser 


*** Keywords ***
 Log in to Netease mailbox 
	open browser    https://mail.163.com/   chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    xpath=//iframe[contains(@id,'iframe')]
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    xpath=//*[@name="email"]      Your account number 
	sleep    2
	#  Enter... In the password input box   password 
	input text    xpath=//*[@name="password"]    Your password 
	sleep    2
	#  Click the login button 
	click element    xpath=//*[@id="dologin"]
	sleep    5

 Close the browser 
	close browser

*** Test Cases ***

 Test Download File 
    click element      xpath=//span[contains(.,' closed   Letter ')]
    sleep    2
    click element      xpath=//div[4]/div[2]/div/div[2]/span
    sleep    2
    click element      xpath=//a[contains(text(),' See attachment ')]
    sleep    2
    click element      xpath=//td/div/img
    sleep    2
    click element      xpath=//a[contains(text(),' download ')]
    sleep    10

If you need to specify to download to the corresponding directory , You need to open the browser to access the website , Load the corresponding configuration
You can modify the login keyword here , Every time I log in , Can pass in a parameter ( File download directory ); It can also be done without transmission , A download directory is given by default , Do not download files , The login function can also continue to be used

test.robot

*** Keywords ***
 Log in to Netease mailbox and specify the file download directory 
	[Arguments]     ${
     File download directory }=${
     Default download directory }
	${
    options}    evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys,selenium.webdriver
    ${
    prefs}    create dictionary    download.default_directory    ${
     File download directory }     # Specify the directory to download the files 
    call method    ${
    options}    add_experimental_option    prefs    ${
    prefs}
    create webdriver    Chrome    chrome_options=${
    options}
    
	open browser    https://mail.163.com/   chrome
	sleep    5
	maximize browser window
	#  Switch to iframe
	select frame    xpath=//iframe[contains(@id,'iframe')]
	#  Enter... In the user name input box   user name 
	sleep    2
	input text    xpath=//*[@name="email"]      Your account number 
	sleep    2
	#  Enter... In the password input box   password 
	input text    xpath=//*[@name="password"]    Your password 
	sleep    2
	#  Click the login button 
	click element    xpath=//*[@id="dologin"]
	sleep    5

版权声明
本文为[Sink the wine cup and fleeting time]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231402128805.html

随机推荐