Skip to content

Commit 39f63a9

Browse files
committed
FIX: implement tmp folder and headless chrome to test print pdf
1 parent 8fe96e1 commit 39f63a9

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

botcity/web/bot.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from selenium.webdriver.remote.webelement import WebElement
2626
from selenium.webdriver.support.wait import WebDriverWait, TimeoutException, NoSuchElementException
2727
from selenium.webdriver.support import expected_conditions as EC
28+
from selenium.webdriver.common.print_page_options import PrintOptions
2829

2930
from . import config, cv2find, compat
3031
from .browsers import BROWSER_CONFIGS, Browser, PageLoadStrategy
@@ -266,7 +267,7 @@ def check_driver():
266267
def _get_parameters_to_driver(self):
267268
if self.browser == Browser.UNDETECTED_CHROME:
268269
return {"driver_executable_path": self.driver_path, "options": self.options,
269-
"desired_capabilities": self.capabilities}
270+
"desired_capabilities": self.capabilities, "headless": True}
270271
if compat.version_selenium_is_larger_than_four():
271272
return {"options": self.options, "service": self._get_service()}
272273

@@ -1137,7 +1138,13 @@ def print_pdf(self, path=None, print_options=None):
11371138
'marginTop': 0,
11381139
'marginBottom': 0
11391140
}
1140-
data = self._webdriver_command("print", print_options)
1141+
print_options = PrintOptions()
1142+
print_options.page_ranges = ['1-2']
1143+
print_options.margin_top = 0
1144+
print_options.margin_bottom = 0
1145+
print_options.background = True
1146+
print_options.orientation = 'landscape'
1147+
data = self._driver.print_page(print_options)
11411148
bytes_file = base64.b64decode(data)
11421149
if not path:
11431150
path = default_path

botcity/web/browsers/chrome.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ def default_options(headless=False, download_folder_path=None, user_data_dir=Non
5454
# Disable banner for Browser being remote-controlled
5555
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
5656
chrome_options.add_experimental_option('useAutomationExtension', False)
57-
57+
chrome_options.binary_location = '/usr/bin/google-chrome-beta'
5858
if headless:
5959
chrome_options.add_argument("--headless")
6060
chrome_options.add_argument("--headless=new")
61+
chrome_options.add_argument("--headless=chrome")
6162
chrome_options.add_argument("--disable-gpu")
6263
chrome_options.add_argument("--hide-scrollbars")
6364
chrome_options.add_argument("--mute-audio")

tests/test_browser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ def test_set_current_element(web: WebBot):
284284
assert result['data'] == ['Left2'] or result['data'] == ['Left']
285285

286286

287-
@pytest.mark.xfail(reason="Unknown chrome/undetected error to be investigated.")
288-
def test_print_pdf(web: WebBot):
287+
# @pytest.mark.xfail(reason="Unknown chrome/undetected error to be investigated.")
288+
def test_print_pdf(web: WebBot, tmp_folder):
289289
web.browse(conftest.INDEX_PAGE)
290-
pdf = web.print_pdf(path=os.path.join(conftest.PROJECT_DIR, 'page.pdf'))
290+
pdf = web.print_pdf(path=os.path.join(tmp_folder, 'page.pdf'))
291291

292292
assert os.path.exists(pdf)
293293
os.remove(pdf)

0 commit comments

Comments
 (0)