Appium+Android教程_第三章(简单的微博登录测试脚本)

最近在自学Appium,写了个脚本,记录一下。

附上代码

Testcase0_微博登录

public void test0_login() throws Exception {	
		
		//driver.findElementByXPath("//*[@text='退出']").click();
		//微博提示获取权限
		if(driver.findElementById("com.sina.weibo:id/dialogTitle").isDisplayed())
		 driver.findElementByXPath("//android.widget.TextView[@text=\"		开启		\"]").click();
		//系统弹窗提示获取权限
		while(driver.currentActivity().matches("com.android.packageinstaller.permission.ui.GrantPermissionsActivity"))
		{
			driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click();
			
		}
		//由于每次到这里都会闪退,只能再用startActivity打开主Activity
		driver.startActivity("com.sina.weibo", ".VisitorMainTabActivity");
		//wait 3s
		Thread.sleep(3000);
		//点击登录button
	    driver.findElementById("com.sina.weibo:id/titleBack").click();
		//选择使用用户名和密码登录
		driver.findElementById("com.sina.weibo:id/tv_for_psw_login").click();;
		//输入用户名和密码和登录
		driver.findElementById("com.sina.weibo:id/et_pws_username").sendKeys("xxx@yeah.net");
		driver.findElementById("com.sina.weibo:id/et_pwd").sendKeys("********");
		driver.findElementById("com.sina.weibo:id/bn_pws_Login").click();
		//由于登录需要考虑网络速度,所以wait的时间设置大一点保证case不会错误
		Thread.sleep(10000);
		//登录后会进入web浏览器的点击验证码
		driver.findElementByXPath("//*[@content-desc='点击按钮进行验证']").click();
		Thread.sleep(15000);
		//断言判断当前的Activity
		assertEquals(".MainTabActivity", driver.currentActivity());
		Thread.sleep(3000);
		//截图保存
		takescreenshot(driver,"login.png");
		
		/*if(driver.findElementByXPath("//android.widget.TextView[@text=\"V9.6.3版本更新啦!\"]").isDisplayed())
				{
			driver.findElementByXPath("//android.widget.TextView[@text=\"以后再说\"]").click();
				}*/
	}

Testcase_1点赞微博和评论
这个case的使用封装成了likeandcomment()的方法,后面方便使用

@Test
	public void test1_commet() throws Exception
	{
		//wait 5s
		Thread.sleep(5000);
		
		//执行点击微博后->点赞->评论
		likeandcomment();	
	}

likeandcomment()

public void likeandcomment() throws Exception {
		//点击微博文字TextView
		driver.findElementById("com.sina.weibo:id/contentTextView").click();
		//点击点赞button
		driver.findElementById("com.sina.weibo:id/detail_liked_icon").click();
		//点击评论
		driver.findElementById("com.sina.weibo:id/comment").click();
		//输入评论
		driver.findElementById("com.sina.weibo:id/edit_view").sendKeys("Test by chin");
		Thread.sleep(1000);
		//点击发送button
		driver.findElementById("com.sina.weibo:id/tv_send").click();
		Thread.sleep(3000);
		//截图保存
	    takescreenshot(driver,"likeandcomment.png");
	    Thread.sleep(2000);
		//退出界面
		driver.findElementById("com.sina.weibo:id/detail_activity_header_left_button_text").click();
		Thread.sleep(2000);
	}

封装appium截图功能
appium的截图是继承webdriver selenium库的TakesScreenShot接口下的getScreenShotAs()

public static void takescreenshot(TakesScreenshot drivername, String filename)
	{
		//新建userPath获得当前路径
		String userPath=System.getProperty("user.dir");
		//调用 getScreenshotAs
		File screenshotFile=drivername.getScreenshotAs(OutputType.FILE);
		
		try {
			            System.out.println("save Screenshot path is:" + userPath + "/" + filename);
			            FileUtils.copyFile(screenshotFile, new File(userPath + "\\" + filename));
			        } catch (IOException e) {
			             System.out.println("Can't save screenshot");
			             e.printStackTrace();
			        } finally {
			             System.out.println("Screenshot finished, it's in " + userPath+ " folder");
			         }
	}

需要注意的地方是每一个Case执行前都会调用setUp()方法,以至于Testcase0执行结束后,Testcase1就会重置应用app的数据,所以这个case就肯定会执行错误,为了避免这个问题,在DesiredCapabilities里设置一个参数

cap.setCapability("noReset", true);//运行case时不清除app的应用数据

Testcase运行成功
在这里插入图片描述
输出的截图文件
在这里插入图片描述
完整的代码:
Testcase.java:

public class Testcase {
	
