UI自动化测试案例——简单的Google搜索测试

以下是一个UI自动化测试的经典案例:

import unittest
from selenium import webdriver

class GoogleSearchTest(unittest.TestCase):
    def setUp(self):
        # 创建Chrome浏览器实例
        self.driver = webdriver.Chrome()
        self.driver.maximize_window() # 最大化浏览器窗口

    def tearDown(self):
        self.driver.quit() # 关闭浏览器实例

    def test_google_search(self):
        self.driver.get("https://www.google.com") # 打开Google首页
        self.assertEqual(self.driver.title, "Google") # 校验页面标题是否为"Google"

        search_box = self.driver.find_element_by_name("q") # 找到搜索框元素
        search_box.send_keys("Selenium WebDriver") # 在搜索框中输入"Selenium WebDriver"
        search_box.submit() # 提交搜索请求

        search_results = self.driver.find_elements_by_css_selector("div.g") # 找到搜索结果元素列表
        self.assertGreater(len(search_results), 0) # 校验搜索结果列表是否不为空

        first_result = search_results[0] # 取出第一个搜索结果元素
        first_result_link = first_result.find_element_by_tag_name("a") # 找到第一个搜索结果元素中的链接元素
        first_result_link.click() # 点击链接

        self.assertIn("Selenium - Web Browser Automation", self.driver.title) # 校验新页面标题是否包含"Selenium - Web Browser Automation"

具体的测试步骤如下:

  1. 打开Google首页
  2. 校验页面标题是否为"Google"
  3. 在搜索框中输入"Selenium WebDriver"
  4. 提交搜索请求
  5. 校验搜索结果列表是否不为空
  6. 取出第一个搜索结果元素
  7. 找到第一个搜索结果元素中的链接元素
  8. 点击链接
  9. 校验新页面标题是否包含"Selenium - Web Browser Automation"

这个测试案例使用了Python的unittest测试框架和Selenium WebDriver库。在测试开始前,会在setUp()方法中创建Chrome浏览器实例,并将浏览器窗口最大化。在测试结束后,会在tearDown()方法中关闭浏览器实例。

测试方法test_google_search()中,首先打开Google首页,然后校验页面标题是否为"Google"。接着,在搜索框中输入"Selenium WebDriver",并提交搜索请求。然后,通过CSS选择器找到搜索结果元素列表,并校验列表是否不为空。接着,取出第一个搜索结果元素,并找到其中的链接元素,点击链接。最后,校验新页面标题是否包含"Selenium - Web Browser Automation"。

在测试过程中,如果有任何一个断言失败,测试框架会抛出AssertionError异常,测试将会失败。如果所有断言都通过,测试将会通过。

接下来我们来分析一下这个测试案例的代码。

首先,在setUp()方法中,我们创建了一个Chrome浏览器实例,并将浏览器窗口最大化:

def setUp(self):
    # 创建Chrome浏览器实例
    self.driver = webdriver.Chrome()
    self.driver.maximize_window() # 最大化浏览器窗口

这里我们使用了Selenium WebDriver库中的webdriver.Chrome()方法来创建Chrome浏览器实例。然后,通过调用maximize_window()方法,将浏览器窗口最大化。

在tearDown()方法中,我们关闭了浏览器实例:

def tearDown(self):
    self.driver.quit() # 关闭浏览器实例

这里我们使用了Selenium WebDriver库中的quit()方法来关闭浏览器实例。

在test_google_search()方法中,我们执行了一个完整的Google搜索的UI自动化测试:

def test_google_search(self):
    self.driver.get("https://www.google.com") # 打开Google首页
    self.assertEqual(self.driver.title, "Google") # 校验页面标题是否为"Google"

    search_box = self.driver.find_element_by_name("q") # 找到搜索框元素
    search_box.send_keys("Selenium WebDriver") # 在搜索框中输入"Selenium WebDriver"
    search_box.submit() # 提交搜索请求

    search_results = self.driver.find_elements_by_css_selector("div.g") # 找到搜索结果元素列表
    self.assertGreater(len(search_results), 0) # 校验搜索结果列表是否不为空

    first_result = search_results[0] # 取出第一个搜索结果元素
    first_result_link = first_result.find_element_by_tag_name("a") # 找到第一个搜索结果元素中的链接元素
    first_result_link.click() # 点击链接

    self.assertIn("Selenium - Web Browser Automation", self.driver.title) # 校验新页面标题是否包含"Selenium - Web Browser Automation"

首先,我们打开了Google首页,并校验了页面标题是否为"Google":

self.driver.get("https://www.google.com") # 打开Google首页
self.assertEqual(self.driver.title, "Google") # 校验页面标题是否为"Google"

这里我们使用了Selenium WebDriver库中的get()方法来打开Google首页,然后使用unittest框架中的assertEqual()方法来校验页面标题是否为"Google"。

接下来,我们找到了搜索框元素,并在搜索框中输入了"Selenium WebDriver":

search_box = self.driver.find_element_by_name("q") # 找到搜索框元素
search_box.send_keys("Selenium WebDriver") # 在搜索框中输入"Selenium WebDriver"
search_box.submit() # 提交搜索请求

这里我们使用了Selenium WebDriver库中的find_element_by_name()方法来找到搜索框元素,然后使用send_keys()方法来在搜索框中输入关键字,最后使用submit()方法来提交搜索请求。

然后,我们找到了搜索结果元素列表,并校验了列表是否不为空:

search_results = self.driver.find_elements_by_css_selector("div.g") # 找到搜索结果元素列表
self.assertGreater(len(search_results), 0) # 校验搜索结果列表是否不为空

这里我们使用了Selenium WebDriver库中的find_elements_by_css_selector()方法来找到搜索结果元素列表,然后使用unittest框架中的assertGreater()方法来校验列表长度是否大于0。

接着,我们取出了第一个搜索结果元素,并找到了其中的链接元素,并点击了链接:

first_result = search_results[0] # 取出第一个搜索结果元素
first_result_link = first_result.find_element_by_tag_name("a") # 找到第一个搜索结果元素中的链接元素
first_result_link.click() # 点击链接

这里我们使用了Python的列表索引和Selenium WebDriver库中的find_element_by_tag_name()方法来取出第一个搜索结果元素,并找到其中的链接元素。然后,使用click()方法来点击链接。

最后,我们校验了新页面标题是否包含"Selenium - Web Browser Automation":

self.assertIn("Selenium - Web Browser Automation", self.driver.title) # 校验新页面标题是否包含"Selenium - Web Browser Automation"

这里我们使用了unittest框架中的assertIn()方法来校验页面标题是否包含指定的文本。

总的来说,这个测试案例很简单,但是覆盖了UI自动化测试的基本流程和常用操作,适合初学者练习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜗牛_Chenpangzi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值