Exemplo n.º 1
0
class TestXpathBrowser(unittest.TestCase):
    def setUp(self):
        # We need to enter "single test level" of life for each test
        # It will initialize the webdriver if no webdriver is present from upper levels
        self._level_mngr = WebdriverManager().enter_level(level=SINGLE_TEST_LIFE)
        # Get Xpath browser
        self.browser = self._level_mngr.get_xpathbrowser(name=__name__)

    def tearDown(self):
        # Make sure we quit those webdrivers created in this specific level of life
        self._level_mngr.exit_level()

    def test_select(self):
        # Load a local page for the demo
        self.get_local_page('xpath_browser_demo.html')
        # Do 2 type of selection
        self.browser.select_xpath('//div') # Xpath must be present, but no inner element may be returned
        self.browser.select_xsingle('//div') # Xpath must be present and at least 1 element must be present
        
    def test_extract(self):
        # Load a local page for the demo
        self.get_local_page('xpath_browser_demo.html')
        # Do 2 type of selection
        self.browser.select_xpath('//div') # Xpath must be present, but no inner element may be returned
        self.browser.extract_xsingle('//div') # Xpath must be present and at least 1 element must be present        

    def get_local_page(self, file_name):
        # Auxiliary method
        root_dir = os.path.abspath(os.path.dirname(__file__))
        url = 'file://' + os.path.join(root_dir, 'html', file_name)
        self.browser.get_url(url)
Exemplo n.º 2
0
class XpathShell(object):
    def __init__(self, logger=None):
        self.log = logger or Logger('Xpath Shell')

    def get(self, url):
        u = urlparse.urlparse(url)
        if not u.scheme:
            u = ('http', u.netloc, u.path, u.params, u.query, u.fragment)
            url = urlparse.urlunparse(u)
        self.browser.get_url(url)
        self.log.i('Current url: %r' % self.browser.current_url())

    def run_shell(self, url=None):
        self.level_mngr = WebdriverManager().enter_level(level=PROCESS_LIFE)
        browser = self.browser = self.level_mngr.get_xpathbrowser(name='Browser')
        #Aliases #TODO: add ipython extension
        ex  = extract = browser.extract_xpath
        exs = xsingle = browser.extract_xsingle
        get = self.get
        def reset_browser():
            self.level_mngr.exit_level()
            self.level_mngr = WebdriverManager().enter_level(level=PROCESS_LIFE)
        if url:
            self.get(url)
        print('Available objects/commands %s' % sorted(locals()))
        IpythonEmbedder().embed()
        self.level_mngr.exit_level()
        WebdriverManager().stop_display()
Exemplo n.º 3
0
 def run_shell(self, url=None):
     level_mngr = WebdriverManager().enter_level(level=PROCESS_LIFE)
     browser = self.browser = level_mngr.get_xpathbrowser(name='Browser')
     #Aliases #TODO: add ipython extension
     ex  = extract = browser.extract_xpath
     exs = xsingle = browser.extract_xsingle
     get = self.get
     if url:
         self.get(url)
     IpythonEmbedder().embed()
     level_mngr.exit_level()
     WebdriverManager().stop_display()
Exemplo n.º 4
0
class TestXpathBrowser(unittest.TestCase):
    def setUp(self):
        # We need to enter "single test level" of life for each test
        # It will initialize the webdriver if no webdriver is present from upper levels
        self.__level_mngr = WebdriverManager().enter_level(
            level=SINGLE_TEST_LIFE)
        # Get Xpath browser
        self.browser = self.__level_mngr.get_xpathbrowser(name=__name__)
        # Line above is equivalent to doing:
        #    from smoothtest.Logger import Logger
        #    from smoothtest.webunittest.XpathBrowser import XpathBrowser
        #    # Once we make sure there is a webdriver available, we acquire it
        #    # and block usage from other possible users
        #    webdriver = self.__level_mngr.acquire_driver()
        #    # Initialize the XpathBrowser class
        #    logger = Logger(__name__)
        #    self.browser = XpathBrowser('', webdriver, logger, settings={})

    def tearDown(self):
        # Make sure we quit those webdrivers created in this specific level of life
        self.__level_mngr.exit_level()

    def test_select(self):
        # Load a local page for the demo
        self.get_local_page('xpath_browser_demo.html')
        # Do 2 type of selection
        self.browser.select_xpath(
            '//div'
        )  # Xpath must be present, but no inner element may be returned
        self.browser.select_xsingle(
            '//div'
        )  # Xpath must be present and at least 1 element must be present

    def test_extract(self):
        # Load a local page for the demo
        self.get_local_page('xpath_browser_demo.html')
        # Do 2 type of selection
        self.browser.select_xpath(
            '//div'
        )  # Xpath must be present, but no inner element may be returned
        self.browser.extract_xsingle(
            '//div'
        )  # Xpath must be present and at least 1 element must be present

    def get_local_page(self, file_name):
        # Auxiliary method
        root_dir = os.path.abspath(os.path.dirname(__file__))
        url = 'file://' + os.path.join(root_dir, 'html', file_name)
        self.browser.get_url(url)
