1. 程式人生 > >appium-desktop 環境搭建 Java版

appium-desktop 環境搭建 Java版

用的是appium-desktop1.8.1,testng6.11,java-client6.1.0,selenium-java3.13.0

1、下載逍遙模擬器,裝好app

2、下載adb,用adb連線逍遙模擬器,找到abd路徑,在cmd裡cd D:\platform-tools-latest-windows\platform-tools ,  用 adb connect 127.0.0.1:21503

 

3、用adb devices,檢視是否連線到模擬器

4、下載desktop版,然後配置url和埠號

5、啟動server1.9.1,Start Inspector Session,配置Desired Capabilities,

使用adb shell dumpsys activity | findstr "mFocusedActivity" ,靠手速獲取點開應用的Activity,

使用adb shell pm list package -f 獲取包名

6、start session 進入到定位元素頁面,大功告成,可以寫指令碼了

7、第一個DEMO,建立maven專案,配置pom檔案如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>AppiumLatest</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<testFailureIgnore> false </testFailureIgnore>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>

</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>

8、建立一個hahaha的類,如下

package com.yidong.Test;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;

public class hahaha {

static AndroidDriver<WebElement> driver;
static DesiredCapabilities capabilities;

@BeforeTest
public static void start() throws MalformedURLException, InterruptedException{


driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"),setCapsforMobileBrowser(capabilities));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("com.creditease.android:id/btnStart")).click();
driver.findElement(By.id("com.creditease.android:id/agree")).click();
WebElement tvlift=driver.findElementById("com.creditease.android:id/tv_life");
Assert.assertEquals(tvlift.isEnabled(), true);

}

@Test
public void init() throws MalformedURLException, InterruptedException {

try {
start();


} catch (Exception e) {
System.out.println("inside catch");
e.printStackTrace();
}

}


public static DesiredCapabilities setCapsforMobileBrowser(DesiredCapabilities capabilities){

capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "1.8.1");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "SM-A053F");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.creditease.android");
capabilities.setCapability("appActivity", ".view.activity.home.SplashH5AdvertisementActivity");
capabilities.setCapability("noRest", true);
return capabilities;
}

@AfterTest
public void tearDown(){
driver.quit();

}

}

9.執行,testng test,成功