1. 程式人生 > >【Appnium 小試牛刀一】根據元素係數座標,計算不同機型相對座標,進行元素點選操作

【Appnium 小試牛刀一】根據元素係數座標,計算不同機型相對座標,進行元素點選操作

package main;

import io.appium.java_client.AppiumDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.remote.DesiredCapabilities;

public class app_init {

    public static AppiumDriver set_up() throws Exception {
    	AppiumDriver driver;

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("deviceName", "W4OZLZ6L4TWG4HCU");
        capabilities.setCapability("platformVersion", "5.1");
        capabilities.setCapability("appPackage","com.fuzhong.xiaoliuaquatic");
        capabilities.setCapability("app","D:\\XiaoLiuAquatic_product_6.0.4.apk");
        capabilities.setCapability("noReset","True");
        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        return driver;
    }

	/**獲取變動的X座標值
	 * 原理:根據設定係數(以某個機型作為標準,獲取此機型的width值和元素座標X值),再獲取當前測試機的width值,得到當前測試機的相對座標X值
	 * @param driver
	 * @param x    參考機型該元素的x座標值
	 * @param s_w  參考機型的width值
	 * @return  返回當前機型的x座標值
	 */
	public static int Get_x_coordinate(AppiumDriver driver,double x, int s_w){
		double a = x / s_w;
		int w = driver.manage().window().getSize().width;
		int x1= (int) (a * w);
		return x1;		
	}
	
	/**獲取變動的Y座標值
	 * 	原理:根據設定係數(以某個機型作為標準,獲取此機型的height值和元素座標Y值),再獲取當前測試機的height值,得到當前測試機的相對座標Y值 
	 * @param driver
	 * @param y   參考機型該元素的y座標值
	 * @param s_h  參考機型的height值
	 * @return   返回當前機型的y座標值
	 */
	public static int Get_y_coordinate(AppiumDriver driver,double y, int s_h){
		double b = y / s_h;
		int h = driver.manage().window().getSize().height;
		int y1= (int) (b * h);
		return y1;
	}


	public static void main(String[] args) throws Exception {
		AppiumDriver drivers = set_up();
        //獲取當前測試機操作元素的座標:star(x1,y1)座標到end(x2,y2)座標
		int x1 = Get_x_coordinate(drivers, 54.00, 720);
		int y1 = Get_y_coordinate(drivers, 578.00, 1184);
		int x2 = Get_x_coordinate(drivers, 666.00, 720);
		int y2 = Get_y_coordinate(drivers, 658.00, 1184);
		System.out.print("x1:" + x1 + "\n");
		System.out.print("y1:" + y1 + "\n");
		System.out.print("x2:" + x2 + "\n");
		System.out.print("y2:" + y2 + "\n");
		drivers.swipe(x1, y1, x2, y2, 2);
    }
}