1. 程式人生 > >自動化測試工具appium安裝&操作微信與好友聊天

自動化測試工具appium安裝&操作微信與好友聊天

package test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities; /** * Created by author on 2018/1/9. */ public class CalculaterTest2 { private AppiumDriver driver; @Before public void setUp() throws Exception { DesiredCapabilities cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.BROWSER_NAME
, ""); cap.setCapability("platformName", "Android"); //指定測試平臺 cap.setCapability("deviceName", "D8YDU16616000842"); //指定測試機的ID,通過adb命令`adb devices`獲取 cap.setCapability("platformVersion", "5.1"); //將上面獲取到的包名和Activity名設定為值 cap.setCapability("appPackage", "com.tencent.mm"); cap.setCapability("appActivity", "com.tencent.mm.ui.LauncherUI"
); //A new session could not be created的解決方法 cap.setCapability("appWaitActivity", "com.tencent.mm.ui.LauncherUI"); cap.setCapability("unicodeKeyboard", "True"); cap.setCapability("resetKeyboard", "True"); //每次啟動時覆蓋session,否則第二次後執行會報錯不能新建session cap.setCapability("sessionOverride", true); driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap); } @After public void tearDown() throws Exception { Thread.sleep(5000); driver.quit(); } @Test public void apiDemo() throws InterruptedException, IOException { Thread.sleep(5000); List<WebElement> list = driver.findElementsById("com.tencent.mm:id/apt"); int width = driver.manage().window().getSize().width; int height = driver.manage().window().getSize().height; for (; ; ) { WebElement target = getTarget(list); if (target != null) { target.click(); Thread.sleep(500); driver.findElementById("com.tencent.mm:id/aae").click(); driver.findElementById("com.tencent.mm:id/aaf").sendKeys("11"); Thread.sleep(100); driver.findElementById("com.tencent.mm:id/aal").click(); break; } else { driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, 800); Thread.sleep(100); list = driver.findElementsById("com.tencent.mm:id/apt"); } } } WebElement getTarget(List<WebElement> list) throws InterruptedException { for (WebElement w : list) { if (w.getText().contains("雙雙")) { return w; } } return null; } }