Sikuli教程:如何将Sikuli与Selenium结合使用(示例)

Version 2.0.2以后,不再需要安装程序,只需下载IDE或API包并开始使用即可。

什么是Sikuli?

官网文档: https://sikulix-2014.readthedocs.io/en/latest/index.html
Sikuli是一个基于GUI的开源自动化工具。它用于与Web页面的元素交互并处理基于windows的弹出窗口(页面)。它使用“图像识别”技术与网页弹出窗口的元素进行交互。Sikuli将Web页面的所有元素视为图像,并根据其图像识别这些元素。当UI元素稳定且不经常变化时,Sikuli是首选。

Sikuli与Selenium Webdriver的集成

Sikuli可以使用Sikuli JAR文件与selenium webdriver集成。
以下序列是使用selenium webdriver配置Sikuli的步骤列表。

【另外一种方式Maven配置Jar包:】

Step 1) 从下面的网址下载Sikuli JAR包:(比如sikulixsetup-1.1.3.jar)
https://launchpad.net/sikuli/+download
cdm 进入到sikulixsetup所在的文件目录,执行Jar包:java -jar sikulixsetup-1.1.3.jar
执行Jar包之后要选择配置
生成Jar包的版本号
正在下载界面
选择Yes即可
最后的提示
安装完成之后的CMD窗口
新生成的jar包如下图所示:
新生成的jar包
Step 2) 然后在Eclipse中创建一个新的JAVA项目,并使用右键单击project -> Build Path -> Configure Build Path,将JAR文件添加到构建路径以及Selenium jar包。

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>

导入到Project中
一旦将JAR文件添加到项目构建路径中,就可以使用Sikuli提供的类。

提示:如果不按照上述的方式操作可能执行不成功,Sikuli一直报错** [error] RunTimeINIT: terminating: libs to export not found on above classpath: /sikulixlibs/windows/libs64 **

Sikuli的类Screen

Screen类是Sikuli提供的所有方法的基类。Screen类包含用于屏幕元素上所有常用操作的预定义方法,如单击、双击、向文本框提供输入、悬停等。下面是Screen类提供的常用方法列表

MethodDescriptionSyntax(语法)
Click 单击此方法用于使用图像名称作为参数单击屏幕上的元素。Screen s = new Screen(); s.click(“QA.png”);
doubleClick双击此方法用于双击元素。它接受图像名称作为参数。Screen s = new Screen(); s.doubleClick(“QA.png”);
Type 输入此方法用于向元素提供输入值。它接受图像名称和文本作为参数。s.type(“QA.png”,“TEXT”);
Hover 悬停此方法用于将鼠标悬停在元素上。 它接受图像名称作为参数。s.hover(“QA.png”);
Find 查找此方法用于在屏幕上查找特定元素。 它接受图像名称作为参数。s.find(“QA.png”);

Sikuli的类Pattern

Pattern类用于将图像文件与其他属性相关联,以唯一标识元素。它将图像的路径作为参数。
Pattern p = new Pattern(“Path of image”);
下面是模式类最常用的方法。

MethodDescriptionSyntax(语法)
getFileName返回Pattern 对象中包含的文件名。Pattern p = new Pattern(“D:\Demo\QA.png”); String filename = p.getFileName();
similar此方法返回一个新的Pattern对象,其相似性设置为指定值。 它接受0到1之间的相似性值作为参数。 Sikuli查找属于指定相似范围的所有元素并返回一个新的模式对象。Pattern p1 = p.similar(0.7f);
Exact该方法返回一个新的Pattern对象,相似度设置为1。它只查找指定元素的精确匹配Pattern p1 = p.exact();

使用Sikuli上传文件的代码示例

下面的代码解释了如何使用Sikuli在Chrome浏览器中上传文件。
//不要直接下载sikuli.script.jar导入,一定要按照上面的方式生成。

package com.morningstar.sikuli;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
import org.openqa.selenium.chrome.ChromeDriver;

public class SikuliDemo {
    public static void main(String[] args) throws FindFailed {
    	System.setProperty("webdriver.chrome.driver", "resources/Driver/chromedriver.exe");
        String filepath = "resources/";
        String inputFilePath = "resources/";
        Screen s = new Screen();
        Pattern fileInputTextBox = new Pattern(filepath + "FileTextBox.PNG");
        Pattern openButton = new Pattern(filepath + "OpenButton.PNG");
        WebDriver driver;

        // Open Chrome browser    
        driver = new ChromeDriver();
        driver.get("http://demo.guru99.com/test/image_upload/index.php");

        // Click on Browse button and handle windows pop up using Sikuli
        driver.findElement(By.xpath(".//*[@id='photoimg']")).click();
        s.wait(fileInputTextBox, 20);
        s.type(fileInputTextBox, inputFilePath + "Test.docx");
        s.click(openButton);

        // Close the browser
        driver.close();
    }
}

