How to explicitly write a Selenium WebDriver screenshot to a directory in python -
i working on win7 pycharm3. have functional test 'y1.py' have exported selenium ide. 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 run script pycharm manage.py tool realized screenshots being saved
pycharm 3.0.1\jre\jre\bin"
also, reason specifying path explicitly not work :
(driver.get_screenshot_as_file('c/foo1.png') ).
i've tried variations of based on webdriver screenshot, can't work. how use python directly save screenshot directory "screenshots" under project root?
edit:
i realized there difference b/w command line's manage.py , pycharm's tools->manage.py ( i've been using ). when run command line tests run faster , screenshot saves in project's root directory ( wanted ). hope helps someone. - bill
i didn't have success get_screenshot_as_file()
offer alternative using get_screenshot_as_base64()
. define function in 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 test replace
driver.get_screenshot_as_file('foo.png')
module_containing_this_function.save_screenshot("foo")
Comments
Post a Comment