我有一个功能测试'y1.py',我从selenium IDE导出。它看起来像:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Y1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.yahoo.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_y1(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_link_text("Weather").click()
driver.save_screenshot('out11.png')
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
我创建了一个django app'fts'并将脚本放在'fts / tests.py中。我添加了一行:
driver.save_screenshot('out11.png')
到最后得到截图。
当我使用以下命令行运行脚本时:
$ python manage.py test fts
测试通过,但没有截屏。我该如何解决这个问题?
答案 0 :(得分:2)
我个人使用get_screenshot_as_file
代替,这是我的代码:
from selenium import webdriver
if __name__ == '__main__':
browser = webdriver.Firefox()
try:
browser.get('http://www.google.com')
browser.get_screenshot_as_file('screenshot.png')
except Exception as e:
print e
使用save_screenshot
,您必须将图像写入文件以保留在内存中。