玩转UI自动化测试2

1.关于webdriver的init函数

  不同的浏览器,其webdriver的init函数不一样,如chrome浏览器的init函数如下:

  def __init__(self, executable_path="chromedriver", port=0,
options=None, service_args=None,
desired_capabilities=None, service_log_path=None,
chrome_options=None, keep_alive=True):
  """
  Creates a new instance of the chrome driver.
  Starts the service and then creates new instance of chrome driver.
  :Args:
  - executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
  - port - port you would like the service to run, if left as 0, a free port will be found.
  - options - this takes an instance of ChromeOptions
  - service_args - List of args to pass to the driver service
  - desired_capabilities - Dictionary object with non-browser specific
  capabilities only, such as "proxy" or "loggingPref".
  - service_log_path - Where to log information from the driver.
  - chrome_options - Deprecated argument for options
  - keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
  """
 其中executable_path主要是指定webdriver的路径,如果webdriver在python目录下,可以不用指定
Firefox浏览器的init函数与chrome不一样,Firefox浏览器init函数如下:
  def __init__(self, firefox_profile=None, firefox_binary=None,
timeout=30, capabilities=None, proxy=None,
executable_path="geckodriver", options=None,
service_log_path="geckodriver.log", firefox_options=None,
service_args=None, desired_capabilities=None, log_path=None,
keep_alive=True):

"""Starts a new local session of Firefox.

Based on the combination and specificity of the various keyword
arguments, a capabilities dictionary will be constructed that
is passed to the remote end.

The keyword arguments given to this constructor are helpers to
more easily allow Firefox WebDriver sessions to be customised
with different options. They are mapped on to a capabilities
dictionary that is passed on to the remote end.

As some of the options, such as `firefox_profile` and
`options.profile` are mutually exclusive, precedence is
given from how specific the setting is. `capabilities` is the
least specific keyword argument, followed by `options`,
followed by `firefox_binary` and `firefox_profile`.

In practice this means that if `firefox_profile` and
`options.profile` are both set, the selected profile
instance will always come from the most specific variable.
In this case that would be `firefox_profile`. This will result in
`options.profile` to be ignored because it is considered
a less specific setting than the top-level `firefox_profile`
keyword argument. Similarily, if you had specified a
`capabilities["moz:firefoxOptions"]["profile"]` Base64 string,
this would rank below `options.profile`.

:param firefox_profile: Instance of ``FirefoxProfile`` object
or a string. If undefined, a fresh profile will be created
in a temporary location on the system.
:param firefox_binary: Instance of ``FirefoxBinary`` or full
path to the Firefox binary. If undefined, the system default
Firefox installation will be used.
:param timeout: Time to wait for Firefox to launch when using
the extension connection.
:param capabilities: Dictionary of desired capabilities.
:param proxy: The proxy settings to us when communicating with
Firefox via the extension connection.
:param executable_path: Full path to override which geckodriver
binary to use for Firefox 47.0.1 and greater, which
defaults to picking up the binary from the system path.
:param options: Instance of ``options.Options``.
:param service_log_path: Where to log information from the driver.
:param firefox_options: Deprecated argument for options
:param service_args: List of args to pass to the driver service
:param desired_capabilities: alias of capabilities. In future
versions of this library, this will replace 'capabilities'.
This will make the signature consistent with RemoteWebDriver.
:param log_path: Deprecated argument for service_log_path
:param keep_alive: Whether to configure remote_connection.RemoteConnection to use
HTTP keep-alive.
"""
  其中参数:
  firefox_profile,启动火狐浏览器配置文件,目的是自动化测试环境统一
  timeout 启动浏览器的超时时间
  executable_path webdriver路径
  firefox_binary 指定火狐浏览器安装的路径,非默认安装的需要设置

