作者:卢策
一、weex app回归面临的问题分析:
package com.kaola.test.legomobileuitest;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class AndroidKaolaTest {
private static AndroidDriver<?> driver;
ImageCompare imageCompare= new ImageCompare();
@BeforeClass
public static void beforeClass() throws Exception {
File appDir = new File("../../../apps");
File app = new File(appDir, "kaola_30160010.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("noReset", true); //不需要再次安装
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "TEX9K17A16900528");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
//support Chinese支持中文输入
capabilities.setCapability("unicodeKeyboard","True");
//重置输入法为系统默认
capabilities.setCapability("resetKeyboard","True");
//no need sign 安装时不对apk进行重签名,设置很有必要,否则有的apk在重签名之后无法正常使用
capabilities.setCapability("noSign","True");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@Before
public void setUp() throws Exception {
if (driver != null){
driver.startActivity(new Activity("com.kaola", "com.kaola.modules.boot.splash.SplashActivity"));
}
}
@AfterClass
public static void afterClass() throws Exception {
if (driver != null)
driver.quit();
}
@Test
public void check() {
System.out.println("success!");
try {
Thread.sleep(10 * 1000);
// permission(driver,"禁止");
permission(driver,"取消");
permission(driver,"暂不升级");
// driver.findElementById("com.kaola:id/dialog_close_image_iv_close").click();
driver.findElementById("com.kaola:id/kaola_image_layout_view").click();
Thread.sleep(5 * 1000);
//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,
// 其含义是将截取的屏幕以文件形式返回。
File srcFile = driver.getScreenshotAs(OutputType.FILE);
//利用FileUtils工具类的copyFile()方法保存getScreenshotAs()返回的文件对象。
// FileUtils.copyFile(srcFile, new File("/Users/administrator/Desktop/11.png"));
FileUtils.copyFile(srcFile, new File("/Users/administrator/Desktop/12.png"));
Thread.sleep(3 * 1000);
System.out.println("截图成功");
imageCompare.compareTest("/Users/administrator/Desktop/11.png","/Users/administrator/Desktop/12.png",true);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void check2() {
System.out.println("success!");
try {
Thread.sleep(10 * 1000);
// permission(driver,"禁止");
permission(driver,"取消");
permission(driver,"暂不升级");
// driver.findElementById("com.kaola:id/dialog_close_image_iv_close").click();
driver.findElementById("com.kaola:id/kaola_image_layout_view").click();
Thread.sleep(5 * 1000);
//指定了OutputType.FILE做为参数传递给getScreenshotAs()方法,
// 其含义是将截取的屏幕以文件形式返回。
File srcFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File("/Users/administrator/Desktop/13.png"));
Thread.sleep(3 * 1000);
imageCompare.compareTest("/Users/administrator/Desktop/11.png","/Users/administrator/Desktop/13.png",false);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void permission(AndroidDriver<?> driver,String btnText) {
try {
if (driver.getPageSource().contains(btnText)) {
driver.findElement(By.xpath("//android.widget.Button[contains(@text,'"+btnText+"')]")).click();
}
}
catch (NoSuchElementException e1) {
System.out.println("not contains button:"+btnText);
}
}
}
package com.kaola.test.legomobileuitest;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
public class ImageCompare {
public static int[] getData(String name) {
try {
BufferedImage img = ImageIO.read(new File(name));
BufferedImage slt = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
slt.getGraphics().drawImage(img, 0, 0, 100, 100, null);
// ImageIO.write(slt,"jpeg",new File("slt.jpg"));
int[] data = new int[256];
for (int x = 0; x < slt.getWidth(); x++) {
for (int y = 0; y < slt.getHeight(); y++) {
int rgb = slt.getRGB(x, y);
Color myColor = new Color(rgb);
int r = myColor.getRed();
int g = myColor.getGreen();
int b = myColor.getBlue();
data[(r + g + b) / 3]++;
}
}
// data 就是所谓图形学当中的直方图的概念
return data;
} catch (Exception exception) {
System.out.println("有文件没有找到,请检查文件是否存在或路径是否正确");
return null;
}
}
public static float compare(int[] s, int[] t) {
try {
float result = 0F;
for (int i = 0; i < 256; i++) {
int abs = Math.abs(s[i] - t[i]);
int max = Math.max(s[i], t[i]);
result += (1 - ((float) abs / (max == 0 ? 1 : max)));
}
return (result / 256) * 100;
} catch (Exception exception) {
return 0;
}
}
@Test
public void test11() {
compareTest("/Users/administrator/Desktop/1.png", "/Users/administrator/Desktop/2.png", true);
}
@Test
public void test12() {
compareTest("/Users/administrator/Desktop/1.png", "/Users/administrator/Desktop/3.png", false);
}
@Test
public void test13() {
compareTest("/Users/administrator/Desktop/1.png", "/Users/administrator/Desktop/4.png", false);
}
@Test
public void test14() {
compareTest("/Users/administrator/Desktop/1.png", "/Users/administrator/Desktop/5.png", false);
}
public void compareTest(String filepath1, String filepath2, boolean flag) {
double percentDefault = 99.99;
float percent = compare(getData(filepath1),
getData(filepath2));
if (percent == 0) {
System.out.println("无法比较");
} else {
System.out.println("两张图片的相似度为:" + percent + "%");
}
if (flag) {
assertTrue(percent > percentDefault);
} else {
assertFalse(percent > percentDefault);
}
}
}
网易云大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经作者卢策授权发布