当前位置:网站首页>Sikulix select the picture of relative position (advanced version)
Sikulix select the picture of relative position (advanced version)
2022-06-22 14:38:00 【qq_ four hundred and ninety-two million four hundred and forty-】
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.Match;
import org.sikuli.script.Region;
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 final String WIN_DRIVER_PATH = "D:/Program Files/nodejs/node.exe";
public static final String WIN_APPIUM_PATH = "C:/Users/jeff.xie/AppData/Roaming/npm/node_modules/appium/build/lib/main.js";
public static final String MAC_DRIVER_PATH = "/usr/local/bin/node";
public static final String MAC_APPIUM_PATH = "/usr/local/lib/node_modules/appium/build/lib/main.js";
int width;
int height;
AndroidDriver driver;
public static void main(String[] args) throws URISyntaxException, IOException {
TestSikulix testOpenCV = new TestSikulix();
testOpenCV.startMain();
}
public DesiredCapabilities setAndroidDesiredCapabilities(){
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);
return devices;
}
public void getWindowWidthAbdHeight(){
width = driver.manage().window().getSize().width;
height = driver.manage().window().getSize().height;
}
@SneakyThrows
public void startMain() throws URISyntaxException, IOException {
// Need to open appium server, And start the simulator
//driver= new AndroidDriver(new URL("http://localhost:4723/wd/hub"),setAndroidDesiredCapabilities());
// Don't need to open appium The desktop version
AppiumDriverLocalService service =startAppiumService();
driver = new AndroidDriver(service.getUrl(), setAndroidDesiredCapabilities());
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";
String target ="D:\\images\\test\\test01.png";
getWindowWidthAbdHeight();
Region r = new Region(0,0,width,height);
Match match=r.exists(target);
//Match match = r.find(target);
Region region=match.highlight();
String locations=region.toStringShort(); String matchString = match.toStringShort();
System.out.println("matchString:"+matchString);
System.out.println("match.x:"+match.x);
System.out.println("match.y:"+match.y);
System.out.println("match.h:"+match.h);
System.out.println("match.w:"+match.w);
System.out.println("match.getCenter:"+match.getCenter());
System.out.println("match.getScore(): "+match.getScore());
System.out.println("region.x:"+region.x);
System.out.println("region.y:"+region.y);
System.out.println("region.h:"+region.h);
System.out.println("region.w:"+region.w);
System.out.println("region.getCenter:"+region.getCenter());
System.out.println("locations: "+locations);
System.out.println("match.getScore(): "+match.getScore());
// [log] CLICK on L[120,648]@S(0) (546 msec) // [log] highlight M[46,621 148x54]On(0) S 10000 for -1.0 secs // region.x:46 The picture is on the left side of the interface 46 // region.y:621 The image in the interface transition position is 4621 // region.h:54 Height of picture // region.w:148 Width of picture // region.getCenter:L[120,648]@S(0) // Click on the center point of the picture Left position 46 + Width 148/2 =120 Height position 621 + Picture height 54/2 =648 // match.getScore(): 1.0 score 1.0 Description match is 100%
System.out.println(match.h);
System.out.println(match.w);
match.highlight();
Thread.sleep(5000);
s.click(target);
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;
}
}
Sikulix Select a picture or object in relative position
Generally speaking, there is such a demand , I already have a picture element , There will be a dynamically displayed object around this element , I'm going to do a click or hover Or simply picture the object save To local , Keep it as the object to be clicked on the next page , In this case, you can use sikuli To solve .
Here I still follow baidu Take the home page as an example , Maybe not very typical , But it's OK to be an example .
I first click the skin change button , Then when the skin changing picture is known , Click to take pictures from other directions , and save Go to the local location to retain the object to be clicked by the next script .
1. So let's set up a region object , This region It's a square area , I will default to the resolution size
Region r = new Region(0,0,1023,767);
2. Click the skin button
r.click("img/ChangeSK.png");
3. I have a skin picture in the center of all skin pictures
r.exists("img/SK1.png");
Under operation , The result is no problem :
[debug] Region: exists: img/SK1.png has appeared
4. Find this picture and use it as a Match object ,highlight And print the similarity , At the same time print out width and height
Match SK1 = r.find("img/SK1.png");
System.out.println(baidu.getScore());
SK1.highlight();
System.out.println(SK1.h);
System.out.println(SK1.w);
result 0.9999843239784241, Indicates that... Has been found
5. Create a new one Location object , To store this object in region The coordinates of
Location Loc1 = SK1.getTarget();
Note that describing a picture has four dimensions ,start x,start y,width,height. there location It means x+height/2, y+width/2, That is, the central position
Here we need a tool to get the coordinates on the screen , In this way, the relative position of the picture can be accurately obtained during the manual test , There are many such tools on the Internet , Search and you will have
6. Get Location after , I just need to know in this SK1 An image of the same size above the object , How to do it here :
Location Loc2 = Loc1.above(60);
Through the tool, we know that the position of the picture above is probably higher than 60pix, So build a new location object
7. Because we already know the picture above width and height, So now you need to create a new one Match Just the object
Match SK2 = new Match(SK1);
SK2.setLocation(Loc2);// It is wrong to do so , Because here setLocation Not the central axis , It is set It becomes the starting coordinate , It should be :
SK2.y = SK2.y-60;
8, Print the similarity to see if it is found
SK2.highlight();
System.out.println(SK2.getScore());
The score is 1, That's all right.
9, Put this picture save To the local path
SK2.saveScreenCapture("img", "SK2");
SK2-1444961093063.png Save name should be followed by timestream, Should be to avoid coverage , Change sikuli jar Just recompile the source code in the package .
And so selenium Together, it can greatly enhance case Coverage of
边栏推荐
- 验证码是自动化的天敌?看看大神是怎么解决的
- 基于SSM框架实现的甜品饮品店前后台管理系统甜品商城蛋糕店【源码+数据库】
- [introduction to postgraduate entrance examination] analysis of postgraduate entrance examination data of Cyberspace Security Major of Beijing Jiaotong University from 2018 to 2022
- 哈希索引设计发展史
- Kukai TV ADB
- C#定义和实现Interface接口
- Unity 子线程调用主线程的UI
- How to protect WordPress websites from cyber attacks? It is important to take safety measures
- Unity sub thread calls UI of main thread
- 11 method reference and constructor application
猜你喜欢

