元素等待
设置元素等待可以更加灵活的指定等待元素的时间,从而增强脚本的健壮性,提高执行效率
强制等待
from time import sleep sleep(5)
隐式等待
隐式等待是针对全部元素设置的等待时间
driver.implicitly_wait(30)
显示等待
显示等待是针对某个元素来设置的等待时间
from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(driver,30).until(lambda x:x.find_element_by_id('com.jgw.csca:id/et_pwd'))
屏幕截图
方法一
save_screenshot()该方法直接保存当前屏幕截图到当前脚本所在文件位置
driver.save_screenshot('login.png')
方法二
get_screenshot_as_file(self,filename)
将截图保存到指定文件路径
driver.get_screenshot_as_file('../images/login.png')
实战
import pytest from appium import webdriver class Testcsca(): def setup(self): caps = {} caps["platformName"] = "Android" # caps["deviceName"] = "127.0.0.1:62001" caps["deviceName"] = "CLB0219314000452" caps["appPackage"] = "com.jgw.csca" caps["appActivity"] = "com.jgw.csca.view.activity.LoginActivity" caps["platfromVersion"] = "9.0.0" caps["autoGrantPermissions"] = True # 设置自动授权权限 caps['unicodeKeyboard'] = True # 输入中文时要加,要不然输入不了中文 caps['resetKeyboard'] = True # 输入中文时要加,要不然输入不了中文 self.driver = webdriver.Remote("http://localhost:4723/wd/hub", caps) self.driver.implicitly_wait(20) def test_login(self): self.driver.find_element_by_android_uiautomator('new UiSelector().text("请输入用户名")').send_keys('666') self.driver.save_screenshot('login.png') # 保存到当前脚本位置 self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.jgw.csca:id/et_pwd")').send_keys('balabala') self.driver.get_screenshot_as_file('E:/login.png') # 保存到指定位置 self.driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.Button")').click()