0

I am working on Win7 with PyCharm3. I have a functional test 'y1.py' which I have exported from the selenium IDE. It contains:

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.get_screenshot_as_file('foo.png')    

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)    

if __name__ == "__main__":
    unittest.main()

When I run the script from the pycharm manage.py tool I realized that the screenshots are being saved to

PyCharm 3.0.1\jre\jre\bin"

Also, for some reason specifying the path explicitly does not work :

(driver.get_screenshot_as_file('c/foo1.png') ).

I've tried variations of this based on Webdriver Screenshot, but can't get it to work. How could I use python to directly save the screenshot to a directory "screenshots" under my project root?

Edit:

I realized that there is a difference b/w the command line's manage.py and pycharm's tools->manage.py ( which is what I've been using ). When you run it from the command line the tests run much faster and the screenshot saves in the project's root directory ( which is what I wanted ). Hope this helps someone. - Bill

2
  • 1
    did you really try c/foo1.png? this is not a valid path.. how about C:\\foo1.png Commented Jan 31, 2014 at 18:17
  • I did try this and others in the post above. Please see edit. Commented Jan 31, 2014 at 20:10

1 Answer 1

1

I didn't have success with get_screenshot_as_file() but I offer an alternative using get_screenshot_as_base64(). Define this function in another module:

def save_screenshot(self, driver, file_name_prefix):
    img_str64 = driver.get_screenshot_as_base64()
    f = open("screenshots/%s.png" % file_name_prefix, "wb")
    f.write(img_str64.decode("base64"))
    f.close()

Then, in the test replace

driver.get_screenshot_as_file('foo.png')
module_containing_this_function.save_screenshot("foo")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.