SuperPatrick教程之如何使用Java进行桌面自动化程序开发---晒酷学院

晒酷学院:https://shareku.ke.qq.com/
微信号:添加请注明晒酷学院            QQ群:979438600
微信号:添加请注明晒酷学院         QQ群:979438600

1.SuperPatrick API接口是通过SuperPatrickLibrary.dll提供, 创建Maven工程,在pom.xml文件中添加如下依赖:

<dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>jna</artifactId>
      <version>4.4.0-1</version>
      <scope>compile</scope>
</dependency>

2. 我们定义这样一个接口:

public interface SuperPatrickLibrary extends Library {
      void findElement(String pStrId, String pStrName, String pStrClassName, String controlType);
      void sendKeys(String pKeysString);
      void sendShortCutKeys(String pKeysString);
}

3.使用前,你需要设置字符集编码如果你需要支持中文的话,同时你需要指定SuperPatrickLibrary.dll所在的路径:

System.setProperty("jna.encoding","GBK"); 
String dllPath = "C:/1/SuperPatrickLibrary.dll";
SuperPatrickLibrary superpatrick = (SuperPatrickLibrary) Native.loadLibrary(dllPath, SuperPatrickLibrary.class);

下面的例子SuperXi为你演示如何自动化的去操作记事本这个windows内置的应用程序:

import com.sun.jna.Library;
import com.sun.jna.Native;

public class SuperPatrickTest {
	public interface SuperPatrickLibrary extends Library {
		void findElement(String pStrId, String pStrName, String pStrClassName, String controlType);
		void sendKeys(String pKeysString);
		void sendShortCutKeys(String pKeysString);
	}
    

	public static void main(String[] args) throws InterruptedException {
	   System.setProperty("jna.encoding","GBK");   
	   String dllPath = "C:/1/SuperPatrickLibrary.dll";   
	   SuperPatrickLibrary superpatrick = (SuperPatrickLibrary) Native.loadLibrary(dllPath, SuperPatrickLibrary.class);
	   superpatrick.findElement("307","显示桌面","","Butto
	   n");
	   superpatrick.sendShortCutKeys("{WIN}r");
	   Thread.sleep(2000);
	   superpatrick.sendKeys("notepad");
	   superpatrick.sendShortCutKeys("{Return}");
	   Thread.sleep(500);
	   superpatrick.sendKeys("欢迎使用来到AutoTestOps测试开发专业网站!!!");
	   Thread.sleep(500);
	   superpatrick.findElement("","文件(F)","","MenuItem");
	   superpatrick.findElement("3","保存(S)    Ctrl+S","","MenuItem");
	   Thread.sleep(500);
	   superpatrick.sendKeys("test.txt");
	   superpatrick.findElement("1","","Button","Button");
	   superpatrick.findElement("CommandButton_6","是(Y)","CCPushButton","Button");
	   superpatrick.findElement("","关闭","","Button");
	}
}

在这里插入图片描述

注意: 用Sleep方法在自动化测试中不是一个好习惯,这里只是为了演示方便。SuperPatrick后续版本会逐步退出智能等待的API

findElement函数的参数由SuperPatrick定位器所提供,你也可以通过SuperPatrick定位器提供的右键复制findlement函数来自动生成findElement函数调用进一步减少工作量,sendShortCutKeys集成了Windows快捷键操作,sendKeys则可以输入各种字符包括中文等各种字符,并且接口名称和webdriver一致,希望大家亲自把这个例子运行以下即可看到效果,是不是相当简单?更多的技巧接口SuperXi会陆续发布文章详细告诉大家如何使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值