Appium移动端自动化测试--录制测试用例并运行
文章目录如下
录制用例并执行
- 使用Appium desktop录制用例
- 安装Python依赖pip install Appium-Python-Client
- 增加隐式等待增强稳定性
- 重新运行
1、使用Appium desktop录制用例
autoGrantPermissions=true #不再跳出定位信息
选择右下方的Send Keys, 跳出输入框,输入搜索内容并点击Send Keys按钮。
之后可以看到录制的脚本内容以及可以选择语言类型,如下图所示:
1、代码片段
2、选择脚本语言
3、显示全部的脚本语言——Python
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
from appium import webdriver
caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "pzhang7"
caps["automationName"] = "uiautomator2"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["autoGrantPermissions"] = "true"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
el1 = driver.find_element_by_id("com.xueqiu.android:id/home_search")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el2.send_keys("alibaba")
driver.quit()
4、复制脚本
5、删除脚本
(3)、显示全部的脚本语言——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 org.openqa.selenium.remote.DesiredCapabilities;
public class SampleTest {
private AndroidDriver driver;
@Before
public void setUp() throws MalformedURLException {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", "android");
desiredCapabilities.setCapability("deviceName", "pzhang7");
desiredCapabilities.setCapability("automationName", "uiautomator2");
desiredCapabilities.setCapability("appPackage", "com.xueqiu.android");
desiredCapabilities.setCapability("appActivity", ".view.WelcomeActivityAlias");
desiredCapabilities.setCapability("autoGrantPermissions", "true");
URL remoteUrl = new URL("http://127.0.0.1:4723/wd/hub");
driver = new AndroidDriver(remoteUrl, desiredCapabilities);
}
@Test
public void sampleTest() {
MobileElement el1 = (MobileElement) driver.findElementById("com.xueqiu.android:id/home_search");
el1.click();
MobileElement el2 = (MobileElement) driver.findElementById("com.xueqiu.android:id/search_input_text");
el2.sendKeys("alibaba");
}
@After
public void tearDown() {
driver.quit();
}
}
2、安装Pythony依赖Appium-Python-Client
将上述的Python脚本保存到本地任意路径下,在Python脚本最上面3行可看到描述说要安装Appium-Python-Client之后才能正常运行脚本
pip install Appium-Python-Client
退出Appium的录制界面;
然后切换到cmd界面,并执行Python脚本,如下图,出现异常信息,其实就是没有找到元素
3、增加隐式等待增强稳定性
上述代码增加隐式等待之后重新运行即可
driver.implicitly_wait(10) #添加隐式等待10秒
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
from appium import webdriver
caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "pzhang7"
caps["automationName"] = "uiautomator2"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["autoGrantPermissions"] = "true"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
driver.implicitly_wait(10) #添加隐式等待10秒
el1 = driver.find_element_by_id("com.xueqiu.android:id/home_search")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/search_input_text")
el2.send_keys("alibaba")
driver.quit()
4、重新运行
重新运行上面的测试用例,在等待10秒之后就可以正常运行了。
***********若有错误欢迎指正
欢迎关注微信公众号【软测小生】,号内有大量免费软件测试学习资料,欢迎自取,还望多多交流软件测试相关的内容*********