Reputation: 36205
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
Upvotes: 0
Views: 2301
Reputation: 36
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")
Upvotes: 1