Exemplo n.º 5
0
class TestChat(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # It will initialize the webdriver if no webdriver is present from upper levels
        # Since we don't want to initialize a webdriver for each single test
        # we create the webdriver one level upper SINGLE_TEST_LIFE
        cls.cls_level_mngr = WebdriverManager().enter_level(level=TEST_ROUND_LIFE)

    @classmethod
    def tearDownClass(cls):
        cls.cls_level_mngr.exit_level()

    def setUp(self):
        # The webdriver is already initialized on previous level
        # We simply manage it's context with this new _level_mngr
        self._level_mngr = WebdriverManager().enter_level(level=SINGLE_TEST_LIFE)
        # Lock webdriver and build XpathBrowser API
        self.browser = self._level_mngr.get_xpathbrowser(name=__name__)
        self.browser.set_base_url(base_url)

    def tearDown(self):
        # Release webdriver, we are not longer using it.
        self._level_mngr.exit_level()

    def test_loaded(self):
        b = self.browser
        b.get_page('/')
        # Make sure there is at least 1 element
        b.select_xsingle(".//*[@id='input_area']")

    def test_send_msg(self):
        browser1 = self.browser
        path = '/'
        with WebdriverManager().enter_level(level=SINGLE_TEST_LIFE, name='Browser2') \
        as browser2:
            browser1.get_page(path)
            browser2.set_base_url(base_url)
            browser2.get_page(path)
            username = '******'
            message = 'my message'
            browser1.fill_input(".//*[@id='username']", username)
            browser1.fill_input(".//*[@id='message']", message)
            browser1.click(".//*[@id='input_area']/button")
            msgs_xpath = ".//*[@id='chat']/div"
            msg_recv = browser1.extract_xpath(msgs_xpath)[-1]
            self.assertEqual(msg_recv, '%s: %s' % (username, message))
            msg_recv2 = browser2.extract_xpath(msgs_xpath)[-1]
            self.assertEqual(msg_recv, msg_recv2)
Exemplo n.º 6
0
class SearchEnginesDemo(unittest.TestCase):
    def setUp(self):
        # We need to enter "single test level" of life for each test
        # It will initialize the webdriver if no webdriver is present from upper levels
        self._level_mngr = WebdriverManager().enter_level(level=SINGLE_TEST_LIFE)
        # Get Xpath browser
        self.browser = self._level_mngr.get_xpathbrowser(name=__name__)

    def tearDown(self):
        # Make sure we quit those webdrivers created in this specific level of life
        self._level_mngr.exit_level()

    def test_duckduckgo(self):
        # Load a local page for the demo
        self.browser.get_url('https://duckduckgo.com/')
        # Type smoothtest and press enter
        self.browser.fill(".//*[@id='search_form_input_homepage']", 'smoothtest\n')
        result_link = './/a[@title="smoothtest "]'
        # Wait for the result to be available
        self.browser.wait_condition(lambda brw: brw.select_xpath(result_link))
        # Click on result
        self.browser.click(result_link)
        # First result should point to github
        expected_url = 'https://github.com/joaduo/smoothtest'
        wait_url = lambda brw: brw.current_url() == expected_url
        # Make sure we end up in the right url
        self.assertTrue(self.browser.wait_condition(wait_url))

    def test_google(self):
        # go to google
        self.browser.get_url('https://www.google.com/')
        # search for smoothtest and press enter
        self.browser.fill(".//*[@id='lst-ib']", 'smoothtest\n')
        first_result = './/a[contains(text(),"smoothtest 0.1.3")]'
        # Wait for first result
        self.browser.wait_condition(lambda brw: brw.select_xpath(first_result))
        # Click first result
        self.browser.click(first_result)
        # Make sure we went to pypi
        wait_pypi_url = lambda brw: brw.current_url() == 'https://pypi.python.org/pypi/smoothtest/0.1.3'
        self.assertTrue(self.browser.wait_condition(wait_pypi_url))
Exemplo n.º 7
0
class TestXpathBrowser(unittest.TestCase):
    def setUp(self):
        # We need to enter "single test level" of life for each test
        # It will initialize the webdriver if no webdriver is present from upper levels
        self.__level_mngr = WebdriverManager().enter_level(level=SINGLE_TEST_LIFE)
        # Get Xpath browser
        self.browser = self.__level_mngr.get_xpathbrowser(name=__name__)
        # Line above is equivalent to doing:
        #    from smoothtest.Logger import Logger
        #    from smoothtest.webunittest.XpathBrowser import XpathBrowser
        #    # Once we make sure there is a webdriver available, we acquire it
        #    # and block usage from other possible users
        #    webdriver = self.__level_mngr.acquire_driver()
        #    # Initialize the XpathBrowser class
        #    logger = Logger(__name__)
        #    self.browser = XpathBrowser('', webdriver, logger, settings={})

    def tearDown(self):
        # Make sure we quit those webdrivers created in this specific level of life
        self.__level_mngr.exit_level()

    def test_select(self):
        # Load a local page for the demo
        self.get_local_page('xpath_browser_demo.html')
        # Do 2 type of selection
        self.browser.select_xpath('//div') # Xpath must be present, but no inner element may be returned
        self.browser.select_xsingle('//div') # Xpath must be present and at least 1 element must be present
        
    def test_extract(self):
        # Load a local page for the demo
        self.get_local_page('xpath_browser_demo.html')
        # Do 2 type of selection
        self.browser.select_xpath('//div') # Xpath must be present, but no inner element may be returned
        self.browser.extract_xsingle('//div') # Xpath must be present and at least 1 element must be present        

    def get_local_page(self, file_name):
        # Auxiliary method
        root_dir = os.path.abspath(os.path.dirname(__file__))
        url = 'file://' + os.path.join(root_dir, 'html', file_name)
        self.browser.get_url(url)