Selenium + java - With autolt complete the upload file operations

EDITORIAL:

Upload files that each automated test students will encounter, but also can be said that the issue of a compulsory interview, we generally use the standard controls sendkeys () will be able to complete the upload, but our test site upload control typically own package, with traditional upload has been well spent, that is to say with selenium of APi has been unable to complete the upload operation, and then we will borrow Autolt third-party tools to complete the operation to upload files.

Ready to work

1, download autolt

Official website: https://www.autoitscript.com/site/autoit/downloads/ , download your own

Baidu may also download the green version, free installation, the author is the green version, the following cases have to explain green version

Baidu network disk attached: link: https://pan.baidu.com/s/1szmGK7wudsXKkH5xkEOnOQ extraction code: dysb 

2, after downloading unzip to the specified directory

3, the following test page HTML code

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>上传文件演示案例</title>
</head>
<body>
  <div class="row-fluid">
    <div class="span6 well">
    <h3>upload File</h3>
      <input id="upload" type="file" name="file" />
    </div>
  </div>
</body>
</html>

Upload script writing

  • Find unzip directory, double-click AU3TOOL.exe, open interface is used to write the script

  • Double-click Au3Info.exe, open the positioning tool interface

  • Enter the following code in the file: Note The parameters in parentheses, the next step will be about how to obtain parameters

ControlFocus("title1","","Edit1"); 
WinWait("[CLASS:#32770]","",10);
ControlSetText("title1","","Edit1","文件地址");
Sleep(2000);
ControlClick("title2","","Button1");

Parameter obtaining step (first three lines) of

  • Next, the last line of code in the title and button1

To generate an executable program

 Select Tools -> Script Compiler

An executable file as follows:

Automated test script calls upload.exe complete the upload

Specific code as follows:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

/**
 * @author rongrong
 * 上传文件演示案例
 */
public class TestUpload {
    WebDriver driver;

    @BeforeClass
    public void beforeClass() {
        System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Test
    public void testUpload() {
        driver.get("file:///C:/Users/Administrator/Desktop/index.html");
        driver.manage().window().maximize();
        //选择文件
        driver.findElement(By.id("upload")).click();
        try {
            Runtime.getRuntime().exec("upload.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @AfterClass
    public void afterClass() {
        driver.quit();
    }

}

效果如下:

 

到此使用自动化调用autolt上传文件的案例演示结束,可能很多同学会纠结autolt语法不会写啥的,大可不必纠结,基本写完是一劳永逸的,不会在维护了,更多autolt的用法,有兴趣的同学可以自行去官网查看了解。

 

Guess you like

Origin www.cnblogs.com/longronglang/p/11312140.html