代码解读:
Step 1)、 第一个语句涉及为chrome设置驱动程序可执行路径。

System.setProperty("webdriver.chrome.driver", "D:\\ chromedriver.exe");

Step 2)、 使用截屏工具等截屏工具对弹出的“FileTextBox”和“Open”按钮进行截屏。
在这里插入图片描述
你的截图应该是这样的:
在这里插入图片描述
Windows文件输入文本框打开按钮的图像存储成 “FileTextBox.PNG”和“OpenButton.PNG”。
Sikuli使用图像识别技术来识别屏幕上的元素。它只根据元素的图像在屏幕上找到元素。
例如:如果您想要自动打开记事本,那么您需要将记事本桌面图标的图像存储到PNG文件中,并对其执行单击操作。
在我们上述的例子中,它识别文件输入文本框,并使用存储的图像在Windows弹出窗口上打开按钮。如果屏幕分辨率从图像捕获更改为测试脚本执行,Sikuli的结果将是不一致的。因此,总是建议在捕获图像的同一分辨率上运行测试脚本。图像像素大小的改变将导致Sikuli抛出FindFailed异常。

Step 3)、 接下来的语句包括创建Screen和Pattern类对象。创建一个新的screen对象。设置上载的文件路径设置为Pattern对象的参数。

Screen s = new Screen();
Pattern fileInputTextBox = new Pattern(filepath + "FileTextBox.PNG");
Pattern openButton = new Pattern(filepath + "OpenButton.PNG");

Step 4) 以下语句涉及使用chrome浏览器打开URL:http://demo.guru99.com/test/image_upload/index.php

driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/image_upload/index.php");

上面的URL是一个演示应用程序,演示文件上传功能。

Step 5) 点击choose file按钮使用下面的语句

driver.findElement(By.xpath(".//*[@id='photoimg']")).click(); 

Step 6) 等待弹出窗口出现。 Wait方法用于处理与单击浏览按钮后弹出窗口相关的延迟。

s.wait(fileInputTextBox, 20);

Step 7) 在“输入文件”文本框中输入文件路径,然后单击“打开”按钮

s.type(fileInputTextBox, inputFilePath + "Test.docx");
s.click(openButton);

Step 8) 关闭浏览器

driver.close();

最后:
起初,脚本打开了浏览器
在这里插入图片描述
点击“Choose File”按钮,将会出现windows文件弹出窗口。将数据输入文件输入文本框并单击“打开”按钮
在这里插入图片描述
完成文件上传并关闭浏览器后,将显示下面的屏幕
在这里插入图片描述
结论:
Sikuli用于处理网页上的flash对象和Windows弹出窗口。 当用户界面上的元素不经常更改时,最好使用Sikuli。 由于这个缺点,从自动化测试的角度来看,与其他框架(如Robot和AutoIT)相比,Sikuli的偏好较少。

番外篇-----图片校验:

Sikuli可以用来识别图片中是否有某一部分图片,这个是很有用的,比如一整张图片中检查是否有某块明显的Logo之类的图片。

String imgPath = "xxxx/image.png";
Screen s = new Screen();
boolean test = s.exists(imgPath).isValid();
System.out.println("########Test======"+test);

或者:

String imgPath = "xxxx/image.png";
Screen s = new Screen();
Assert.assertTrue(s.exists(imgPath).isValid(), "can not find the LOGO:" + Page1And16);
.
.
.
	//基于图片对象查找
    public static boolean isExist(String iconPath) throws FindFailed{
        boolean isVaild = false;
        pattern = new Pattern(iconPath);
        screen = new Screen();
        try {
        	//screen.wait(iconPath, 20);
        	Image image = screen.find(pattern).getImage();
            System.out.println("########IMAGE===="+image);
            isVaild = image != null;
        } catch (FindFailed e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
        clear();
        return isVaild;
    }
    
    public static void clear(){
        if(pattern != null){
            pattern = null;
        }
        if(screen != null){
            screen = null;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软测小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值