线下实体店结合VR全景,让虚拟购物更加真实
Implementation of redis+caffeine two-level cache

验证码是自动化的天敌?看看大神是怎么解决的

3dMax建模笔记(一):介绍3dMax和创建第一个模型Hello world

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷

C# Winform 相册功能,图片缩放,拖拽,预览图分页

JS advanced programming version 4: learning iterators

Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?

开源SPL重新定义OLAP Server

Shan Zhiguang, chairman of BSN Development Alliance: DDC can provide the underlying support for the development of China's meta universe industry
随机推荐
In 5g era, how to create an amazing live VR activity?
[untitled]
Implementation of redis+caffeine two-level cache
机器学习之支持向量机
How MySQL enables multiple auto incrementing columns in a table
Analysis on data skew of redis slice cluster
一文彻底弄懂建造者模式(Builder)
Understand the quality assurance of open source software (OSS)
Neuron+ekuiper realizes data collection, cleaning and anti control of industrial Internet of things
JS高级程序设计第 4 版:迭代器的学习
D damage and safety
利用图片实现APP元素定位sikulix
Summary of patent blog writing
Stm32f1 and stm32subeide programming example - Optical intermittent sensor drive
What is the difference between Z-score and deltf/f?
Getting started with go web programming: validators
基于SSM框架实现的甜品饮品店前后台管理系统甜品商城蛋糕店【源码+数据库】
Groovy之闭包
Specific methods and steps of PROFINET communication between s7-200smart and Fanuc robot
Screenshot of the uniapp app and save it locally