Appium环境搭建
Appium环境搭建 Java版本
文章转发来源 https://www.cnblogs.com/ydnice/p/5787800.html
一、安装node.js
1、到官网下载node.js:https://nodejs.org/en/download/
2、获取到安装文件后,直接双击安装文件,根据程序的提示,完成nodejs的安装。
3、安装完成后,运行cmd,输入node –v,如果安装成功,会输出如下版本信息:v10.14.1
二、配置Android sdk环境
1、sdk环境配置:http://www.cnblogs.com/puresoul/p/4597211.html
2、确保安装了 Level 17 或以上的版本 api
3、设置 ANDROID_HOME 系统变量为你的 Android SDK 路径
F:\1Study\Andriod\51zxw_2018-0102\Sdk
4、把 tools 和 platform-tools 两个目录加入到系统的 Path 路径里
F:\1Study\Andriod\51zxw_2018-0102\Sdk\platform-tools;F:\1Study\Andriod\51zxw_2018-0102\Sdk\tools
三、安装手机驱动并测试连接真机
完成上述步骤以后,为了能够让手机连接到PC端进行真机测试。还需要安装测试手机对应的驱动程序。根据手机型号提前下载相应的离线驱动并安装,之后将手机与PC通过usb线相连。在cmd中输入以下命令,如果能够看到设备,则表示安装成功。
adb devices
D8Y0215B02005857
四、安装Appium
1.下载安装文件:https://bitbucket.org/appium/appium.app/downloads/
2.直接双击appium-installer.exe文件安装就好,桌面会生成一个appium的图标
3.把node_modules的bin目录放到系统的Path路径里
C:\Program Files (x86)\Appium\node_modules\.bin
4.检查appium所需的环境是否OK://参考https://blog.csdn.net/u011296485/article/details/79494110
进入cmd命令行,输入appium-doctor ,出现以下提示,All Checks were successful ,说明环境成功。
ANDROID_HOME is set to "F:\1Study\Andriod\51zxw_2018-0102\Sdk"
JAVA_HOME is set to "D:\Program Files (x86)\java\bin."
ADB exists at F:\1Study\Andriod\51zxw_2018-0102\Sdk\platform-tools\adb.exe
Android exists at F:\1Study\Andriod\51zxw_2018-0102\Sdk\tools\android.bat
Emulator exists at F:\1Study\Andriod\51zxw_2018-0102\Sdk\tools\emulator.exe
Android Checks were successful.
All Checks were successful
JDK环境配置:
JAVA_HOME C:\Program Files\Java\jdk1.8.0_151\bin
CLASSPATH .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
path C:\ProgramData\Oracle\Java\javapath;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;D:\Program Files (x86)\java\bin;D:\Program Files (x86)\mysql\MySQL Server 5.6\bin;F:\1Study\Andriod\51zxw_2018-0102\android-sdk\sdk\tools;F:\1Study\Andriod\51zxw_2018-0102\Sdk\platform-tools;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Users\Administrator\AppData\Local\Programs\Appium;C:\Users\Administrator\AppData\Local\Programs\Appium\resources\app\node_modules.bin;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools;C:\Python27;C:\Program Files (x86)\Appium\node_modules\.bin
二、Appium入门实例(Java)
一、使用Eclipse直接创建案例工程
1、打开Eclipse,【File】-->【New】-->【Project】
2、选择【Java Project】-->【Next】
3、输入工程名称Appium_demo,点击【Finish】
4、右键点击工程 New-Folder,新建两个文件夹:apps和libs,目录结构如下:
二、导入测试的类库
1、导入Selenum类库:http://docs.seleniumhq.org/download/
1) selenium-server-standalone-2.44.0.jar
2、导入Appium类库:
工程需要导入4个jar包
selenium-java-2.44.0.jar
selenium-java-2.44.0-srcs.jar
java-client-7.0.0.jar
selenium-server-standalone-2.44.0.jar
三、下载测试APK
1、下载测试的文件ContactManager.apk:https://github.com/appium/sample-code/tree/master/sample-code/apps/ContactManager
2、将下载的apk放到项目的apps目录下
四、建立package包和案例文件
1、在src文件夹上右键单击,【New】-->【package】,输入包名:com.dan.demo,点击【Finish】
2、在package下新建类:ContactsTest.java,如下:
五、 启动Android模拟器(也可以连接真机)
1、cmd输入:android avd,选择模拟器,点击【Start】即可。
2、启动好后,cmd输入:adb devices,确定设备是否连接上,如下图连接成功:
六、 启动Appium
方法一:cmd输入:appium
方法二:
1、直接双击appium gui图标(如下图):
2、点击右上角的启动按钮,启动日志如下:
七、 运行测试案例:
1、在eclipse中,项目右键>【Run As】>【JUnit Test】,运行过程截图如下:
具体jar包可到如下网站下载:
http://selenium-release.storage.googleapis.com/index.html?path=3.5/
更换新的jar包后
java-client-4.12.jar http://maven.outofmemory.cn/io.appium/java-client/4.1.2/
selenium-java-2.53.1.zip
selenium-server-standalone-2.53.1.jar
未提示config build path———修改成功
目前不知道java-client与selenium-版本 对应关系,后续记录更改
//测试代码贴上来:
package com.dan.demo;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import java.io.File;
import java.net.URL;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
public class ContactsTest {
private AppiumDriver driver;
@Before
public void setUp() throws Exception {
// set up appium
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/apps");
File app = new File(appDir, "ContactManager.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","D8Y0215B02005857");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.example.android.contactmanager");
capabilities.setCapability("appActivity", ".ContactManager");
//appium 与eclipise之间通信协议
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println("App is launched!");
}
@Test
public void addContact(){
WebElement el = driver.findElement(By.name("Add Contact"));
el.click();
List<AndroidElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
textFieldsList.get(0).sendKeys("Some Name");
textFieldsList.get(2).sendKeys("Some@example.com");
driver.swipe(100, 500, 100, 100, 2);
driver.findElementByName("Save").click();
System.out.println("App is done!");
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
当前报错:
org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Bad app: C:\Users\Administrator\eclipse-workspace\Appium_demo\apps\ContactManager.apk. App paths need to be absolute, or relative to the appium server install dir, or a URL to compressed file, or a special app name. cause: Error: Error locating the app: ENOENT, stat 'C:\Users\Administrator\eclipse-workspace\Appium_demo\apps\ContactManager.apk') (WARNING: The server did not provide any stacktrace information)
解决办法时候:
发现可正常运行的脚本,并不需要初始化设置app路径,也即没有
// File classpathRoot = new File(System.getProperty("user.dir"));
//
// File appDir = new File(classpathRoot, "/apps");
//
// File app = new File(appDir, "ContactManager.apk");
//
// capabilities.setCapability("app", app.getAbsolutePath());
到此第一个自动化脚本开发完成
package com.dan.demo;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
public class CalculatorTest {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "D8Y0215B02005857");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("appPackage", "com.android.calculator2");
capabilities.setCapability("appActivity", ".Calculator");
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.findElement(By.name("1")).click();
driver.findElement(By.name("5")).click();
driver.findElement(By.name("9")).click();
driver.findElement(By.name("delete")).click();
driver.findElement(By.name("+")).click();
driver.findElement(By.name("6")).click();
driver.findElement(By.name("=")).click();
Thread.sleep(2000);
String result = driver.findElement(By.id("com.android.calculator2:id/formula")).getText();
System.out.println(result);
driver.quit();
}
}