import unittest
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
import datetime

class Importing(unittest.TestCase):
    '''
    This test...
      * Creates a collection called "Selenium Test Book" plus the date and time
      * Adds module called "Selenium Test Page'
      * Changes  the print parameter to "CCAP Biology"
      * publishes the collection

    '''


    def setUp(self):
        self.driver = webdriver.Firefox()
        # chrome driver option
        #self.driver = webdriver.Chrome('/home/ew2/PycharmProjects/chromedriver')
        propfile = open('properties.ini')
        items = [line.rstrip('\n') for line in propfile]
        self.authkey = items[0]
        self.pw = items[1]
        self.url = items[2]
        self.zip = items[3]

    def tearDown(self):
        self.driver.save_screenshot('publishing-test.png')
        #self.driver.quit()

    def test_importing(self):
        self.driver.get(self.url)
        self.driver.implicitly_wait(300)
        #login
        authKey = self.driver.find_element_by_id('__ac_name')
        authKey.send_keys(self.authkey)
        pw = self.driver.find_element_by_id('__ac_password')
        pw.send_keys(self.pw)
        signin = self.driver.find_element_by_name('submit')
        signin.click()
        self.driver.implicitly_wait(300)
        #create book
        create_link = self.driver.find_element_by_link_text('Create a new collection')
        create_link.click()
        checkbox = self.driver.find_element_by_name('agree')
        checkbox.click()
        next = self.driver.find_element_by_name('form.button.next')
        next.click()
        name = self.driver.find_element_by_name('title')
        name.clear()
        name.send_keys('Selenium Test Book ' + str(datetime.datetime.now()))
        try:
            next = self.driver.find_element_by_name('form.button.next')
            next.click()
            self.driver.implicitly_wait(300)
        except WebDriverException:
            #getting popup error everytime so this is a workaround
            pass

        #add module
        add_button = self.driver.find_element_by_link_text('Add modules')
        add_button.click()
        popup = self.driver.find_element_by_css_selector('#collection-composer-collection-module-form-search > input[name="words"]')
        popup.send_keys('Selenium Test Page')
        search_button = self.driver.find_element_by_id('collection-composer-collection-module-search')
        search_button.click()
        checkbox = self.driver.find_element_by_name('ids:list')
        checkbox.click()
        content_add = self.driver.find_elements_by_name('form.button.ContextAdd')
        content_add[0].click()
        #change print style parameter
        parameter_tab = self.driver.find_element_by_link_text('Parameters')
        parameter_tab.click()
        printstyle = self.driver.find_element_by_name('printstyle')
        for option in printstyle.find_elements_by_tag_name('option'):
            if option.text == 'CCAP Biology':
                option.click()
                break
        update_parameters = self.driver.find_element_by_name('submit')
        update_parameters.click()
        #publish
        publish_link = self.driver.find_element_by_css_selector('#portlet-logaction > dd.portletItem.even > ul > li > a')
        publish_link.click()
        message = self.driver.find_element_by_name('message')
        message.send_keys('Test collection generated by Selenium test')
        publish_button = self.driver.find_element_by_name('form.button.publish')
        publish_button.click()
        confirm = self.driver.find_element_by_name('publish')
        confirm.click()


if __name__ == "__main__":
    unittest.main()

test that creates collection with OSC biology print parameter and publishes