# Path: wtframework/wtf/utils/test_utils.py
# def do_and_ignore(lambda_func):
#     """
#     Perform the given function, but only log the error that's printed out.
#     Use this function to wrap method calls you would normally do in a try/raise 
#     block, but do not care about the results.
# 
#     Args:
#         lambda_func (function) : Lambda function to execute.
# 
#     Returns:
#         Returns same return as the lambda statement.  Otherwise returns None
# 
#     Usage::
# 
#         do_and_ignore(lambda: driver.find_element_by_id("logoutButton").click())
# 
#     is equivalent to::
# 
#         try:
#             driver.find_element_by_id("logoutButton").click()
#         except Exception as e:
#             print e
# 
#     This function is useful for wrapping cleanup calls, since it'll ignore any errors 
#     and keeps the test moving along.
# 
#     """
#     try:
#         return lambda_func()
#     except Exception as e:
#         try:
#             print e
#         except:
#             _wtflog.debug("unknown error")
#         return None
# 
# Path: wtframework/wtf/web/webdriver.py
# WTF_WEBDRIVER_MANAGER = WebDriverManager()
# 
# Path: wtframework/wtf/web/webelement.py
# class WebElementSelector():
# 
#     "Utiltiy class for selecting elements."
# 
#     @staticmethod
#     def find_element_by_selectors(webdriver, *selectors):
#         """
#         Utility method makes it easier to find an element using multiple selectors. This is 
#         useful for problematic elements what might works with one browser, but fail in another.
#         (Like different page elements being served up for different browsers)
# 
#         Args:
#             selectors - var arg if N number of selectors to match against.  Each selector should 
#                         be a Selenium 'By' object.
# 
#         Usage::
#             my_element = WebElementSelector.find_element_by_selectors(webdriver,
#                                                                     (By.ID, "MyElementID"),
#                                                                     (By.CSS, "MyClassSelector") )
# 
#         """
#         # perform initial check to verify selectors are valid by statements.
#         for selector in selectors:
#             (by_method, value) = selector
#             if not WebElementSelector.__is_valid_by_type(by_method):
#                 raise BadSelectorError(
#                     u("Selectors should be of type selenium.webdriver.common.by.By"))
#             if type(value) != str:
#                 raise BadSelectorError(
#                     u("Selectors should be of type selenium.webdriver.common.by.By"))
# 
#         selectors_used = []
#         for selector in selectors:
#             (by_method, value) = selector
#             selectors_used.append(
#                 u("{by}:{value}").format(by=by_method, value=value))
#             try:
#                 return webdriver.find_element(by=by_method, value=value)
#             except:
#                 pass
# 
#         raise ElementNotSelectableException(
#             u("Unable to find elements using:") + u(",").join(selectors_used))
# 
#     @staticmethod
#     def __is_valid_by_type(by_type):
#         for attr, value in By.__dict__.iteritems():
#             if "__" not in attr:
#                 if by_type == value:
#                     return True
# 
#         return False
# 
# class BadSelectorError(Exception):
# 
#     "Raised when a bad selector is passed into a WebElementSelector() method."
#     pass


from selenium.common.exceptions import ElementNotSelectableException
from selenium.webdriver.common.by import By
from wtframework.wtf.utils.test_utils import do_and_ignore
from wtframework.wtf.web.webdriver import WTF_WEBDRIVER_MANAGER
from wtframework.wtf.web.webelement import WebElementSelector, BadSelectorError
import unittest2
#    along with WTFramework.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################



class TestWebElementSelector(unittest2.TestCase):

    '''
    Unit test of the WebElementSelector Class
    '''

    driver = None

    def setUp(self):
        self.driver = WTF_WEBDRIVER_MANAGER.new_driver(
            "TestWebElementSelector")

    def tearDown(self):
        do_and_ignore(lambda: WTF_WEBDRIVER_MANAGER.close_driver())

    def test_find_element_by_selectors_with_GoodSelectorFirst(self):
        self.driver.get("http://www.google.com")

        element = WebElementSelector.find_element_by_selectors(self.driver,
                                                               (By.NAME, "q"))
        self.assertIsNotNone(element)

    def test_find_element_by_selectors_with_GoodSelector2nd(self):
        self.driver.get("http://www.google.com")


