当前位置:网站首页>Using pictures to locate app elements sikulix

Using pictures to locate app elements sikulix

2022-06-22 14:38:00 qq_ four hundred and ninety-two million four hundred and forty-

brief introduction :Sikuli  It's a novel graphic scripting language , Or an alternative automated testing technology . It is similar to our commonly used automated testing technology ( Tools ) There's a big difference .

sikulix  jar Package download address

SikuliX project files : SikuliX

You can also search this in the Alibaba cloud warehouse jar package

Warehouse Services

 

You need to download the following 3 A package ,

sikulixapi-2.0.5.jar sikulixapi-2.0.5-win.jar be used for windows

sikulixapi-2.0.5.jar sikulixapi-2.0.5-mac.jar be used for mac The computer

 

It can also be in pom Add... To the file , However, this method is not recommended , This package will have many dependent packages , The download may fail

You can also refer to :java+win7+eclipse+Maven+sikuli Configuration summary --- Graphic scripting language - Let's have a look

 <dependency>
    <groupId>com.sikulix</groupId>
    <artifactId>sikulixapi</artifactId>
    <version>1.1.0</version>
</dependency>
 

package com.welab.automation.projects.demo;

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import lombok.SneakyThrows;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.sikuli.script.Screen;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.Base64;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import static io.appium.java_client.service.local.flags.GeneralServerFlag.LOG_LEVEL;
import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE;

public class TestSikulix {


    public static void main(String[] args) throws URISyntaxException, IOException {
        TestSikulix testOpenCV = new TestSikulix();
        testOpenCV.startMain();
    }

    AndroidDriver driver;
    @SneakyThrows
    public void startMain()  throws URISyntaxException, IOException {
        DesiredCapabilities devices  = new DesiredCapabilities();
        devices.setCapability("platformName", "Android");
        devices.setCapability("deviceName","emulator-5554");
        devices.setCapability("platformVersion", "10");
        devices.setCapability("appPackage","com.android.settings");
        devices.setCapability("appActivity","com.android.settings.Settings");
        devices.setCapability("automation","uiautomator2");
        devices.setCapability("unicodeKeyboard", true);
        devices.setCapability("resetKeyboard",true);
        // Need to open appium server, And start the simulator 
        //driver= new AndroidDriver(new URL("http://localhost:4723/wd/hub"),devices);

        // Don't need to open appium The desktop version 
        AppiumDriverLocalService service =startAppiumService();
        driver = new AndroidDriver(service.getUrl(), devices);

        Screen s = new Screen();
        //  Set element not found , Automatic wait time 10 second 
        s.setAutoWaitTimeout(10);
        String project_path=System.getProperty("user.dir");
        System.out.println(System.getProperty("user.dir"));
        String path = project_path+ "\\src\\main\\java\\com\\welab\\automation\\projects\\demo\\Notifications.png";
        s.click("D:\\images\\test\\test01.png");

        Thread.sleep(5000);
        s.click("D:\\images\\test\\test02.png");
        System.out.println("pass");
    }

    public  void clickMobileElementByPicture(String picPath) throws URISyntaxException, IOException {
        File file = new File(picPath);
        String base64String = Base64.getEncoder().encodeToString(Files.readAllBytes(file.toPath()));
        By by= MobileBy.image(base64String);
        findMobileElement(by).click();
    }

    public MobileElement findMobileElement(By by) {
        return (MobileElement) driver.findElement(by);
    }

    private static AppiumDriverLocalService startAppiumService() {
        AtomicInteger port = new AtomicInteger();
        AppiumDriverLocalService service = null;
        service = new AppiumServiceBuilder()
                .usingAnyFreePort()
                .withIPAddress("0.0.0.0")
                .withArgument(SESSION_OVERRIDE)
                .withArgument(LOG_LEVEL, "error")
                .usingDriverExecutable(new File("D:\\Program Files\\nodejs\\node.exe"))
                .withAppiumJS(new File("C:\\Users\\jeff.xie\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))
                .build();
        Optional.ofNullable(service).ifPresent(s -> {
            s.start();
            port.set(s.getUrl().getPort());
        });
        AppiumDriverLocalService appiumDriverLocalService = service;
        return service;
    }

}

appium combination sikuli The simple practice of _Xiamen_wiley The blog of -CSDN Blog

原网站

版权声明
本文为[qq_ four hundred and ninety-two million four hundred and forty-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221317289471.html