自动化测试-知乎关注功能用例

package com.casedesign;
import java.util.List;
import java.util.NoSuchElementException;
import org.openqa.selenium.By;
import sun.reflect.generics.tree.Tree;
import io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class GuanZhu {
	public AndroidDriver driver;

	public GuanZhu(AndroidDriver driver) {
		this.driver = driver;
	}
	public void gz() throws Exception {
		List title = driver.findElements(By
				.id("com.zhihu.android:id/title"));
		System.out.println("jff");
		String titleName=null;
		for (AndroidElement arr : title) {
			titleName = arr.getText();
			arr.click();
			Thread.sleep(1000);
			try {
				if(!driver.getPageSource().contains("已关注")){
					//如果没有关注过,就点关注
					driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text(\"关注\")")).click();
					System.out.println(titleName);
					//写回答
					if(driver.getPageSource().contains("写回答")){
						driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text(\"写回答\")")).click();
						//关闭写回答
						driver.findElement(By.className("android.widget.ImageButton")).click();
						//点菜单
						driver.findElements(By.className("android.support.v7.app.a$c")).get(4).click();
					}
					break;
				}else {
					//如果已经关注了就返回,找下一条
					driver.findElementByClassName("android.widget.ImageButton").click();
					continue;
				}
			} catch (NoSuchElementException e) {
				System.out.println("没有找到元素");
			}
		}
		//进入我得关注,查找刚才关注的和里面的进行对比,如果有则关注成功
		driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text(\"我的关注\")")).click();
		List title_gz = driver.findElements(By.id("com.zhihu.android:id/title"));
		for(AndroidElement exp:title_gz){
			String title_gz_name=exp.getText();
			if(title_gz_name.equals(titleName)){
				System.out.println(title_gz_name);
				System.out.println("两个关注相同,检查通过");
			}else {
				System.out.println("检查不相同,检查失败");
			}		
			break;
		}
	}
}

 
 

你可能感兴趣的:(Appium自动化)