关于java调用autoit脚本的问题

由于selenium本身的局限性,我们在做web测试的时候,不得不使用autoit作为辅助工具来处理一些windows窗口的操作。

先来段autoit代码:

Sleep($CmdLine[1])

编译成exe文件,文件名为sleep.exe,放在c盘根目录下,代码的功能很简单,就是根据传入的时间休眠。cmd下执行:c:\sleep.exe 2000,表示休眠2秒。

我们在Java中调用这个程序:

   long time1 = System.currentTimeMillis();
try {
Runtime.getRuntime().exec("c://sleep.exe 10000");
long time2 = System.currentTimeMillis();
System.out.println((time2 - time1) + "毫秒。");
} catch (Exception e) {
e.printStackTrace();
}

用上面这段代码去调用的话,java不会等待autoit的执行,sleep启动后,继续往下执行,这样几乎没有实际的应用价值。我们通常都是要等autoit去把该做的事情都做完再做后面的事情。

long time1 = System.currentTimeMillis();
try {
Runtime.getRuntime().exec("c://sleep.exe 10000").waitFor();
long time2 = System.currentTimeMillis();
System.out.println((time2 - time1) + "毫秒。");
} catch (Exception e) {
e.printStackTrace();
}

我们在代码中加入waitFor(),可以达到上面的效果,但是又带来了另外一个问题,玩意autoit执行出了问题,一直不退出怎么办?程序就hang住了。

向高人讨教了一番,知道了如何控制autoit的超时问题,我们可以利用检查内存中是否存在au3进程的方式来做判断程序是否执行完毕,同时加一个超时控制,超时则继续往后执行,下面是demo 代码:

Java代码:

    
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

public class Runau3 {
public static void main(String[] args) {
// 执行au3脚本
// 休眠的那个脚本,脚本所在的目录(秒),等待时间,脚本的休眠时间(毫秒)
runAu3("sleep.exe", "c://", 5, "10000");
}

/**
*
* @param filename
* au3的文件名,必须编译成exe
* @param filepath
* au3的文件的路径
* @param timeout
* 超时时间(秒)
* @param args
* au3的参数
*/
public static void runAu3(String filename, String filepath, int timeout,
String... args) {
try {
// 如果发现进程,先杀了
if (findProcess(filename)) {
killProcess(filename);
}
// 拼装命令
String cmd = filename;
for (String arg : args) {
cmd += " " + arg;
}

// 执行au3脚本
Runtime.getRuntime()
.exec("cmd /C " + cmd, null, new File(filepath));
int count = 0;
while (findProcess(filename) && count < timeout) {
System.out.println(count);
Thread.sleep(1000);
count++;
}
// 如果进程未结束,杀了
if (findProcess(filename)) {
killProcess(filename);
}
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* 查找进程
* @param processname
* @return
*/
public static boolean findProcess(String processname) {
BufferedReader bufferedreader = null;
try {
Process proc = Runtime.getRuntime().exec(
"tasklist /fi \" imagename eq " + processname + " \" ");
bufferedreader = new BufferedReader(new InputStreamReader(proc
.getInputStream()));
String line = null;
while ((line = bufferedreader.readLine()) != null) {
if (line.contains(processname)) {
return true;
}
}
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
} finally {
if (bufferedreader != null) {
try {
bufferedreader.close();
} catch (Exception ex) {
}
}
}
}

/**
* 杀进程
* @param processname
*/
public static void killProcess(String processname) {
BufferedReader bufferedreader = null;
try {
Process proc = Runtime.getRuntime().exec(
"taskkill /F /IM " + processname);
bufferedreader = new BufferedReader(new InputStreamReader(proc
.getInputStream()));
String line = null;
while ((line = bufferedreader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (bufferedreader != null) {
try {
bufferedreader.close();
} catch (Exception ex) {
}
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤使用AutoIt脚本设置DNS: 1. 打开“网络和共享中心”(Network and Sharing Center)。 2. 点击“更改适配器设置”(Change adapter settings)。 3. 找到需要设置DNS的网络连接,右键点击并选择“属性”(Properties)。 4. 在属性窗口中选择“Internet协议版本4”(Internet Protocol Version 4),点击“属性”(Properties)。 5. 在弹出的窗口中选择“使用下面的DNS服务器地址”(Use the following DNS server addresses)并输入相应的DNS服务器地址。 6. 点击“确定”(OK)保存设置。 以下是设置DNS的AutoIt脚本示例: ``` ; 打开“网络和共享中心” Run("control.exe ncpa.cpl") ; 等待窗口打开 WinWaitActive("网络连接") ; 找到需要设置DNS的网络连接 ControlClick("网络连接", "", "[CLASS:SysListView32; INSTANCE:1]") ControlSend("网络连接", "", "[CLASS:SysListView32; INSTANCE:1]", "{DOWN 1}{SPACE}") ; 打开属性窗口 ControlClick("网络连接", "", "[CLASS:ToolbarWindow32; INSTANCE:1]", "left", 1) ; 等待属性窗口打开 WinWaitActive("WLAN 连接属性") ; 打开“Internet协议版本4”属性 ControlClick("WLAN 连接属性", "", "[CLASS:SysTabControl32; INSTANCE:1]", "left", 1) ControlClick("WLAN 连接属性", "", "[CLASS:Button; INSTANCE:1]", "left", 1) ; 等待“Internet协议版本4属性”窗口打开 WinWaitActive("Internet 协议版本 4 属性") ; 输入DNS服务器地址 ControlSetText("Internet 协议版本 4 属性", "", "[CLASS:Edit; INSTANCE:1]", "8.8.8.8") ; 这里输入需要设置的DNS服务器地址 ; 点击“确定”保存设置 ControlClick("Internet 协议版本 4 属性", "", "[CLASS:Button; INSTANCE:1]", "left", 1) ; 关闭所有窗口 WinClose("Internet 协议版本 4 属性") WinClose("WLAN 连接属性") WinClose("网络连接") ``` 该脚本会打开“网络和共享中心”窗口,找到需要设置DNS的网络连接,打开属性窗口,进入“Internet协议版本4”属性,输入DNS服务器地址并保存设置。请根据实际情况调整脚本中的窗口和控件名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值