下面以测试开源中国登录为例,安卓机。
一、打开uiautomatorviewer,点击 “Device screenshot”, 连接手机应用的打开窗口将显示出来,点击“点击头像登录”,选中该按钮,查看右边的panel来找到“text”或“resource-id” 或“class”的值,如下图。具体请详见“如何使用uiautomatorviewer来扫描和分析UI组件”博文。
二、打开eclipse软件,打开工程目录,点击OK,进入软件主界面,新建一个类。
三、连接手机到电脑,打开cmd窗口,用adb devices命令看看是否可以检测到手机设备
记下设备名:f0bcade5,测试代码要使用。
四、打开手机设置,查看手机设备的版本号,测试代码要使用到。
五、打开“开源中国”APP源代码的”AndroidManifest.xml”,找到包名。测试代码要使用到。
开源中国APP的源代码地址:http://git.oschina.net/oschina/android-app
六、测试“开源中国”登录功能的实例,源代码如下:
package com.appium.osc;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class TestOscLogin {
private AppiumDriver driver;
// private boolean isInstall = false;
@BeforeClass
public void setUp() throws Exception {
// 配置appium相关参数
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "f0bcade5");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.4.4");
// 配置测试apk
capabilities.setCapability("appPackage", "net.oschina.app");
capabilities.setCapability("appActivity","net.oschina.app.LaunchActivity");
capabilities.setCapability("appWaitActivity","net.oschina.app.LaunchActivity"); //A new session could not be created的解决方法
capabilities.setCapability("sessionOverride", true); // 每次启动时覆盖session,否则第二次后运行会报错不能新建session
// 如果真机设备已经安装,则不需要重新安装
// File classpathRoot = new File(System.getProperty("user.dir"));
// File appDir = new File(classpathRoot, "apps");
// File app = new File(appDir,"osc-android-v2.6.9-oschina-release.apk");
// capabilities.setCapability("app", app.getAbsolutePath());
//启动appium
driver = new AndroidDriver(new URL("http://192.168.199.110:4723/wd/hub"),capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void TestLogin() throws InterruptedException {
// 点击【我的】
WebElement itemMe = driver.findElement(By.id("net.oschina.app:id/nav_item_me"));
itemMe.click();
// 点击头像登录
WebElement loginbtn = driver.findElement(By.id("net.oschina.app:id/tv_nick"));
loginbtn.click();
//进入登录页面
WebElement viewLogin = driver.findElement(By.id("net.oschina.app:id/bt_login_submit"));
System.out.println("========" + viewLogin.getText());
//输入账号和密码,登录
WebElement delUsername = driver.findElement(By.id("net.oschina.app:id/iv_login_username_del"));
delUsername.click();
WebElement loginuUsername = driver.findElement(By.id("net.oschina.app:id/et_login_username"));
loginuUsername.sendKeys("xxxxx@163.com");
WebElement loginuPwd = driver.findElement(By.id("net.oschina.app:id/et_login_pwd"));
loginuPwd.clear();
loginuPwd.sendKeys("123456");
WebElement loginBtn = driver.findElement(By.id("net.oschina.app:id/bt_login_submit"));
loginBtn.click();
// 点击【我的】
WebElement itemMeTwo = driver.findElement(By.id("net.oschina.app:id/nav_item_me"));
itemMeTwo.click();
//进入登录成功的个人信息界面
WebElement loginName = driver.findElement(By.id("net.oschina.app:id/tv_nick"));
if(loginName.getText().equals("登录人名称") == true){
System.out.println("====目前在个人信息界面登录人是" + loginName.getText());
Reporter.log("点击头像登录成功");
}
//点击设置,进入设置页面,注销
WebElement setting = driver.findElement(By.id("net.oschina.app:id/iv_logo_setting"));
setting.click();
WebElement settingclose = driver.findElement(By.name("注销"));
settingclose.click();
//点击
WebElement returnButton = driver.findElement(By.id("net.oschina.app:id/action_bar"));
WebElement clickButton = returnButton.findElement(By.className("android.widget.ImageButton"));
clickButton.click();
WebElement loginNameAssert = driver.findElement(By.name("点击头像登录"));
System.out.println("=====" + loginNameAssert.getText()+ "成功" );
}
@AfterClass
public void tearDown() throws Exception {
driver.quit();
}
}
七、手机设备连接到PC端,手机屏幕打开着。接着启动Appium,启动成功如下图:
八、运行测试类TestOscLogin
九、大功告成,注意查看运行日志和appium控制台日志