具有下载功能的无头浏览器测试? [英] Headless browser testing with download functionality?

查看:73
本文介绍了具有下载功能的无头浏览器测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找在osx中​​进行无头测试的解决方案.但是我需要能够保存服务器返回的文件.

I've been looking for a solution to do headless testing in osx. But I need the ability to save files returned by the server.

我已经测试了硒,phantomjs,casperjs,并研究了可以在网上找到的任何东西.

I've tested selenium, phantomjs, casperjs and have looked into anything I could find online.

它们都不支持下载.我错过了什么吗?是否有支持下载的无头浏览器/测试框架?

none of them supports downloading. am I missing something? are there any headless browser/testing frameworks that support downloads?

推荐答案

您可以做的是:

  • 启动虚拟展示(请参见 Xvfb )
  • 使用配置为自动保存csv文件
  • 的首选项启动Firefox浏览器
  • start a virtual display (see Xvfb)
  • start up Firefox browser with preferences configured to automatically save csv files

工作示例并带有附加注释(使用 pyvirtualdisplay xvfb包装器):

Working example in python with additional comments (using pyvirtualdisplay xvfb wrapper):

from os import getcwd
import time

from pyvirtualdisplay import Display
from selenium import webdriver

# start the virtual display
display = Display(visible=0, size=(800, 600))
display.start()

# configure firefox profile to automatically save csv files in the current directory
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")

browser = webdriver.Firefox(firefox_profile=fp)
browser.get('http://www.nationale-loterij.be/nl/onze-spelen/lotto/resultaten')

# check the option
browser.find_element_by_id('corporatebody_3_corporategrid93961a8f9b424ed6bd0697df356d9483_1_rblType_0').click()

# click the link
browser.find_element_by_name('corporatebody_3$corporategrid93961a8f9b424ed6bd0697df356d9483_1$btnDownload').click()

# hardcoded delay for waiting a file download (better check for the downloaded file to appear on the disk)
time.sleep(2)

# quit the browser
browser.quit()

# stop the display
display.stop()

另请参阅:

  • How do I run Selenium in Xvfb?
  • Access to file download dialog in Firefox

这篇关于具有下载功能的无头浏览器测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