mardi 27 octobre 2020

Upload and process a file in a MaterializeCSS form with Selenium

Html of the form:

<form action="" method="post" enctype="multipart/form-data"><input type="hidden" name="csrfmiddlewaretoken" value="nTfw60C2FSwLfGTvOlO6FITRnz4yq2HObLpcDw1enoqaT9JsD4ztaDJpCpBVBpS7">
  <div class="file-field input-field">
    <div class="btn">
      <input type="file" name="uploaded" id="id_uploaded">
      <span>Choose file</span>
    </div>
    <div class="file-path-wrapper">
      <input class="file-path validate" type="text" id="id_path">
    </div>
  </div>
  <div class="col s12">
    <button class="btn-flat" id="id_submit" type="submit">Submit</button>
  </div>
</form>

The problem is that a click on the file text input opens a file dialog and MaterializeCSS doesn't accept pasting a file path string. Also, only the filename without the full path is shown in the text input after a user selects a file in the dialog.

When the following code runs then the form invalidates with my no file provided error:

text_input = webdriver.find_element_by_id("id_path")
text_input.clear()
text_input.send_keys(path_to_file)
webdriver.find_element_by_id("id_submit").click()

Any suggestions on how to solve this problem and provide a file in tests? Any workaround could be an acceptable answer as I just want to test the functionality that takes place after submitting a file.

Aucun commentaire:

Enregistrer un commentaire