Skip to content

强制等待

from time import sleep
sleep(5)

隐性等待

driver.implicitly_wait(10)

我们等待元素已经加载出来了,但页面还没有加载完,需要继续等待页面加载完才能执行下一步操作。

显性等待

python
from  selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.common.by import  By
wait = WebDriverWait(driver, 10, 0.5)wait.until(EC.presence_of_element_located((By.ID, 'KW')))
loc = ("xpath", "//*[@text='允许']")
e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))e.click()

示例代码

python
from time import  sleep
from ddt import ddt,file_data
from appium import  webdriver
from common.pyyaml import  getyaml
from common.parame import Parmer
from config import apk_path,login_element
from  common.pertools import caijicpu,getnencun
from  selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import  By
element=getyaml(login_element)
from  common.isintsallapp import isinstallapp
@ddt
class testCase(Parmer):
    def setUp(self) -> None:
        is_first_install=isinstallapp(devicesName=self.parme['deviceName'],apk_path=apk_path,
                                      apppackage=self.parme['appPackage'])
        desired_caps=self.parme['desired_caps']
        self.driver = webdriver.Remote('http://0.0.0.0:%s/wd/hub'%str(self.parme['port']), desired_caps)
        self.driver.implicitly_wait(10)
        # 启动同意用户协议
        agree=element['agreeelement']
        self.driver.find_element_by_id(agree).click()
        if is_first_install:
            # 首次安装需要加载文件
            sleep(50)
        sleep(50)
        self.driver.find_element_by_id(element['opearelement']).click()
        sleep(10)
        WebDriverWait(self.driver, 10, 0.5).until(
            EC.presence_of_element_located((By.ID,element['logintextclik']))).click()
    def tearDown(self) -> None:
        self.driver.quit()
        sleep(20)
    @file_data("../data/casedata/logincase.yaml")
    def testlogin(self,user,user_password,msg,casename):
        cpu=caijicpu(packagename=self.parme['appPackage'],devices=self.parme['deviceName'])
        memory=getnencun(packagename=self.parme['appPackage'],devices=self.parme['deviceName'])
        print("用例:%s 执行前:cpu:%s 内存:%s"%(casename,cpu,memory))
        sleep(10)
        loc = ("id", element['usernameedit'])
        username = WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(loc))
        username.clear()
        username.send_keys(user)
        password=WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.ID,element['usernameedit'])))
        # password = self.driver.find_element_by_id(element['passwordedit'])
        password.clear()
        password.send_keys(user_password)
        sleep(2)
        login = self.driver.find_element_by_id(element['loginbtn'])
        login.click()
        sleep(10)
        try:
            self.driver.find_element_by_id(element['assertelement'])
            self.assertTrue(False,msg=msg)
            cpuend = caijicpu(packagename=self.parme['appPackage'], devices=self.parme['deviceName'])
            memoryend = getnencun(packagename=self.parme['appPackage'], devices=self.parme['deviceName'])
            changecpu = float(cpuend) - float(cpu)
            memorychange = float(memoryend) - float(memory)
            print("用例:%s 执行过程变化:cpu:%s M 内存:%s M" % (casename, changecpu, memorychange))

        except:
            self.assertTrue(True,msg=msg)
            cpuend = caijicpu(packagename=self.parme['appPackage'], devices=self.parme['deviceName'])
            memoryend = getnencun(packagename=self.parme['appPackage'], devices=self.parme['deviceName'])
            changecpu=float(cpuend)-float(cpu)
            memorychange=float(memoryend)-float(memory)
            print("用例:%s 执行过程变化:cpu:%s M 内存:%s M" % (casename, changecpu