diff options
| author | Teddy Wing | 2018-02-04 03:17:01 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2018-02-04 03:17:01 +0100 | 
| commit | 0316618c74b2d15ee31f9a7627aef4c1d44bfd7b (patch) | |
| tree | db5750000a34f0692edf89865ce81798037399e0 /harvester_submit_week_for_approval.py | |
| parent | b289678fba194fc8f26b244724bc93ff99fe06de (diff) | |
| download | harvester-submit-week-for-approval-0316618c74b2d15ee31f9a7627aef4c1d44bfd7b.tar.bz2 | |
Draft of submitting week for approval
First pass, doesn't work yet.
This will determine last Friday's date and visit the page corresponding
to that week. It will then try to submit that week's timesheet.
The `wait` on the `script` tag contents waiting for the "week" page to
load isn't working. Looks like you can't wait on the contents of a
`<script>` tag.
Moved the `wait` variable outside of `login` so it can be used in
`submit_week_for_approval`.
Diffstat (limited to 'harvester_submit_week_for_approval.py')
| -rw-r--r-- | harvester_submit_week_for_approval.py | 54 | 
1 files changed, 51 insertions, 3 deletions
| diff --git a/harvester_submit_week_for_approval.py b/harvester_submit_week_for_approval.py index 586cc4f..809979b 100644 --- a/harvester_submit_week_for_approval.py +++ b/harvester_submit_week_for_approval.py @@ -1,5 +1,7 @@  #!/usr/bin/env python +from datetime import datetime, timedelta +  from selenium.webdriver import Firefox  from selenium.webdriver.common.by import By  from selenium.webdriver.common.keys import Keys @@ -11,9 +13,8 @@ email = ''  password = ''  subdomain = '' -def login(driver): -    wait = WebDriverWait(driver, timeout=10) +def login(driver, wait):      driver.get('https://id.getharvest.com/harvest/sign_in')      wait.until( @@ -27,6 +28,50 @@ def login(driver):      return driver +def most_recent_friday(): +    friday = 4 +    now = datetime.now() + +    if now.weekday() >= friday: +        return now - timedelta(days=now.weekday() - friday) +    else: +        return now - timedelta(weeks=1) + timedelta(days=friday - now.weekday()) + +def submit_week_for_approval(driver, wait): +    # friday = most_recent_friday() +    friday = datetime(2018, 1, 15) + +    url = 'https://{subdomain}.harvestapp.com/time/week/{year}/{month}/{day}/'.format( +        subdomain=subdomain, +        year=friday.year, +        month=friday.month, +        day=friday.day) + +    driver.get(url) + +    # Wait for page to load +    wait.until(expected.text_to_be_present_in_element( +        (By.ID, 'days-in-week-data-island'), +        friday.strftime('%F'))) + +    # Click "Submit Week for Approval" button +    driver.find_element_by_css_selector( +        '.approval-button .submit-link' +    ).click() + +    # Click "Yes, Submit Timesheet" button +    wait.until( +        expected.visibility_of_element_located( +            ( +                By.CSS_SELECTOR, +                '.approval-confirmation .js-submit-for-approval', +            ))) + +    # Wait for success message +    wait.until(expected.text_to_be_present_in_element( +        (By.ID, 'status_message'), +        'Timesheet has been submitted for approval.')) +  if __name__ == "__main__":      options = Options()      # options.add_argument('-headless') @@ -35,7 +80,10 @@ if __name__ == "__main__":          firefox_binary='/Applications/FirefoxDeveloperEdition.app/Contents/MacOS/firefox-bin',          firefox_options=options) -    driver = login(driver) +    wait = WebDriverWait(driver, timeout=10) + +    driver = login(driver, wait) +    driver = submit_week_for_approval(driver, wait)      print(driver.page_source) | 
