This web automated testing framework is really good, selenium advanced pro plus version

In web automated testing, the selenium architecture should be difficult to bypass. Many next-generation web automated testing frameworks that claim to surpass selenium ultimately fail. However, selenium's API is indeed relatively complex, so there are many libraries trying to encapsulate it at the upper level, and splinter is the best developed one among them. In this article, we introduce a simple introduction to splinter. If you think selenium is easy enough to use, you can also refer to the design of splinter and re-encapsulate selenium.

Quick to use

# 导入浏览器
from splinter import Browser
# 开启 chrome 浏览器
browser = Browser("chrome")
# 访问网址
browser.visit('http://www.baidu.com')

The browser will automatically open:

Install

  • pip install splinter
  • Install the browser driver. The driver and browser model version must correspond.

Driver download steps:

  • Open the download address: https://npm.taobao.org/mirrors/chromedriver
  • Select driver version. For example, if you are using Chrome browser v78, you can download the 78 version driver accordingly.
  • Unzip the chromedriver.exe file and put it in the environment variable (such as the python root directory).

Browser options

from splinter import Browser
from selenium import webdriver
# 添加浏览器选项
options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')
options.add_argument('--disable-notifications')
# 无头浏览器选项,开启之后,不会再有界面出现
browser = Browser('chrome', options=options, headless=True)
browser.visit("http://www.baidu.com")

Element search

The method of element search basically follows selenium:

  • id
  • name
  • css selector
  • xpath
# 通过id查找
input_element = browser.find_by_id('kw')
# 通过css选择器查找
browser.find_by_css('h1')
# 通过id查找
browser.find_by_xpath('//h1')
browser.find_by_tag('h1')
browser.find_by_name('name')
browser.find_by_text('Hello World!')
browser.find_by_id('firstheader')

But some more commonly used methods have also been added:

  • text text
  • href attribute
  • value attribute
browser.find_by_text()
browser.find_link_by_href()
browser.find_by_value('query')

There are many search methods, but in fact they are of little use. Instead, they cause confusion in the framework code. It is better to just keep xpath and css.

In fact, there is a more streamlined __call__ method to implement element search. I will talk about it when I have time, so I won’t go into details here.

wait

Compared to selenium's waiting, splinter's friendliness is much better. You can set a global waiting time when initializing the browser, and all subsequent element searches will be based on this timeout:

browser = Browser('chrome', wait_time=4)

Judgment and matching:

Those who have used selenium may be very troubled by the way of using display waiting. It is really difficult to use. Splinter's matcher mechanism is similar to display waiting. Of course, it is better to use:

# 当 h1 元素出现
#返回 True or False
browser.is_element_present_by_css('h1', wait_time=6) 

Although it is easier to use, this method still has the same problem as element search: the API is too complicated, and I feel dizzy when I look at the source code. Friends, can you think of a way to streamline it? :

input element input

browser.visit("http://www.baidu.com")
# 通过 name 直接输入
browser.fill('wd', )

Window management and switching

Window management and switching are the most worthwhile aspects of using Splinter, because Selenium's way of managing windows is very primitive, and it is almost impossible to use it normally without encapsulating it yourself.

splinter uses a specialized Window class to manage windows:

# 列举所有窗口,得到 Windows 对象
browser.windows 
# 通过索引获取某个 Window 对象
browser.windows[0]
# 通过窗口名称获取某个 Window 对象
browser.windows[window_name] 
# 获取当前窗口
browser.windows.current
# 设置当前窗口
browser.windows.current = browser.windows[3]

# 判断某一个窗口是否是当前窗口
window.is_current 
# 打开下一个窗口
window.next   
# 打开上一个窗口
window.prev 

Splinter's implementation of window management is very interesting. Students who know python magic methods and descriptors can take a look at the source code. iframe switching uses a context manager. You only need to do this to complete iframe switching, and it will automatically exit after each execution:

with get_iframe('iframe_name') as frame:
	frame.find_by_id('id_value')

Summarize

  • Splinter is easier to use than selenium, but since it is just a shell outside selenium, the entire architecture and process have not changed;
  • Splinter's API management is quite complex, so for some rarely used methods, just leave them alone;
  • splinter does a good job of encapsulating some complex operations of the browser: windows management iframe switching select selector processing mouse operations, especially form and input input is more streamlined
  • Lack of handling of file uploads
  • Element search and management still need to be optimized


 

Guess you like

Origin blog.csdn.net/a448335587/article/details/132605887