2.driver的11个常用方法

  四个导航方法 :

  1.driver.get("http://www.baidu.com")  get方法会等待页面静态资源加载完毕

  2.driver.back  相当于浏览器的回退操作,不常用

  3.driver.foward  与回退相反的操作,也不常用

  4.driver.refresh  浏览器刷新操作 

  三个get方法:

  1.driver.title  获取网页标题

  2.driver.current_url 获取当前url

  3.driver.page_source 获取当前页面的源代码

  两个退出:

  driver.close 关闭当前焦点所在的浏览器窗口

  drivre.quit  断开浏览器会话,关闭所有的启动的浏览器

  管理窗口的方法:

  driver.maximize_window  最大化浏览器窗口

  driver.get_window_size   获取当前窗口大小

  driver.set_window_size    设置窗口大小

  截图方法

  get_screenshot_as_file  get_screenshot_as_file(./baidu.png)  截图保存成图片,格式可以自定义

  get_screenshot_as_base64  保存的是base64的编码格式,在html界面输出,结合allure使用:

  image = driver.get_screenshot_as_base64()
  allure.attach(f'<img src="data:image/png;base64,{image}" />', '屏幕截图', allure.attachment_type.HTML)

  get_screenshot_as_png 保存的是二进制数据,很少用到

3.HTML常见的元素类型:

  html元素的结构通常是: 元素  对象   控件

  html 基础  <input id="a" name="b"> hello </input>

  input 标签名称 元素类型 tag

  hello  文本值

  属性  id (唯一),name(提交给服务器,名称,一般不重复)

  class 元素分类---样式   class = "aaa bbb"  style = "color:red"

  <1>.input 输入域 type

  <2>.button 按钮

  <3>.select 下拉列表,现在很少用了

  <4>.ul 无序列表

  <5>. a  超链接

  <6>. image 图片

  <7>. table 表格

  <8>. form表单

  <9>. div 块元素 负责页面布局用

  <10>. span/p 文本显示,段落用p, 短文本用span

  <11>. hidden input type="hidden"  隐藏域,自动化测试时可以去掉

4.元素取值:

  1.取text属性值:driver.find_element_by_name("info").text

  2.取某个链接的url:link = driver.find_element_by_class_name("baidu").get_attribute("href")

   跳转到该链接:  driver.get(link)

5.select 操作:
  from selenium.webdriver.support.select import Select
  1.使用select 的value方法
    element = driver.find_element_by_name("select")
    select = Select(element)
    time.sleep(2)
    select.select_by_value("4")
    time.sleep(2)
    select.select_by_value("2")
  
  2. 循环取,通过select的index, select.options取到所有的选项

    for i in range(0, len(select.options)):
      select.select_by_index(i)
      time.sleep(2)

5.selenium复杂操作:
  如果需要模拟比较复杂的操作,如 焦点移动 按住 拖拽 双击等操作,需要用到actions 
  首先先引入包: 
    from selenium.webdriver.common.action_chains import ActionChains
 
  <1>.模拟鼠标向下滚动:
    driver.execute_script("window.scrollTo(0,1000)")
 
  <2>.移动到某个焦点

    ac = ActionChains(driver)

    ac.move_to_element(driver.find_element_by_class_name("over"))

   <3>.双击操作
    ac.double_click(driver.find_element_by_class_name("double"))
 
  <4>.拖拽点住
    ac.click_and_hold(driver.find_element_by_xpath("//div[@id='slider']/span"))
   
     <5>.拖拽往右滑动

    ac.move_to_element(driver.find_element_by_id("slider_confirm"))

  <6>. 抬手

    ac.release()

  <7>.另外一种拖拽方法:

    drag_and_drop(source,target)  把source拖拽到target

    ac.drag_and_drop(driver.find_element_by_xpath("//div[@id='slider']/span"),

    driver.find_element_by_id("slider_confirm"))

  <8>.第三种拖拽方法,通过坐标来拖拽
    drag_and_drop_by_offset(source, xoffset, yoffset)

    ac.drag_and_drop_by_offset(driver.find_element_by_xpath("//div[@id='slider']/span"), 0, 1000)

  <9>.鼠标右键操作 

    ac.context_click()

    <10>. 焦点平移到空白处操作,有时候用的到,比如输入框输入后,在空白处点击才会生效
    ac.click()
  <11>.以上操作都需要回放,否则无法展示效果
    ac.perform()
  
posted @ 2022-06-05 14:16  maxwell11  阅读(50)  评论(0编辑  收藏  举报