#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import  WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.keys import Keys
__author__ = 'matti'


# Initialize webdriver
def init_driver():
    driver = webdriver.Firefox()
    driver.set_window_size(1024, 768)
    return driver

# Load stuff from config.json
def load_settings_from_json():
    with open('config.json') as settings_file:
        settings = json.load(settings_file)
        return settings

# Waits set amount of time until element is visible
def wait_until_element_available(driver, id, timeout):
    try:
        element = WebDriverWait(driver, timeout).until(
            EC.presence_of_element_located((By.ID, id))
        )
    finally:
        return

# Checks if element exists, returns it if it does.
def check_if_element_available(driver, class_name):
    try:
        element = driver.find_element_by_class_name(class_name)
        return element
    except NoSuchElementException:
        return None

# Logs in to Reportronic...
def login_to_reportronic(username, password, driver):
    wait_until_element_available(driver, "chkLDAPTunnistus", 5)
    driver.find_element_by_id('chkLDAPTunnistus').click()
    driver.find_element_by_id('itxtUserID').send_keys(username)
    driver.find_element_by_id('itxtPassword').send_keys(password)
    driver.find_element_by_id('icmdOk').click()

    errorMsg = check_if_element_available(driver, 'SBErrorText')
    if errorMsg is not None:
        print "Login failed... " + errorMsg.text



# Load settings, create webdriver, login to reportronic...
def main():
    settings = load_settings_from_json()
    driver = init_driver()

    driver.get(settings['url'])
    login_to_reportronic(settings['username'], settings['password'], driver)

if __name__ == '__main__':
    main()
@@ -76,16 +76,42 @@
  sleep%0A%0A
+from pprint import pprint%0A
 from sel
@@ -838,20 +838,16 @@
 ver,
- id,
  timeout
 ):%0A 
@@ -842,16 +842,40 @@
  timeout
+, selectortype, selector
 ):%0A    t
@@ -982,17 +982,30 @@
 ed((
-By.ID, id
+selectortype, selector
 ))%0A 
@@ -1420,17 +1420,28 @@
 driver, 
-%22
+10, By.ID, '
 chkLDAPT
@@ -1452,12 +1452,9 @@
 stus
-%22, 5
+'
 )%0A  
@@ -1831,17 +1831,613 @@
 sg.text%0A
-%0A
+        exit(1)%0A%0Adef go_to_worktimes(driver):%0A    driver.find_element_by_class_name('uiMenuWork').click()%0A    wait_until_element_available(driver, 10, By.ID, 'prlWTEP_uwtWorkTime__ctl0_rlbLisaaTyoaika')%0A    driver.find_element_by_id('prlWTEP_uwtWorkTime__ctl0_rlbLisaaTyoaika').click()%0A%0Adef get_worktime_cells(driver, projectname, date=None):%0A    selector = %22.WTGCellWrapper input%5Btitle*='%7Bprojectname%7D'%5D%22.format(projectname=projectname)%0A    if date is not None:%0A        selector += %22%5Btitle*='%7Bdate%7D'%5D%22.format(date=date)%0A    print selector%0A    return driver.find_elements_by_css_selector(selector)
 %0A%0A# Load
@@ -2679,16 +2679,207 @@
 driver)%0A
+    go_to_worktimes(driver)%0A    wait_until_element_available(driver, 10, By.CLASS_NAME, 'WTGCellWrapper')%0A%0A    test = get_worktime_cells(driver, 'JAMK', date='01.06.2015')%0A    print len(test)
 %0Aif __na

Add stuff