selenium自动化测试之测试结果验证

1.实际测试过程中,常常要对比实际结果与期望结果是否一致。
2.如果实际结果与期望结果不一致则被认为bug

selenium广泛应用于B/S架构,如何通过selenium来验证测试结果的正确性呢。

  • 案例分析:以百度为例,一起来看看如何验证测试结果的正确性。
    • 点击百度首页的“hao123”后;
    • 跳转至"hao123"页面
    • 验证:是否跳转至“hao123”页面
  • 如何通过selenium实现该场景呢?
#******************
# 获取验证信息,操作某页面后,确认进入的是期望结果
#******************

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

print("===========The baidu Page===============")
First_Title = driver.title
print("The first page title is:%s" % First_Title)

print("===========The hao123 Page===============")
driver.find_element_by_xpath("//a[@name='tj_trhao123']").click()
Second_Title = driver.title
print("The Second page title is:%s" % Second_Title)

Expect_Title = "hao123_上网从这里开始"
if Second_Title == Expect_Title:
    print(True)
else:
    print(False)
driver.quit()
  • selenium通过driver.title检查页面的title;
  • 判断实际页面的title值是否与期望值相同;
  • 从而得出测试结果。

如果你想系统学习selenium,可以通过如途径哦.....

推广下我的博客专栏,目前选定了一个主题《从零学Selenium自动化测试框架》请添加链接描述,让我们从代码撸起,一步步实现Web自动化测试框架

该专题会从零带你搭建一个可用的自动化测试框架(基于python+selenium)

前提:你要掌握了python与selenium基础哦。要不你看不懂的。

猜你喜欢

转载自blog.51cto.com/starpoint/2298722
今日推荐