1.錄制自動化腳本
場景:啟動雪球,點擊我的,登陸雪球,選擇手機及其他登陸,輸入手機號
2.使用Java進行測試Appium測試
2.1創建Java工程
file-創建maven工程-填寫GroupId(團隊名)&ArtifactId(工程名)-finish
2.2Java安裝Appium客戶端
maven中安裝Java客戶端,只需要在pom.xml中加入java-client依賴即可
2.3添加測試代碼
test-java-目錄下創建XueqiuDemo.java文件,並將錄制生成的Java代碼復制進去。
代碼發現如下錯誤,需要導入junit依賴,去maven搜索junit依賴,添加到pom.xml。
pom.xml代碼 ```#xml
<groupId>csj</groupId>
<artifactId>Study520</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
import org.openqa.selenium.remote.DesiredCapabilities;
public class XueqiuDemo {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "android");
desiredCapabilities.setCapability("deviceName", "domo");
desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");
desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");
URL remoteUrl = new URL("http://localhost:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
}
@Test
public void sampleTest() {
MobileElement el1 = (MobileElement) driver.findElementById("com.xueqiu.android:id/user_profile_icon");
el1.click();
MobileElement el2 = (MobileElement) driver.findElementById("com.xueqiu.android:id/tv_login");
el2.click();
MobileElement el3 = (MobileElement) driver.findElementById("com.xueqiu.android:id/tv_login_by_phone_or_others");
el3.click();
MobileElement el4 = (MobileElement) driver.findElementById("com.xueqiu.android:id/register_phone_number");
el4.sendKeys("123456789");
}
@After
public void tearDown() {
driver.quit();
}
}
# FAQ
## 1.元素找不到
網絡不好,或信號比較差,往往頁面加載時間比較長,此時需要添加隱式等待。
初次執行用例或網絡比較差,提示找不到元素。是因為頁面沒有加載完,就開始尋找元素。
<img src="https://img2018.cnblogs.com/blog/1418970/201810/1418970-20181002151514310-1418133742.png" width="700" />
此時只需要添加隱私等待即可
```#java
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
2.獲取maven依賴
以java-client為例:maven依賴地址:https://mvnrepository.com/,查找java-client,並將代碼復制到pom.xml。

