#!/usr/bin/env python
'''
Andre Geracimian
Last Update: August 25, 2014
This script will be used to login to trade and check links for product categories and logout.
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class TradeProdCategoriesLinks(unittest.TestCase):
    def setUp(self):
        #Added these three lines
        desired_cap = {'browser': 'firefox', 'browser_version': '31.0', 'os': 'Windows', 'os_version': '8.1', 'resolution': '2048x1536'}

#Added this line to enable the Visual Logs tab.
        desired_cap['browserstack.debug'] = True
        desired_cap['build'] = 'v1.0'
        desired_cap['project'] = 'Trade'

#To run on BrowserStack's remote machines
        self.driver =     webdriver.Remote(command_executor='http://dianesalter1:xXz8vB9ft7A9zjksCyBm@hub.browserstack.com:80/wd/hub', desired_capabilities=desired_cap)

        self.driver.implicitly_wait(30)
        self.base_url = "https://trade.4over.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_trade_prod_categories_links(self):
        driver = self.driver
    #    driver.set_window_size(2000, 2560)
        driver.get(self.base_url + "/")
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='login_top']/form/div/input").clear()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='login_top']/form/div/input").send_keys("andreg@4over.com")
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='login_top']/form/div[2]/input").clear()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='login_top']/form/div[2]/input").send_keys("asdasd")
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='login_top']/form/div[3]/input").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li/a)[20]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[2]/a)[3]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[3]/a)[3]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[4]/a)[3]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[6]/a)[3]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[7]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[8]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[9]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[10]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[11]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[12]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("(//div[@id='categories']/ul/li[13]/a)[2]").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[14]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[15]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[16]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[17]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[18]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[19]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[20]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[21]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[22]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[24]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[25]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[26]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[27]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[32]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[33]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[34]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[35]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[36]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='categories']/ul/li[37]/a").click()
        driver.save_screenshot('screenshot.png')
        driver.find_element_by_xpath("//div[@id='scoop']/div[4]/div[2]/a").click()
        driver.save_screenshot('screenshot.png')
        print "Succeed"
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True
    
    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True
    
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()
