Appium录制脚本520-2

时间:2023-03-09 09:36:55
Appium录制脚本520-2

1.录制自动化脚本

场景:启动雪球,点击我的,登陆雪球,选择手机及其他登陆,输入手机号

2.使用Java进行测试Appium测试

2.1创建Java工程

file-创建maven工程-填写GroupId(团队名)&ArtifactId(工程名)-finish

2.2Java安装Appium客户端

maven中安装Java客户端,只需要在pom.xml中加入java-client依赖即可

Appium录制脚本520-2

2.3添加测试代码

test-java-目录下创建XueqiuDemo.java文件,并将录制生成的Java代码复制进去。
代码发现如下错误,需要导入junit依赖,去maven搜索junit依赖,添加到pom.xml。

Appium录制脚本520-2
pom.xml代码
```#xml

4.0.0

<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>

```
/test/java/XueqiuDemo.java代码
```#java
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

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。

Appium录制脚本520-2