UI Automation how to handle the file upload events

In UI Automation, we often encounter the upload file operations. Upload event processing is a relatively cumbersome operation, click the upload control because Windows will pop up a window for the user to select a file, but Windows window is a component other than the browser, so selenium alone can not deal with this windows window. We are here to deal with a few ideas, we look at the following HTML.

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>test</title>

<script language="javaScript">

function toAlert()

{

alert("hello continue...");

}

 

</script>

</head>

<body>

<form>

<table >

 

<tr>

 

<td>

<input name="file" type="file"/>

</td>

 

</tr>

 

</table>

</form>

</body>

</html>

 

Open with notepad ++, it will be saved as a file autotest.html, after opening, only an upload button, we look at how to handle upload events.

 

 

 

1

 

Direct call comes sendkeys selenium operate you will need to upload a file path passed directly into the upload control.

 

Org.openqa.selenium.By 2.import;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;
/ **
* the Description:
* the Author: Chrisma
* a Date: 2019-05-15
* /
public class testupload2 {
public static void main (String AGS []) throws InterruptedException {
// initialize the webdriver
the WebDriver new new ChromeDriver Driver = ();
// open the local HTML
driver.get ( "File: /// D: / the UI / autotest1.html ");
// the path to the file is passed to the file upload control
driver.findElement (By.name (" file ") D) sendKeys (.": \ named text.txt in the ");
// wait to see the results of
Thread .sleep (10000);
// Close the webdriver
driver.quit ();
}
}

 

 

This approach can solve most of the upload operation, but for some upload box prohibited input can not operate, and this time we will consider other options.

 

 

 

2

 

For you can not enter, we can consider AutoIT to upload.

AutoIt v3 is the latest version, which is a free software BASIC-like scripting language, which is designed for automated operation (graphical user interface) in Windows GUI. It is implemented using a combination of automated tasks by simulating keystrokes, mouse movements and window / control.

Official Website: https://www.autoitscript.com/site/

AutoIt downloaded from the website and install, the installation is complete directories in the menu will see the following figure:

 

 

AutoIt Windows Info Windows to help us identify the control information.

Compile Script to.exe for AutoIt generate exe executable file.

Run Script for executing AutoIt script.

AutoIt SciTE Script Editor for writing scripts.

We open the html page and click the upload button:

 

 

Here we look at how to deal with autoIT this upload.

• When you open AutoIT Window Info, and then click on the Finder Tool, do not release the left button, such as the mouse into sight type, move the mouse to the file upload control box needs to identify the release button.

 

After identifying the target all the information is displayed in AutoIT windows info

After identifying the window title is "Open", the title of the Class as "# 32770."

class file name input box is "Edit", Instance "1", so ClassnameNN as "Edit1".

Open button class as "Button", Instance "1", so ClassnameNN as "Button1".

We open SciTE Script Editor, and then fill in the following code:

 

;ControlFocus("title","text",controlID) Edit1=Edit instance 1

ControlFocus("Open", "","Edit1")

 

; Wait 10 seconds for the Upload window to appear

WinWait("[CLASS:#32770]","",10)

 

; Set the File name textmargin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; color: rgb(51, 51, 51); font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: left; text-indent: 39pt;">

ControlSetText("Open", "", "Edit1", "D: est.txt")

 

Sleep(2000)

 

; Clickmargin: 0px; padding: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; clear: both; min-height: 1em; color: rgb(51, 51, 51); font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif; font-size: 17px; letter-spacing: 0.544px; text-align: left; text-indent: 39pt;">

ControlClick("Open", "","Button1");

 

 

The script after SciTE Script Editor to save, open upload window, select Tools-> go in SciTE Script Editor in, to check whether files can be uploaded.

Confirm that the script runs correctly, we save this script into Script.au3, and then open the Compile Script to.exe, convert files to Script.au3 Script.exe:

 

This time, we open the file upload controls, double-click Script.exe file, you can see the file upload event has been processed successfully.

Next, is the use of java to call the EXE file:

org.openqa.selenium.By Import;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;
Import java.io.IOException;
/ **
* the Description:
* the Author: Chrisma
* a Date: 2019 -05-15
* /
public class testupload2 {
public static void main (String AGS []) throws InterruptedException {
// initialize the webdriver
the WebDriver new new ChromeDriver Driver = ();
// open the local HTML
driver.get ( "File: /// D: / the UI / autotest.html ");
// click the choose file button
driver.findElement (By.name (" file ")) the click ();.
// set wait 3 seconds
Thread.sleep (3000);
// the Java Runtime module getruntime.exec () method can be called exe program and executed.
EXE = Runtime.getRuntime the Runtime ();
the try {
STR = String "D: //Script.exe";
.exe file // Runs the specified position
exe.exec (STR);
} the catch (IOException E) {
System.out.println ( "Error The RUN to EXE") ;
e.printStackTrace ();
}
// wait to see the results of
the Thread.sleep (10000);
// Close the webdriver
driver.quit ();

}
}

 

With AutoIT also has its own limitations, such as only in the Windows system, if you want to migrate to other systems, you have to refer to other ways.

 

3

 

If you want to deal with non-Windows systems to upload, we can deal with in the form of pure Java, this time, we have to use this class Robot in the process flow as follows: Open the Control file upload -> file on disk path through robot copy pasty into (required file input box default cursor focusing) -> press enter, OK button to pop the trigger, the file upload process is completed

 

 

 

4

 

code show as below:

org.openqa.selenium.By Import;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.chrome.ChromeDriver;
Import in java.awt *;.
Import with java.awt.datatransfer.StringSelection;
Import in java.awt. event.KeyEvent;
/ **
* the Description:
* the Author: Chrisma
* a Date: 2019-05-15
* /
public class testUpload {
public static void main (String AGS []) throws InterruptedException, of AWTException {
// initialize the webdriver
the WebDriver Driver = ChromeDriver new new ();
// open the local HTML
driver.get ( "file: /// D: / the UI / autotest.html");
// specify the path to the file upload
StringSelection sel = new StringSelection ( "D : \ test \ test1.txt ");
// copy the image file path to the clipboard
Toolkit.getDefaultToolkit () getSystemClipboard () setContents (SEL, null);..
System.out.println ( "Selection" + SEL);
// click the Upload button
driver.findElement (By.name ( "file") ) click. ();
// Create a new object class Robot
Robot Robot Robot new new = ();
the Thread.sleep (1000);
// press Enter
robot.keyPress (KeyEvent.VK_ENTER);
// carriage release
robot.keyRelease ( KeyEvent.VK_ENTER);
// + V pressing the CTRL
robot.keyPress (KeyEvent.VK_CONTROL);
robot.keyPress (KeyEvent.VK_V);
// + V release the CTRL
robot.keyRelease (KeyEvent.VK_CONTROL);
robot.keyRelease (KeyEvent .VK_V);
Thread.sleep (1000);
// press the Enter the Enter
robot.keyPress (KeyEvent.VK_ENTER);
robot.keyRelease (KeyEvent.VK_ENTER);
// wait to see the results of
the Thread.sleep (10000);
// Close the webdriver
driver.quit ();
}
}

 

We will first deal with file upload here, I hope you can give us broaden their thinking, we see you next time.

Author: Testfan Chris

Source: micro-channel public number: automated software testing platform

Copyright: welcome to reprint, but must indicate the source and gives links to articles in the apparent position of the article page

 

Guess you like

Origin www.cnblogs.com/testfan2019/p/12531710.html