user1592380
user1592380

Reputation: 36307

Selenium webdriver screenshot not being taken from django

I have a functional test 'y1.py' which I have exported from the selenium IDE. It looks like:

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()

I have created a django app 'fts' and have placed the script in 'fts/tests.py. I added the line:

driver.save_screenshot('out11.png') 

to the end to get a screenshot.

When I run the script from the command line using:

$ python manage.py test fts

the test passes but no screenshot is taken. How can I fix this?

Upvotes: 0

Views: 1032

Answers (1)

Victor Sigler
Victor Sigler

Reputation: 23449

I personally use get_screenshot_as_file instead, this is my code :

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

With save_screenshot you have to write the image to a file to keep in memory.

Upvotes: 2

Related Questions