def test_download_url_sync(self): fname = "some-file.txt" url = f"https://the-internet.herokuapp.com/download/{fname}" home_dir = os.getenv('HOME') or os.path.expanduser( os.getenv('USERPROFILE')) to_dir = os.path.join(home_dir, "tmp") f = os.path.join(to_dir, fname) #preset if file_exists(f): rm_files(f) #before self.assertFalse(file_exists(f)) download_url(url, to_dir=to_dir, asynch=False, overwrite_existing=True) self.assertTrue(file_exists(f))
def test_links(self): with Browser() as b: h = b.html(url="http://google.com") ls = h.elements.links() #print(ls) self.assertTrue(len(ls) > 0) p = h.save() self.assertTrue(file_exists(p))
def test_webdriver_download(self): home_dir = os.getenv('HOME') or os.path.expanduser( os.getenv('USERPROFILE')) to_dir = os.path.join(home_dir, "tmp") f = os.path.join(to_dir, "chromedriver.exe") #preset if file_exists(f): rm_files(f) #before self.assertFalse(file_exists(f)) start = datetime.now() end = start webdriver_downloader.download_chrome(to_dir=to_dir, asynch=True) #to test asynchronous download while not file_exists(f): end = datetime.now() total_secs = (end - start).total_seconds() self.assertTrue(total_secs > 0)
def test_download_url(self): url = "https://the-internet.herokuapp.com/download/some-file.txt" home_dir = os.getenv('HOME') or os.path.expanduser( os.getenv('USERPROFILE')) to_dir = os.path.join(home_dir, "tmp") f = os.path.join(to_dir, "some-file.txt") #preset if file_exists(f): rm_files(f) #before self.assertFalse(file_exists(f)) start = datetime.now() end = start download_url(url, to_dir=to_dir, overwrite_existing=True) #async True by default #to test asynchronous download while not file_exists(f): end = datetime.now() total_secs = (end - start).total_seconds() self.assertTrue(total_secs > 0)
def test_elements(self): dp = "C:\\Users\Ranjith\\pybrowser\\browserdrivers" with Browser(browser_name=Browser.CHROME, headless=True, driver_path=dp) as b: h = b.goto( "https://the-internet.herokuapp.com/forgot_password").html() e = h.elements.find_by_id('email') p = h.save() self.assertTrue(file_exists(p)) self.assertEqual(e.type, "text") self.assertEqual(e.name, "email")
def test_screenshots(self): home_dir = os.getenv('HOME') or os.path.expanduser( os.getenv('USERPROFILE')) bro = Browser(browser_name=Browser.CHROME, headless=True) bro.goto("https://www.google.com/") v = bro.button("name:=btnK").name self.assertEqual(v, "btnK") bro.input("name:=q").enter("sachin") bro.button("name:=btnK").click() f = bro.take_screenshot() self.assertTrue(file_exists(f)) bro.input("name:=q").enter("virat kohli").submit() f = bro.take_screenshot(os.path.join(home_dir, "tmp")) self.assertTrue(file_exists(f)) bro.input("name:=q").enter("ab de villiers").submit() f = bro.take_screenshot(os.path.join(home_dir, "tmp")) self.assertTrue(file_exists(f)) bro.input("name:=q").enter("dhoni").submit() f = bro.take_screenshot("googly_dhoni") self.assertTrue(file_exists(f)) bro.input("name:=q").enter("ganguly").submit() f = bro.take_screenshot("googly_ganguly.png") self.assertTrue(file_exists(f)) bro.close()
def test_file_download_upload(self): with Browser(browser_name=Browser.CHROME, headless=True) as b: home_dir = os.getenv('HOME') or os.path.expanduser( os.getenv('USERPROFILE')) to_dir = os.path.join(home_dir, "tmp") fl = "some-file.txt" f = os.path.join(to_dir, fl) #preset if file_exists(f): rm_files(f) #before self.assertFalse(file_exists(f)) start = datetime.now() end = start b.goto("https://the-internet.herokuapp.com") u = b.link("PARTIAL_LINK_TEXT:=Download").url self.assertTrue(u, "https://the-internet.herokuapp.com/download") b.link("PARTIAL_LINK_TEXT:=Download").click() fd = b.file("PARTIAL_LINK_TEXT:=some-file.txt").download( directory=to_dir) while not file_exists(f): end = datetime.now() total_secs = (end - start).total_seconds() self.assertTrue(total_secs > 0) #wait for callback to happen time.sleep(1) self.assertTrue(len(fd.downloaded_files) > 0) self.assertTrue(fd.is_download_complete) b.back() u2 = b.link("PARTIAL_LINK_TEXT:=Upload").url self.assertTrue(u2, "https://the-internet.herokuapp.com/upload") b.link("PARTIAL_LINK_TEXT:=Upload").click() b.file("file-upload").upload(filename=f) b.button("file-submit").click() t = b.element("uploaded-files").text.strip() self.assertTrue(t, fl)
def test_sample(self): with Browser(browser_name=Browser.CHROME, headless=True) as b: b.goto("https://www.google.com/") b.refresh() b.back() b.forward() self.assertEqual(200, b.response_code) hd = b.response_headers #print(hd) self.assertTrue("text/html" in hd['Content-Type']) b.input("name:=q").enter("news") b.button("name:=btnK").click() f = b.take_screenshot() self.assertTrue(file_exists(f)) ls = b.html().elements.links() self.assertTrue(len(ls) > 0)