# Path: wtframework/wtf/utils/test_utils.py
# def do_and_ignore(lambda_func):
#     """
#     Perform the given function, but only log the error that's printed out.
#     Use this function to wrap method calls you would normally do in a try/raise 
#     block, but do not care about the results.
# 
#     Args:
#         lambda_func (function) : Lambda function to execute.
# 
#     Returns:
#         Returns same return as the lambda statement.  Otherwise returns None
# 
#     Usage::
# 
#         do_and_ignore(lambda: driver.find_element_by_id("logoutButton").click())
# 
#     is equivalent to::
# 
#         try:
#             driver.find_element_by_id("logoutButton").click()
#         except Exception as e:
#             print e
# 
#     This function is useful for wrapping cleanup calls, since it'll ignore any errors 
#     and keeps the test moving along.
# 
#     """
#     try:
#         return lambda_func()
#     except Exception as e:
#         try:
#             print e
#         except:
#             _wtflog.debug("unknown error")
#         return None
# 
# Path: wtframework/wtf/web/webdriver.py
# WTF_WEBDRIVER_MANAGER = WebDriverManager()
# 
# Path: wtframework/wtf/web/webelement.py
# class WebElementSelector():
# 
#     "Utiltiy class for selecting elements."
# 
#     @staticmethod
#     def find_element_by_selectors(webdriver, *selectors):
#         """
#         Utility method makes it easier to find an element using multiple selectors. This is 
#         useful for problematic elements what might works with one browser, but fail in another.
#         (Like different page elements being served up for different browsers)
# 
#         Args:
#             selectors - var arg if N number of selectors to match against.  Each selector should 
#                         be a Selenium 'By' object.
# 
#         Usage::
#             my_element = WebElementSelector.find_element_by_selectors(webdriver,
#                                                                     (By.ID, "MyElementID"),
#                                                                     (By.CSS, "MyClassSelector") )
# 
#         """
#         # perform initial check to verify selectors are valid by statements.
#         for selector in selectors:
#             (by_method, value) = selector
#             if not WebElementSelector.__is_valid_by_type(by_method):
#                 raise BadSelectorError(
#                     u("Selectors should be of type selenium.webdriver.common.by.By"))
#             if type(value) != str:
#                 raise BadSelectorError(
#                     u("Selectors should be of type selenium.webdriver.common.by.By"))
# 
#         selectors_used = []
#         for selector in selectors:
#             (by_method, value) = selector
#             selectors_used.append(
#                 u("{by}:{value}").format(by=by_method, value=value))
#             try:
#                 return webdriver.find_element(by=by_method, value=value)
#             except:
#                 pass
# 
#         raise ElementNotSelectableException(
#             u("Unable to find elements using:") + u(",").join(selectors_used))
# 
#     @staticmethod
#     def __is_valid_by_type(by_type):
#         for attr, value in By.__dict__.iteritems():
#             if "__" not in attr:
#                 if by_type == value:
#                     return True
# 
#         return False
# 
# class BadSelectorError(Exception):
# 
#     "Raised when a bad selector is passed into a WebElementSelector() method."
#     pass


# Path: wtframework/wtf/web/tests/test_webelement_selector.py
from selenium.common.exceptions import ElementNotSelectableException
from selenium.webdriver.common.by import By
from wtframework.wtf.utils.test_utils import do_and_ignore
from wtframework.wtf.web.webdriver import WTF_WEBDRIVER_MANAGER
from wtframework.wtf.web.webelement import WebElementSelector, BadSelectorError
import unittest2

#    along with WTFramework.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################



class TestWebElementSelector(unittest2.TestCase):

    '''
    Unit test of the WebElementSelector Class
    '''

    driver = None

    def setUp(self):
        self.driver = WTF_WEBDRIVER_MANAGER.new_driver(
            "TestWebElementSelector")

    def tearDown(self):
        do_and_ignore(lambda: WTF_WEBDRIVER_MANAGER.close_driver())

    def test_find_element_by_selectors_with_GoodSelectorFirst(self):
        self.driver.get("http://www.google.com")

        element = WebElementSelector.find_element_by_selectors(self.driver,
                                                               (By.NAME, "q"))
        self.assertIsNotNone(element)

    def test_find_element_by_selectors_with_GoodSelector2nd(self):
        self.driver.get("http://www.google.com")


        element = WebElementSelector.find_element_by_selectors(self.driver,