断言/try异常--163

'''
 断言:
     就是判断预期结果和实际结果是否一致
     现在可以使用if语句判断
     在后面学assertEqual()   等方法实现断言
     在使用断言的时候,注意异常的处理,如果不处理,可能会导致pvm退出,后续的脚本或者代码不能执行,
     只有使用try--except--finally这种语法处理,保证后续代码或脚本的正常运行
'''
from selenium import webdriver
import time

from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
driver.get('https://mail.163.com/register/index.htm?from=163mail&utm_source=163mail')
# 注册用户名
driver.find_element_by_xpath('//*[@id="username"]').send_keys("adsadassd")
driver.find_element_by_xpath('//*[@id="password"]').send_keys("15127918912W")
driver.find_element_by_xpath('//*[@id="phone"]').send_keys("15127918912")
driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[4]/span').click()
driver.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div[2]/div[5]/a[1]').click()

# 先设置一下延迟时间
time.sleep(2)
# 需要设置断言

exUrl = "adsadassd"
#抛出异常,元素定位错误,则报出:抛出异常,未找到元素
try:
    actUrl = driver.find_element_by_xpath('//*[@id="username"]').text
    #判断的条件是:提示信息是否符合要求
    if exUrl == actUrl:
         print("注册成功,正向用例成功")
    else:
        print("注册失败,注册用例不通过")
except NoSuchElementException as e:
        print("抛出异常,未找到元素",e)

# 设置进入下一步时间
time.sleep(2)
# 退出
driver.quit()

  

posted on 2021-03-05 17:16  爱前端的小魏  阅读(82)  评论(0编辑  收藏  举报

导航