Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload files by Java selenium

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to upload files on Java selenium". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to upload files on Java selenium.

Download and install AutoIt

File upload is a tricky part of automation. Currently, selenium does not provide api for uploading, so you know to do it with the help of external forces, such as AutoIt and sikuli.

AutoIt, a free software that uses a scripting language similar to BASIC, is designed for Windows GUI (graphical user interface) automation, using a combination of simulated keyboard keys, mouse movement, and windows / controls to automate tasks

1. Open the official website of AutoIt to download

Both download methods are available. What I want to download here is zip, which is decompressed as shown in the following figure:

Click on the SciTe folder and we open the script editor.

Open the Baidu picture upload window, open the AutoIt Windows Info tool, move the mouse to Finder Tool, hold down the left mouse button and drag to the windows control you need to identify. Drag the target shape button on the element locator to the file upload pop-up window to capture some element information. Drag the Finder Tool icon on the tool (that is, the blue circle) on the tool to the control you want to identify, and the unique identification information for the control will be displayed on the left side of the tool (the part marked in the red box in the figure). From the results displayed, the Title= of this control is "open" and the Class is Edit,Instance=1. We use this information of the control to locate the control and write scripts.

Open the editor and call the function to write the script according to the information recognized by the control Finder Tool; enter the following script in the AutoIt script editor without the comments I wrote below.

We need to know the following information here:

1. The title of the operation page, which is used for the fixed operation page.

two。 For the information to be filled in, fill in the "path and file name of the uploaded file" in the input box (windows operation)

3. Click the "Open" button to upload the file.

According to the control information identified above, use the editor SciTE Script Editor to write the script according to the syntax of AutoIT.

There are several methods needed to implement file upload:

ControlFocus ("window title", "window text", control ID)-> sets the input focus to a control of the specified window (that is, the id of the control ID "File name" input box) WinWait ("window title" [, "window text" [, timeout]])-> pauses the execution of the script until the specified window exists (appears) ControlSetText ("window title") "window text", control ID, "new text")-> modify the text of the specified control (that is, the id of the control ID filename input box) Sleep (delay)-> pause the script for a specified period of time ControlClick (window title, window text, control ID [, button] [ Clicks]])-> sends a mouse click command to the specified control (that is, the id of the control ID "Open" button)

Where title is the Title field recognized by AutoIt Window Info, and controlID is the stitching of Class and Instance recognized by AutoIt Window Info. For example, the stitching result of the above image should be: Button1 (i.e. classnameNN)

ControlFocus (("title", "text", controllD) is used to identify the windows file upload window ControlFocus ("Open", "", ") enter the local path to upload the file ControlSetText (" Open ","," Edit1 "," C:\ Users\ DELL\ Desktop\ test\ upload\ Beijing Hongge. Jpeg ") Sleep (2000) Click the" Open "button ControlClick (" Open "," Button1 ") in the upload window.

Save the script file in ChromFileUpload.au3 format, and then click Tools menu, tools= > go in the AutoIt script Editor to perform script verification (provided that the windows window must be open). The verification is successful, as shown in the following figure:

In order for this script to be called by the java program, the exe file needs to be generated through the Compile Script to .exe (x64) tool (this is AutoIt installed through the .exe installation package).

Click the Tools menu in the AutoIt script editor and select compile, which will generate a .exe file in the same path (this is the AutoIt installed by the unzip package).

Prompt Conversion complete conversion completed: copy the ChromeFileUpload.exe to the project and use it later in the Selenium script.

Java code

/ / realize file upload. Get the Runtime object Runtime runtime = Runtime.getRuntime () through the static method of Runtime; / / call the exe method runtime.exec through the Runtime object ("C:\ Users\ DELL\ Desktop\ test\ upload\ ChromeFileUpload.exe"). At this point, I believe you have a better understanding of "how to upload files by Java selenium". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report