代码示例:
from time import sleep
import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.extensions.android.gsm import GsmCallActions
from selenium.webdriver.common.by import By
class TestHybrid:
def setup(self):
# 路径中test为用户名
desired_caps = {
"platformName": "android",
"appium:deviceName": "emulator-5554",
"browserName": "chrome",
"chromedriverExecutableDir": "/Users/test/tools/chromedriverdir",
"chromedriverChromeMappingFile": "/Users/test/PycharmProjects/test_appium/testcase/mapping.json"
}
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置隐式等待
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_devices(self):
self.driver.get("http://m.baidu.com")
self.driver.make_gsm_call('13020997158', GsmCallActions.CALL)
self.driver.send_sms('13120993123', 'hello appium test message')
if __name__ == 'main':
pytest.main()
网络状态切换:driver.set_network_connection
参数:0表示网络断开,1表示飞行模式,2表示仅wifi,4表示仅数据连接,6表示wifi和数据都开启
代码示例:
self.driver.set_network_connection(1)
sleep(3)
self.driver.set_network_connection(4)
sleep(3)
截图方法:get_screenshot_as_file
参数中传入文件的路径及文件名即可,示例如下:
self.driver.get_screenshot_as_file('./screenshot/img.png')
完整代码如下:
from time import sleep
import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.extensions.android.gsm import GsmCallActions
from selenium.webdriver.common.by import By
class TestHybrid:
def setup(self):
desired_caps = {
"platformName": "android",
"appium:deviceName": "emulator-5554",
"browserName": "chrome",
"chromedriverExecutableDir": "/Users/gaozeyu/tools/chromedriverdir",
"chromedriverChromeMappingFile": "/Users/gaozeyu/PycharmProjects/test_appium/testcase/mapping.json"
}
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
# 设置隐式等待
self.driver.implicitly_wait(10)
def teardown(self):
self.driver.quit()
def test_devices(self):
self.driver.get("http://m.baidu.com")
self.driver.make_gsm_call('13020997158', GsmCallActions.CALL)
self.driver.send_sms('13120993123', 'hello appium test message')
self.driver.get_screenshot_as_file('./screenshot/img.png')
self.driver.set_network_connection(1)
sleep(3)
self.driver.set_network_connection(4)
sleep(3)
if __name__ == 'main':
pytest.main()
mac上面使用emulator直接启动创建的模拟器
appium脚本中启动参数disired_caps中添加模拟器
avd": "Pixel_5_API_30"
注:该启动项仅适用于emulator创建的模拟器,其他第三方创建的模拟器不使用。