简体   繁体   English

使用Robot Framework Test在浏览器中上传文件

[英]Upload a file in browser using Robot Framework Test

I'm writing Test Cases using Robot Framework and Selenium for my Web application. 我正在为我的Web应用程序使用Robot FrameworkSelenium编写测试用例。 I tried to Upload a file, but I can't its failing. 我尝试上传文件,但是我无法失败。

My Code is 我的代码是

*** Variables ***
${TVAURL}     http://localhost:1500/
${Browser}    Firefox

TC_01: Enter into the application
    [Documentation]   Enter into the application to upload a file
    Open Browser   ${TVAURL}     ${Browser}
    maximize browser window
    Choose File ........

HTML File: HTML档案:

<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
</head>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

I need to know how to use this Choose File for this file upload test. 我需要知道如何使用此Choose File进行此文件上传测试。 I don't know how to do this. 我不知道该怎么做。 I need to upload a file automatically without any third party tools like Autoit, etc., 我需要没有任何第三方工具(例如Autoit等)自动上传文件,

I referred the following http://robotframework.org/Selenium2Library/Selenium2Library.html#Choose%20File 我引用了以下http://robotframework.org/Selenium2Library/Selenium2Library.html#Choose%20File

Moreover I referred the following questions too 而且我也提到了以下问题

But I can't get any solutions. 但是我找不到任何解决方案。 Kindly assist me how to do this using Robot Framework, Selenium in Pycharm Studio. 请帮助我如何使用Pycharm Studio中的Robot Framework,Selenium来执行此操作。

Reply for Answer #1: @demouser123 回答#1:@ demouser123

Still I'm having issue, here with I have attached the Screen shot 仍然有问题,在这里附上了屏幕截图

在此处输入图片说明

It always opens the Desktop, and fails. 它总是打开桌面,然后失败。 Kindly assist me. 请帮助我。

I trided the following code 我整理了以下代码

*** Variables ***
${PO_AddShell}     //*[@id="fileToUpload"]

click button  ${PO_AddShell}
Choose File   ${PO_AddShell}    E://Project/Publish/SampleTest.1500/rose.jpg

As per the documentation given in the Selenium2library, the syntax to use use the Choose File keyword is 根据Selenium2library中提供的文档,使用“ Choose File关键字的语法为

Choose File  Locator  File_name

For a dialog or input that WebDriver can interact with the example would do something like this 对于WebDriver可以与示例交互的对话框或输入,将执行以下操作

Choose File id=fileToUpload  C://Downloads/Demo/Abc.txt

Here C://Downloads/Demo/Abc.txt is the location on the system where the file is kept. 这里C://Downloads/Demo/Abc.txt是系统上保存文件的位置。 Change this to your own file location. 将此更改为您自己的文件位置。

Also, I remember this solution also, which ultimately worked for me too - SO Post about uploading from a Windows directory . 另外,我还记得该解决方案,该解决方案最终也对我也有用 - 有关从Windows目录上载的帖子 You can also try this one. 您也可以尝试这个。

I am providing alternate solution for the question. 我正在为这个问题提供替代解决方案。 Please try this if you didnt get. 如果您没有得到,请尝试此。

Prerequisite for running below script : 运行以下脚本的前提条件:

Install Sikulix on your machine from this SikuliX ,this is easy to install and install robotframework-SikuliLibrary . 你的机器从本上安装Sikulix SikuliX ,这是易于安装和安装robotframework-SikuliLibrary。 You can explore the documentation if you want some more options SikuliX documentation . 如果需要更多选项SikuliX文档,可以浏览文档 Capture the images like download path/filename using some snipping tool and update the name in the following script. 使用某些截图工具捕获诸如下载路径/文件名之类的图像,并在以下脚本中更新名称。

*** Settings ***
Library          Selenium2Library
Library           SikuliLibrary

*** Test cases  ***
Login to Browser with download preferences
 [Documentation]   This one is without specifying download location
    Open Browser  https://www.docdroid.net/   Chrome
    Click Element   id=selectFiles
    Sleep   5
    SikuliLibrary.Click      /images/download.PNG
    SikuliLibrary.Click      /images/file.PNG
    SikuliLibrary.Click      /images/open.PNG

If you want to specify download location , go with below script 如果要指定下载位置,请使用以下脚本

*** Settings ***

    Library          Selenium2Library
    Library           SikuliLibrary

    *** Test cases  ***
    Login to Browser with download preferences
     [Documentation]   You can specify your download location
        Open Browser  https://www.docdroid.net/   Chrome
        Click Element   id=selectFiles
        Sleep   5
        SikuliLibrary.Input Text           /images/file_path.PNG     C:\\Users\\Madhu\\Downloads
        Press Special Key        ENTER
        #SikuliLibrary.Click      /images/download.PNG
        SikuliLibrary.Click      /images/file.PNG
        SikuliLibrary.Click      /images/open.PNG

using autoit library in robotframework, you can upload a file into application. 在robotframework中使用自动库,您可以将文件上传到应用程序中。 This solution works even the system is locked. 即使系统处于锁定状态,此解决方案也可以使用。

Input Filename and click send in Window dialog 输入文件名,然后在“窗口”对话框中单击“发送”

run keyword if  '${Browser}' == 'Chrome'  File upload in Chrome browser
...   ELSE IF  '${Browser}' == 'Firefox'  File upload in Firefox browser

File upload in Chrome browser 在Chrome浏览器中上传文件

control focus  [CLASS:#32770; TITLE:Open]  ${EMPTY}  [CLASSNN:Edit1]
control send  [CLASS:#32770; TITLE:Open]  ${EMPTY}  [CLASSNN:Edit1]  C:\\File_Upload\\Test_Upload
control click  [CLASS:#32770; TITLE:Open]  ${EMPTY}  &Open

File upload in Firefox browser 在Firefox浏览器中上传文件

control focus  File Upload  ${EMPTY}  [CLASSNN:Edit1]
control send  File Upload  ${EMPTY}  [CLASSNN:Edit1]  C:\\File_Upload\\Test_Upload
control click  File Upload  ${EMPTY}  &Open

You should not use 你不应该使用

click button  ${PO_AddShell}

Just use choose file without clicking 只需使用选择文件,无需点击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM