Assertions in Selenium (python articles)


Selenium commonly used assertions include
  • Page attribute assertion: Assert whether the title, url or page source code contains or does not contain specific characters
  • Element existence assertion: assert that the specified element exists
  • Picture and link assertion: assert that the picture is displayed normally and the link can be opened normally

Page attribute assertion

This is the most commonly used assertion method, which can be used to assert whether the page is opened normally and whether it is on the specified window and page. Examples are as follows.

from selenium import webdriver

dr = webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.find_element('link text', '新闻').click()
dr.switch_to.window(dr.window_handles[-1])  # 切换到最后一个窗口

assert '百度新闻' in dr.title    # 断言标题包含
assert 'http://news.baidu.com/' == dr.current_url   # 断言为指定url
assert 'not found' not in dr.page_source  # 断言页面源码不包含not found(一般Nginx找不到页面时返回404页面,显示Not Found)

dr.quit()

Page element assertion

Sometimes we need to determine whether a specified element exists on the page to continue the operation, or assert whether it is on the specified page by whether the element exists.
Assert whether the element exists, you can use try...except to catch and suppress the exception that the element cannot be located, or use find_elements+ to determine whether the returned element list is empty to determine whether the element can be located.
For elements that need to wait for a certain period of time to appear, we can use smart waiting or active waiting to poll and check the elements until they time out. Examples are as follows.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait, TimeoutException

dr = webdriver.Chrome()
dr.get('https://www.baidu.com')
dr.maximize_window()


try:
    settings = dr.find_element('id', 's-usersetting-top')
except NoSuchElementException:
    print('未定位到"设置"')
    # raise AssertionError('设置链接不存在')  # 测试时,需抛出标准的断言异常, 对于偶现元素,则无需抛出异常
else:
    print('"设置"存在')
    ActionChains(dr).move_to_element(settings).perform()  # 鼠标移动到设置

# 或 使用 find_elements + 列表长度判断
elms = dr.find_elements('id', 's-usersetting-top')
if len(elms) > 0:
    settings = elms[0]
    print('"设置"存在')
    ActionChains(dr).move_to_element(settings).perform()
else:
    print('未定位到"设置"')
    raise AssertionError('设置链接不存在')


# 对需要等待的元素,使用主动等待轮询
try:
    WebDriverWait(dr, 10, 0.5).until(
        lambda dr: dr.find_element('link text', '高级搜索')
    ).click()
except TimeoutException:
    print('未定位到"高级搜索"')
    # raise AssertionError('高级搜索不存在')

dr.quit()

Picture and link assertion

Whether the picture is displayed and whether the link can be opened normally, we can use dr.get('picture or link address') to see whether the 404 page is returned to determine whether the picture can be opened normally, an example is as follows.

from selenium import webdriver

dr = webdriver.Chrome()
dr.get('https://www.baidu.com')


baidu_logo_url = dr.find_element('id', 's_lg_img').get_attribute('src')
print('百度Logo图片链接', baidu_logo_url)

dr.get(baidu_logo_url)  # 尝试打开图片
assert 'Not Found' not in dr.page_source  # 假设不存在报错页面包含Not Found字样
dr.back()

hao123_link_url = dr.find_element('link text', 'hao123').get_attribute('href')
print('hao123链接', hao123_link_url)
dr.get(hao123_link_url)  # 尝试打开页面
assert 'Not Found' not in dr.page_source
dr.back()

dr.quit()

Images and links can be checked faster using interface requests, or use a special 404 checking tool.

other

  • Color assertion: Selenium support
  • Style arrangement assertion: For example, whether the title is line-breaking, button overlap, misalignment, etc., can be realized by saving a screenshot of the page (or part), through a series of algorithms such as python-opencv image comparison.
  • Assert whether the picture specifies the picture: through picture comparison, some image algorithms are involved

If the article is helpful to you, please reach out to make a fortune and give me a like. Thank you for your support. Your likes are my motivation for continuous updating.


Finally: benefits

In the technology industry, you must improve your technical skills and enrich your practical experience in automation projects. This will be very helpful for your career planning in the next few years and the depth of your testing technology.

In the interview season of Golden 9th and Silver 10th, job-hopping season, organizing interview questions has become my habit for many years! The following is my collection and sorting in recent years, the whole is organized around [software testing], the main content includes: python automation test exclusive video, Python automation details, a full set of interview questions and other knowledge content.

For software testing friends, it should be the most comprehensive and complete interview preparation warehouse. In order to better organize each module, I also refer to many high-quality blog posts and projects on the Internet, and strive not to miss every knowledge point. Friends relied on these contents to review, and got offers from big factories such as BATJ. This warehouse has also helped many software test learners, and I hope it can help you too!

May you and I meet and you will find something! Welcome to follow the WeChat public account: [Sad Spicy Article] Receive a 216-page software test engineer interview book for free. And the corresponding video learning tutorials are free to share!

Guess you like

Origin blog.csdn.net/weixin_50271247/article/details/112909038