	 AndroidDriver driver;
	 AndroidElement element; 
	@Before
	public void setUp() throws Exception {
		/*在setUp()方法里声明setCapabilities();
		 * 为了方便后面的使用,把setCapabilities封装起来一个类,直接调用setup_Capabilities.java
		 */
		 setup_Capabilities setup=new setup_Capabilities();		 
		 driver=setup.setCapabilities();
	}
	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
	@Test
	public void test0_login() throws Exception {		
		//driver.findElementByXPath("//*[@text='退出']").click();
		//微博提示获取权限
		if(driver.findElementById("com.sina.weibo:id/dialogTitle").isDisplayed())
		 driver.findElementByXPath("//android.widget.TextView[@text=\"		开启		\"]").click();
		//系统弹窗提示获取权限
				while(driver.currentActivity().matches("com.android.packageinstaller.permission.ui.GrantPermissionsActivity"))
		{			driver.findElementById("com.android.packageinstaller:id/permission_allow_button").click();			
		}
		//由于每次到这里都会闪退,只能再用startActivity打开主Activity
		driver.startActivity("com.sina.weibo", ".VisitorMainTabActivity");
		//wait 3s
		Thread.sleep(3000);
		//点击登录button
	    driver.findElementById("com.sina.weibo:id/titleBack").click();
		//选择使用用户名和密码登录
		driver.findElementById("com.sina.weibo:id/tv_for_psw_login").click();;
		//输入用户名和密码和登录
		driver.findElementById("com.sina.weibo:id/et_pws_username").sendKeys("xxx@yeah.net");
		driver.findElementById("com.sina.weibo:id/et_pwd").sendKeys("********");
		driver.findElementById("com.sina.weibo:id/bn_pws_Login").click();
		//由于登录需要考虑网络速度,所以wait的时间设置大一点保证case不会错误
		Thread.sleep(10000);
		//登录后会进入web浏览器的点击验证码
		driver.findElementByXPath("//*[@content-desc='点击按钮进行验证']").click();
		Thread.sleep(15000);
		//断言判断当前的Activity
		assertEquals(".MainTabActivity", driver.currentActivity());
		Thread.sleep(3000);
		//截图保存
		takescreenshot(driver,"login.png");
		
		/*if(driver.findElementByXPath("//android.widget.TextView[@text=\"V9.6.3版本更新啦!\"]").isDisplayed())
				{
			driver.findElementByXPath("//android.widget.TextView[@text=\"以后再说\"]").click();
				}*/
	}
	@Test
	public void test1_commet() throws Exception
	{
		//wait 5s
		Thread.sleep(5000);	
		//执行点击微博后->点赞->评论
		likeandcomment();	
	}
	//将微博点赞评论封装成一个方法,方便后续调用
	public void likeandcomment() throws Exception {
		//点击微博文字TextView
		driver.findElementById("com.sina.weibo:id/contentTextView").click();
		//点击点赞button
		driver.findElementById("com.sina.weibo:id/detail_liked_icon").click();
		//点击评论
		driver.findElementById("com.sina.weibo:id/comment").click();
		//输入评论
		driver.findElementById("com.sina.weibo:id/edit_view").sendKeys("Test by chin");
		Thread.sleep(1000);
		//点击发送button
		driver.findElementById("com.sina.weibo:id/tv_send").click();
		Thread.sleep(3000);
		//截图保存
	    takescreenshot(driver,"likeandcomment.png");
	    Thread.sleep(2000);
		//退出界面
		driver.findElementById("com.sina.weibo:id/detail_activity_header_left_button_text").click();
		Thread.sleep(2000);
		
	}
	/*封装appium截图功能
	 * appium的截图是继承webdriver selenium库的TakesScreenShot接口下的getScreenShotAs()
	 */
	public static void takescreenshot(TakesScreenshot drivername, String filename)
	{
		//新建userPath获得当前路径
		String userPath=System.getProperty("user.dir");
		//调用 getScreenshotAs
		File screenshotFile=drivername.getScreenshotAs(OutputType.FILE);
		
		try {
			            System.out.println("save Screenshot path is:" + userPath + "/" + filename);
			            FileUtils.copyFile(screenshotFile, new File(userPath + "\\" + filename));
			        } catch (IOException e) {
			             System.out.println("Can't save screenshot");
			             e.printStackTrace();
			        } finally {
			             System.out.println("Screenshot finished, it's in " + userPath+ " folder");
			         }
	}
}

setup_Capabilities.java:

public class setup_Capabilities {
		
			
	public AndroidDriver setCapabilities() throws Exception
	{
		AndroidDriver driver;
		DesiredCapabilities cap=new DesiredCapabilities();
		cap.setCapability("automationName", "Appium");
		cap.setCapability("deviceName", "Tabs2");
		cap.setCapability("platformName", "Android");
		cap.setCapability("platformVersion", "6.0");
		cap.setCapability("udid", "f3c01270");
		cap.setCapability("appPackage", "com.sina.weibo");//app package name
		cap.setCapability("appActivity", ".SplashActivity");//app Activity
		cap.setCapability("unicodeKeyboard", "True"); //支持中文输入
		cap.setCapability("resetKeyboard", "True"); //支持中文输入,必须两条都配置
		cap.setCapability("noSign", "True"); //不重新签名apk
		cap.setCapability("newCommandTimeout", "30"); //没有新命令,appium30秒退出
		cap.setCapability("noReset", true);//运行case时不清除app的应用数据
		driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"),cap);
		return driver;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值