Skip to content

Commit 4ffd87f

Browse files
authored
Merge pull request #832 from seleniumbase/fix-choose-file-method
Allow the choose_file() method to work on hidden "input" fields
2 parents 5597d71 + 5145d36 commit 4ffd87f

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

examples/test_swag_labs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ def test_swag_labs_basic_flow(self):
4848

4949
# Checkout - Add info
5050
self.click("link=CHECKOUT")
51-
self.assert_exact_text("Checkout: Your Information", "div.subheader")
51+
self.assert_text("Checkout: Your Information", "div.subheader")
5252
self.assert_element("a.cart_cancel_link")
5353
self.type("#first-name", "SeleniumBase")
5454
self.type("#last-name", "Rocks")
5555
self.type("#postal-code", "01720")
5656

5757
# Checkout - Overview
5858
self.click("input.btn_primary")
59-
self.assert_exact_text("Checkout: Overview", "div.subheader")
59+
self.assert_text("Checkout: Overview", "div.subheader")
6060
self.assert_element("link=CANCEL")
6161
self.assert_text(item_name, "div.inventory_item_name")
6262
self.assert_text(item_price, "div.inventory_item_price")

examples/upload_file_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
""" Testing the self.choose_file() method. """
2+
13
import os
24
from seleniumbase import BaseCase
35

46

57
class FileUploadButtonTests(BaseCase):
68

7-
""" The main purpose of this is to test the self.choose_file() method. """
8-
99
def test_file_upload_button(self):
1010
self.open("https://www.w3schools.com/jsref/tryit.asp"
1111
"?filename=tryjsref_fileupload_get")
@@ -15,7 +15,7 @@ def test_file_upload_button(self):
1515
self.add_css_style(zoom_in)
1616
self.highlight('input[type="file"]')
1717
dir_name = os.path.dirname(os.path.abspath(__file__))
18-
file_path = dir_name + "/example_logs/screenshot.png"
18+
file_path = os.path.join(dir_name, "example_logs/screenshot.png")
1919
self.choose_file('input[type="file"]', file_path)
2020
self.demo_mode = True # Adds highlighting to the assert statement
2121
self.assert_element('input[type="file"]')

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "1.56.4"
2+
__version__ = "1.56.5"

seleniumbase/fixtures/base_case.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2964,7 +2964,33 @@ def choose_file(self, selector, file_path, by=By.CSS_SELECTOR,
29642964
timeout = self.__get_new_timeout(timeout)
29652965
selector, by = self.__recalculate_selector(selector, by)
29662966
abs_path = os.path.abspath(file_path)
2967-
self.add_text(selector, abs_path, by=by, timeout=timeout)
2967+
element = self.wait_for_element_present(
2968+
selector, by=by, timeout=timeout)
2969+
if self.is_element_visible(selector, by=by):
2970+
self.__demo_mode_highlight_if_active(selector, by)
2971+
if not self.demo_mode and not self.slow_mode:
2972+
self.__scroll_to_element(element, selector, by)
2973+
pre_action_url = self.driver.current_url
2974+
if type(abs_path) is int or type(abs_path) is float:
2975+
abs_path = str(abs_path)
2976+
try:
2977+
element.send_keys(abs_path)
2978+
except (StaleElementReferenceException, ENI_Exception):
2979+
self.wait_for_ready_state_complete()
2980+
time.sleep(0.16)
2981+
element = self.wait_for_element_present(
2982+
selector, by=by, timeout=timeout)
2983+
element.send_keys(abs_path)
2984+
except Exception:
2985+
exc_message = self.__get_improved_exception_message()
2986+
raise Exception(exc_message)
2987+
if self.demo_mode:
2988+
if self.driver.current_url != pre_action_url:
2989+
self.__demo_mode_pause_if_active()
2990+
else:
2991+
self.__demo_mode_pause_if_active(tiny=True)
2992+
elif self.slow_mode:
2993+
self.__slow_mode_pause_if_active()
29682994

29692995
def save_element_as_image_file(
29702996
self, selector, file_name, folder=None, overlay_text=""):

0 commit comments

Comments
 (0)