appium登录测试java_Appium测试开源中国登录实例

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

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

ac3f9e3ea2db5b99e7c628242ceb6098.png

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

9a51a019f56f0ce34480e73fbc20ea17.png

d4b03124adb424f01986979eda1d2ca1.png

三、连接手机到电脑,打开cmd窗口,用adb devices命令看看是否可以检测到手机设备

1d72d2df589c6467e54967adaa4e03b7.png

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

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

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

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

11034a7dcf2f67cc603f2e816fbe6faf.png

六、测试“开源中国”登录功能的实例,源代码如下:

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,启动成功如下图:

99fbfc49a06c1f1d586c39b48807e876.png

八、运行测试类TestOscLogin

831395a7d7086a1f5407dae4b286d3d2.png

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

ade920c808f005511136f1b84353720f.png

a932630a52599fed8e5baa7f52b92ccb.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值