案例1——汽车之家
在eclipse里使用对应的考试(或练习)秘钥登录并下载题目之后,打开项目的Main.java文件,接下来将在test函数里编写测试脚本。
package com.mooctest;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.AndroidKeyCode;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.UnreachableBrowserException;
public class Main {
/**
* 所有和AppiumDriver相关的操作都必须写在该函数中
* @param driver
* @throws InterruptedException
*/
public void test(AppiumDriver driver) throws InterruptedException {
try {
Thread.sleep(6000); //等待6s,待应用完全启动
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS); //设置尝试定位控件的最长时间为8s,也就是最多尝试8s
/*
* 余下的测试逻辑请按照题目要求进行编写
*/
Thread.sleep(2000);
driver.findElementByXPath("//android.widget.TextView[@text='下次再说']").click();
Thread.sleep(100);
driver.findElementByXPath("//android.widget.TextView[@text='推荐']").click();
Thread.sleep(1000);
driver.findElementByXPath("//android.widget.TextView[@text='原创']").click();
Thread.sleep(100);
driver.findElementByXPath("//android.widget.TextView[@text='车家号']").click();
Thread.sleep(100);
driver.findElementByXPath("//android.widget.TextView[@text='视频']").click();
Thread.sleep(100);
driver.findElementByXPath("//android.widget.TextView[@text='直播']").click();
Thread.sleep(300);
//滑动
swipeRight2Left(driver);
Thread.sleep(400);
driver.findElementByXPath("//android.widget.TextView[@text='图片']").click();
Thread.sleep(300);
driver.findElementByXPath("//android.widget.TextView[@text='话题']").click();
Thread.sleep(300);
driver.findElementByXPath("//android.widget.TextView[@text='游记']").click();
Thread.sleep(300);
driver.findElementByXPath("//android.widget.TextView[@text='行情']").click();
Thread.sleep(300);
driver.findElementByXPath("//android.widget.TextView[@text='口碑']").click();
Thread.sleep(300);
driver.findElementById("com.cubic.autohome:id/article_main_RadioButton").click();
Thread.sleep(2000);
driver.findElementById("com.cubic.autohome:id/club_main_RadioButton").click();
Thread.sleep(3000);
driver.findElementById("com.cubic.autohome:id/car_main_RadioButton").click();
Thread.sleep(4000);
driver.findElementByClassName("android.view.View").click();
Thread.sleep(6000);
driver.findElementById("com.cubic.autohome:id/find_main_RadioButton").click();
Thread.sleep(7000);
driver.findElementById("com.cubic.autohome:id/owner_main_RadioButton").click();
Thread.sleep(4000);
driver.findElementById("com.autohome.main.me:id/owner_user_message").click();
Thread.sleep(4000);
driver.findElementById("com.autohome.main.me:id/comment_close_btn").click();
Thread.sleep(5000);
driver.findElementById("com.cubic.autohome:id/article_main_RadioButton").click();
Thread.sleep(5000);
driver.findElementById("com.autohome.main.article:id/search_box").click();
Thread.sleep(3000);
driver.findElementById("com.autohome.plugin.search:id/fragment_search_keyword_autocompleteview").sendKeys("特斯拉");
Thread.sleep(5000);
//driver.findElementByXPath("//android.widget.TextView[@text=特斯拉中国]").click();
driver.findElementByClassName("android.widget.RelativeLayout").click();
Thread.sleep(2000);
}
private void swipeRight2Left(AppiumDriver driver) {
// TODO Auto-generated method stub
int startx=765;
int starty=244;
int endx=45;
int endy=244;
driver.swipe(startx, starty, endx, endy, 500);
}
/**
* AppiumDriver的初始化逻辑必须写在该函数中
* @return
*/
public AppiumDriver initAppiumTest() {
AppiumDriver driver=null;
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "apk");
File app = new File(appDir, "qichezhijia.apk");
//设置自动化相关参数
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "Android Emulator");
capabilities.setCapability("appPackage", "com.cubic.autohome");
capabilities.setCapability("appActivity", "com.cubic.autohome.LogoActivity");
capabilities.setCapability("noSign", "true");
//设置apk路径
capabilities.setCapability("app", app.getAbsolutePath());
//设置使用unicode键盘,支持输入中文和特殊字符
capabilities.setCapability("unicodeKeyboard","true");
//设置用例执行完成后重置键盘
capabilities.setCapability("resetKeyboard","true");
//初始化
try {
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return driver;
}
public void start() throws InterruptedException {
test(initAppiumTest());
}
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
main.start();
}
}