#!/usr/bin/env python
# -*- coding: utf-8 -*-
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
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

'''
Andre Geracimian
Last Updated: June 21, 2015
'''

class TradeOrder(unittest.TestCase):
    def setUp(self):
        #Added these three lines
        desired_cap = {'browser': 'firefox', 'browser_version': '32.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_order(self):
        driver = self.driver
        driver.get(self.base_url + "")
        driver.find_element_by_xpath("//div/input").clear()
        driver.find_element_by_xpath("//div/input").send_keys("dianes@4over.com")
        driver.find_element_by_xpath("//div[2]/input").clear()
        driver.find_element_by_xpath("//div[2]/input").send_keys("tradea4over")
        driver.find_element_by_xpath("//div[3]/input").click()
        time.sleep(1)
        driver.find_element_by_xpath("//div[2]/div/div/div/div[3]/ul/li[4]/a").click()
        time.sleep(1)
        driver.find_element_by_xpath("//html/body/div[1]/div/div/div/div[3]/div[2]/div[1]/div[2]/div[6]/div[2]/div[3]/div[10]/div[1]/a").click()
        time.sleep(1)
        Select(driver.find_element_by_xpath("//div/span/select")).select_by_visible_text("500")
        time.sleep(1)
        Select(driver.find_element_by_xpath("//div/span[2]/select")).select_by_visible_text("4:4")
        time.sleep(1)
        Select(driver.find_element_by_xpath("//div/span[3]/select")).select_by_visible_text("2-4 Business Days")
        time.sleep(1)
        driver.find_element_by_xpath("//td[3]/input").clear()
        driver.find_element_by_xpath("//td[3]/input").send_keys("Automated_test_order")
        time.sleep(3)
        driver.find_element_by_xpath("//span/input").click()
        time.sleep(4)
        driver.find_element_by_xpath("//div[2]/input").click()
        time.sleep(4)
        driver.find_element_by_xpath("//p/input").click()
        time.sleep(4)
        driver.find_element_by_xpath("//p/a/img").click()
        time.sleep(7)
        driver.find_element_by_xpath("//html/body/div[1]/div/div/div/div[3]/div[2]/div[1]/div[2]/div[1]/div[6]/div[1]/a/img").click()
        time.sleep(3)
        driver.find_element_by_xpath("//p[2]/a").click()
        time.sleep(3)
        driver.find_element_by_xpath("//html/body/div[1]/div/div/div/div[3]/div[2]/div[1]/div[2]/form/div/fieldset/div[4]/div[2]/div[2]/input").send_keys("/var/application/QAAL/image/test_QA_BC.png")
        time.sleep(3)
        driver.find_element_by_xpath("//html/body/div[1]/div/div/div/div[3]/div[2]/div[1]/div[2]/form/div/fieldset/div[4]/div[2]/div[5]/input").send_keys("/var/application/QAAL/image/test_QA_BC.png")
        time.sleep(3)
        driver.find_element_by_xpath("//input[3]").click()
        time.sleep(12)
    
    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()
