Appium测试开源中国登陆实例

下面以测试开源中国登陆为例,安卓机。java

1、打开uiautomatorviewer,点击 “Device screenshot”, 链接手机应用的打开窗口将显示出来,点击“点击头像登陆”,选中该按钮,查看右边的panel来找到“text”或“resource-id” 或“class”的值,以下图。具体请详见“如何使用uiautomatorviewer来扫描和分析UI组件”博文。android

2、打开eclipse软件,打开工程目录,点击OK,进入软件主界面,新建一个类。git

3、链接手机到电脑,打开cmd窗口,用adb devices命令看看是否能够检测到手机设备apache

记下设备名:f0bcade5,测试代码要使用。session

4、打开手机设置,查看手机设备的版本号,测试代码要使用到。app

5、打开“开源中国”APP源代码的” AndroidManifest.xml ”,找到包名。测试代码要使用到。eclipse

开源中国APP的源代码地址:http://git.oschina.net/oschina/android-appide

6、测试“开源中国”登陆功能的实例,源代码以下:测试

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();

       }

}

7、手机设备链接到PC端,手机屏幕打开着。接着启动Appium,启动成功以下图:ui

8、运行测试类TestOscLogin

9、大功告成,注意查看运行日志和appium控制台日志

相关文章
相关标签/搜索