多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

接口自动化测试-Selenium网页截图的四种方法

更新时间:2021-09-28 01:32:32 作者:admin 浏览:361

接口自动化测试-Selenium网页截图的四种方法

  有时候,你会相信一切有尽头...当你的代码走到了尽头,那么保留最后一刻的状态尤为重要,此时你该如何操作?记录日志...没有将浏览器当前的状态进行截图来的直观!那么,selenium截取截屏,有哪些方法呢?有四个。说到讲方法,什么都没有直接看源码来的直观,我带大家走一波,跟进别迷路!

  1.save_screenshot(filename)

  """

  def save_screenshot(self, filename):

  Saves a screenshot of the current window to a PNG image file. Returns

  False if there is any IOError, else returns True. Use full paths in

  your filename.

  :Args:

  - filename: The full path you wish to save your screenshot to. This

  should end with a `.png` extension.

  :Usage:

  driver.save_screenshot('/Screenshots/foo.png')

  """

  return self.get_screenshot_as_file(filename)

  可以看到要求,保存的文件,必须是以==.png==结尾的,朋友们要记得!然后他return了另外一个方法,接着走...

接口自动化测试-Selenium网页截图的四种方法

  2.get_screenshot_as_file

  def get_screenshot_as_file(self, filename):

  """

  Saves a screenshot of the current window to a PNG image file. Returns

  False if there is any IOError, else returns True. Use full paths in

  your filename.

  :Args:

  - filename: The full path you wish to save your screenshot to. This

  should end with a `.png` extension.

  :Usage:

  driver.get_screenshot_as_file('/Screenshots/foo.png')

  """

  if not filename.lower().endswith('.png'):

  warnings.warn("name used for saved screenshot does not match file "

  "type. It should end with a `.png` extension", UserWarning)

  png = self.get_screenshot_as_png()

  try:

  with open(filename, 'wb') as f:

  f.write(png)

  except IOError:

  return False

  finally:

  del png

  return True

  可以看到,这里有一个文件结尾判断,封装了UserWarning,会给出提示信息并终止程序...

  看到这两段代码,你就知道以后yoga那个方法保存文件了吧。肯定用get_screenshot_as_file,干嘛平白无故多一次回调函数...

  所以,看源码的好处就是,不至于被某些帖子所误导...如下贴:

  哥们也是好心整理,但以后还是看我的帖子吧,不迷路....我说了有四个,这才两个啊,还剩下俩呢?

  3.get_screenshot_as_png()

  看上段代码中的这行png = self.get_screenshot_as_png(),前面俩只是传销最底层发广告的,这是个业务部长...

  你看到先创建,又在finally中 ==del png==,就应该知道,它存中间变量...取方法内部看看

  def get_screenshot_as_png(self):

  """

  Gets the screenshot of the current window as a binary data.

  :Usage:

  driver.get_screenshot_as_png()

  """

  return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))

  ==binary data.==,嘛意思?二进制数据。

  怎么来的?刚才说它是业务部长,因为这里还有西北五省总代理,as_png通过b64decode解码得到了二进制数据。接着走

  4.get_screenshot_as_base64()

  def get_screenshot_as_base64(self):

  """

  Gets the screenshot of the current window as a base64 encoded string

  which is useful in embedded images in HTML.

  :Usage:

  driver.get_screenshot_as_base64()

  """

  return self.execute(Command.SCREENSHOT)['value']

  不知道初高中大家有买过一本辅导书没,叫==各个击破==,学代码应该怎么学,如果你有时间,对模块感兴趣,就该层层剖析的去看!有空大家可以继续往深里面瞅....

  看到这里,四个方法看完了...但他们也只是封装调用的,知识点还深着呢。但这四个方法是干嘛的,怎么用,什么关系,至少大家明白了吧!还怕面试的时候问吗?随便来...盘它!

  以上是关于Selenium网页截图的四种方法,由多测师亲自撰写。https://www.aichudan.com/

联系电话

17727591462

返回顶部