Get list of filenames in File Garden

This commit is contained in:
The Magician 2023-10-20 18:32:23 +01:00
parent a60ca8eb52
commit 68d37afec7
2 changed files with 75 additions and 4 deletions

View File

@ -2,9 +2,12 @@
import sys import sys
import bs4
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
def initialize_webdriver(): def initialize_webdriver():
return webdriver.Firefox() return webdriver.Firefox()
@ -31,15 +34,29 @@ def login_with_password(browser, email, password):
click_password_button(browser) click_password_button(browser)
input_password(browser, password) input_password(browser, password)
def click_go_to_your_garden(browser):
browser.implicitly_wait(100)
goToYourGardenButton = browser.find_element(By.LINK_TEXT, "GO TO YOUR GARDEN")
goToYourGardenButton.click()
def get_garden_filenames(browser):
titles = []
soup = bs4.BeautifulSoup(browser.page_source, "lxml")
for item in soup.find_all(attrs={"class": "name"}):
titles.append(item.text)
return titles
def main(email, password): def main(email, password):
browser = initialize_webdriver() browser = initialize_webdriver()
navigate_to_filegarden(browser) navigate_to_filegarden(browser)
login_with_password(browser, email, password) login_with_password(browser, email, password)
# Go To Your Garden click_go_to_your_garden(browser)
# Get list of filenames in File Garden garden_filenames = get_garden_filenames(browser)
# Get list of files in target upload directory that don't exist in File Garden (test based on filename? size? file contents?) # Get list of files in target upload directory that don't exist in File Garden (test based on filename? size? file contents?)
# For each file in the second list, go through the file upload process # For each file in the second list, go through the file upload process
browser.close() #browser.close()
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv[1], sys.argv[2]) main(sys.argv[1], sys.argv[2])

View File

@ -66,10 +66,62 @@ class TestJardin(unittest.TestCase):
mockClickPasswordButton.assert_called_once_with(mockFirefox) mockClickPasswordButton.assert_called_once_with(mockFirefox)
mockInputPassword.assert_called_once_with(mockFirefox, mockPassword) mockInputPassword.assert_called_once_with(mockFirefox, mockPassword)
@patch("jardin.webdriver.Firefox")
def test_click_go_to_your_garden_clicks_button(self, mockFirefox):
jardin.click_go_to_your_garden(mockFirefox)
mockFirefox.find_element.assert_called_with(By.LINK_TEXT, "GO TO YOUR GARDEN")
mockFirefox.find_element.return_value.click.assert_called_once()
@patch("jardin.webdriver.Firefox")
@patch("jardin.bs4.BeautifulSoup")
def test_get_garden_filenames_creates_beautifulsoup_and_finds_names(self, mockBeautifulSoup, mockFirefox):
jardin.get_garden_filenames(mockFirefox)
mockBeautifulSoup.assert_called_once_with(mockFirefox.page_source, "lxml")
mockBeautifulSoup.return_value.find_all.assert_called_once_with(attrs={"class": "name"})
@patch("jardin.webdriver.Firefox")
def test_get_garden_filenames_returns_filenames_correctly(self, mockFirefox):
mockFirefox.page_source = """
<html lang="en">
<body class="mdc-typography">
<div id="items" class="items">
<a class="item typeFile selected" draggable="false" ondragstart="return false;" href="https://file.garden/fakeid/file1.jpg">
<div class="cell icon">
<button class="mdc-icon-button material-icons" type="button">image</button>
</div>
<div class="cell name" title="file1.jpg">file1.jpg</div>
<div class="cell size" title="10 B">10 B</div>
<div class="cell type" title="image/jpeg">image/jpeg</div>
<div class="cell date" title="Wed Aug 30 2023 13:04:10 GMT+0100 (British Summer Time)">Aug 30 2023 13:04:10</div>
</a>
<a class="item typeFile" draggable="false" ondragstart="return false;" href="https://file.garden/fakeid/file2.jpg">
<div class="cell icon">
<button class="mdc-icon-button material-icons" type="button">image</button>
</div>
<div class="cell name" title="file2.jpg">file2.jpg</div>
<div class="cell size" title="20 B">20 B</div>
<div class="cell type" title="image/jpeg">image/jpeg</div>
<div class="cell date" title="Wed Jun 29 2022 17:37:14 GMT+0100 (British Summer Time)">Jun 29 2022 17:37:14</div>
</a>
</div>
</body>
</html>
"""
filenames = jardin.get_garden_filenames(mockFirefox)
self.assertEqual(filenames, ["file1.jpg", "file2.jpg"])
@patch("jardin.initialize_webdriver") @patch("jardin.initialize_webdriver")
@patch("jardin.navigate_to_filegarden") @patch("jardin.navigate_to_filegarden")
@patch("jardin.login_with_password") @patch("jardin.login_with_password")
@patch("jardin.click_go_to_your_garden")
@patch("jardin.get_garden_filenames")
def test_main_calls_methods_in_correct_order(self, def test_main_calls_methods_in_correct_order(self,
mockGetGardenFilenames,
mockClickGoToYourGarden,
mockLoginWithPassword, mockLoginWithPassword,
mockNavigateToFilegarden, mockNavigateToFilegarden,
mockInitializeWebdriver): mockInitializeWebdriver):
@ -81,8 +133,10 @@ class TestJardin(unittest.TestCase):
mockInitializeWebdriver.assert_called_once() mockInitializeWebdriver.assert_called_once()
mockNavigateToFilegarden.assert_called_once_with(mockInitializeWebdriver.return_value) mockNavigateToFilegarden.assert_called_once_with(mockInitializeWebdriver.return_value)
mockLoginWithPassword.assert_called_once_with(mockInitializeWebdriver.return_value, mockEmail, mockPassword) mockLoginWithPassword.assert_called_once_with(mockInitializeWebdriver.return_value, mockEmail, mockPassword)
mockClickGoToYourGarden.assert_called_once_with(mockInitializeWebdriver.return_value)
mockGetGardenFilenames.assert_called_once_with(mockInitializeWebdriver.return_value)
mockInitializeWebdriver.return_value.close.assert_called_once() #mockInitializeWebdriver.return_value.close.assert_called_once()
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()