Merge pull request #832 from seleniumbase/fix-choose-file-method

Allow the choose_file() method to work on hidden "input" fields
This commit is contained in:
Michael Mintz 2021-03-03 16:04:14 -05:00 committed by GitHub
commit 4ffd87f65c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 7 deletions

View File

@ -48,7 +48,7 @@ class SwagLabsTests(BaseCase):
# Checkout - Add info
self.click("link=CHECKOUT")
self.assert_exact_text("Checkout: Your Information", "div.subheader")
self.assert_text("Checkout: Your Information", "div.subheader")
self.assert_element("a.cart_cancel_link")
self.type("#first-name", "SeleniumBase")
self.type("#last-name", "Rocks")
@ -56,7 +56,7 @@ class SwagLabsTests(BaseCase):
# Checkout - Overview
self.click("input.btn_primary")
self.assert_exact_text("Checkout: Overview", "div.subheader")
self.assert_text("Checkout: Overview", "div.subheader")
self.assert_element("link=CANCEL")
self.assert_text(item_name, "div.inventory_item_name")
self.assert_text(item_price, "div.inventory_item_price")

View File

@ -1,11 +1,11 @@
""" Testing the self.choose_file() method. """
import os
from seleniumbase import BaseCase
class FileUploadButtonTests(BaseCase):
""" The main purpose of this is to test the self.choose_file() method. """
def test_file_upload_button(self):
self.open("https://www.w3schools.com/jsref/tryit.asp"
"?filename=tryjsref_fileupload_get")
@ -15,7 +15,7 @@ class FileUploadButtonTests(BaseCase):
self.add_css_style(zoom_in)
self.highlight('input[type="file"]')
dir_name = os.path.dirname(os.path.abspath(__file__))
file_path = dir_name + "/example_logs/screenshot.png"
file_path = os.path.join(dir_name, "example_logs/screenshot.png")
self.choose_file('input[type="file"]', file_path)
self.demo_mode = True # Adds highlighting to the assert statement
self.assert_element('input[type="file"]')

View File

@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "1.56.4"
__version__ = "1.56.5"

View File

@ -2964,7 +2964,33 @@ class BaseCase(unittest.TestCase):
timeout = self.__get_new_timeout(timeout)
selector, by = self.__recalculate_selector(selector, by)
abs_path = os.path.abspath(file_path)
self.add_text(selector, abs_path, by=by, timeout=timeout)
element = self.wait_for_element_present(
selector, by=by, timeout=timeout)
if self.is_element_visible(selector, by=by):
self.__demo_mode_highlight_if_active(selector, by)
if not self.demo_mode and not self.slow_mode:
self.__scroll_to_element(element, selector, by)
pre_action_url = self.driver.current_url
if type(abs_path) is int or type(abs_path) is float:
abs_path = str(abs_path)
try:
element.send_keys(abs_path)
except (StaleElementReferenceException, ENI_Exception):
self.wait_for_ready_state_complete()
time.sleep(0.16)
element = self.wait_for_element_present(
selector, by=by, timeout=timeout)
element.send_keys(abs_path)
except Exception:
exc_message = self.__get_improved_exception_message()
raise Exception(exc_message)
if self.demo_mode:
if self.driver.current_url != pre_action_url:
self.__demo_mode_pause_if_active()
else:
self.__demo_mode_pause_if_active(tiny=True)
elif self.slow_mode:
self.__slow_mode_pause_if_active()
def save_element_as_image_file(
self, selector, file_name, folder=None, overlay_text=""):