Sikuli教程:如何將Sikuli與Selenium結合使用(示例)

什麼是Sikuli?

Sikuli是一個基於GUI的開源自動化工具。它用於與Web頁面的元素交互並處理基於windows的彈出窗口(頁面)。它使用“圖像識別”技術與網頁彈出窗口的元素進行交互。Sikuli將Web頁面的所有元素視爲圖像,並根據其圖像識別這些元素。當UI元素穩定且不經常變化時,Sikuli是首選。

Sikuli與Selenium Webdriver的集成

Sikuli可以使用Sikuli JAR文件與selenium webdriver集成。
以下序列是使用selenium webdriver配置Sikuli的步驟列表。
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類提供的常用方法列表

Method Description Syntax(語法)
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”);
下面是模式類最常用的方法。

Method Description Syntax(語法)
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;
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章