Exemple #1
0
 def test_forward_to_none_page(self):
     "should not fail when trying to forward to none"
     browser = Browser('zope.testbrowser')
     browser.visit(EXAMPLE_APP)
     browser.forward()
     self.assertEqual(EXAMPLE_APP, browser.url)
     browser.quit()
 def test_forward_to_none_page(self):
     "should not fail when trying to forward to none"
     browser = Browser('django')
     browser.visit(EXAMPLE_APP)
     browser.forward()
     self.assertEqual(EXAMPLE_APP, browser.url)
     browser.quit()
Exemple #3
0
class BrowserTester:
    def __init__(self, browser):
        self.browser = Browser(browser)
        self.baseUrl = ''
        self.expectedUrl = ''
        self.status = TEST_PENDING

    def visitUrl(self, url):
        self.browser.visit(url)
        self.baseUrl = url
        self.setTerminalColors(NORMAL_WHITE_FONT)

    def setTerminalColors(self, style):
        print(style, end='')

    def verifySuccessfulPageLoad(self):
        try:
            assert self.isCorrectPageSuccessfullyLoaded()
            self.status = TEST_PASSED
        except Exception:
            printErrorMessage("[ERROR] Page is not successfully loaded.")
            self.status = TEST_FAILED
            self.quit()

    def isCorrectPageSuccessfullyLoaded(self):
        return self.isUrlCorrect() and self.isHttpStatusSuccess()

    def isUrlCorrect(self):
        return self.expectedUrl == self.browser.url

    def isHttpStatusSuccess(self):
        httpRequest = requests.get(self.browser.url)
        return httpRequest.status_code == requests.codes.ok

    def back(self):
        self.browser.back()

    def forward(self):
        self.browser.forward()

    def quit(self):
        self.browser.quit()

    def result(self, testName):
        if (self.status == TEST_PASSED):
            self.setTerminalColors(STYLED_PASSED)

        elif (self.status == TEST_PENDING):
            self.setTerminalColors(STYLED_PENDING)

        # TEST_FAILED is handled in exception
        print(testName)

    def printErrorMessage(self, message):
        self.setTerminalColors(STYLED_FAILED)
        print(message)
class SplinterBrowserDriver(BaseBrowserDriver):
    """
        This is a BrowserDriver for splinter
        (http://splinter.cobrateam.info)
        that implements the BaseBrowserDriver API.

        To use it, you must have splinter installed on your env.

        For itself it's a browser driver that supports multiple browsing
        technologies such as selenium, phantomjs, zope, etc.
    """

    driver_name = 'splinter'

    def __init__(self):
        super(SplinterBrowserDriver, self).__init__()
        if not splinter_available:
            raise ImportError(
                "In order to use splinter Base Driver you have to install it. "
                "Check the instructions at http://splinter.cobrateam.info")
        self._browser = Browser(config.default_browser)

    def _handle_empty_element_action(self, element):
        if not element:
            raise ActionNotPerformableException(
                "The action couldn't be perfomed because the element couldn't "
                "be found; Try checking if your element"
                "selector is correct and if the page is loaded properly.")

    @property
    def page_url(self):
        return self._browser.url

    @property
    def page_source(self):
        return self._browser.html

    @property
    def page_title(self):
        return self._browser.title

    def open_url(self, url):
        self._browser.driver.get(url)

    def quit(self):
        return self._browser.quit()

    def is_element_visible(self, element):
        return element.visible

    def get_element_text(self, element):
        return element.text

    def get_element_by_xpath(self, selector):
        return self._browser.find_by_xpath(selector)

    def get_element_by_css(self, selector):
        return self._browser.find_by_css(selector)

    def get_element_by_id(self, selector):
        return self._browser.find_by_id(selector)

    def get_element_by_tag(self, selector):
        return self._browser.find_by_tag(selector)

    @element_action
    def type(self, element, text, slowly=False):
        return element.type(text, slowly)

    @element_action
    def fill(self, element, text):
        return element.fill(text)

    @element_action
    def clear(self, element):
        self.fill(element, '')

    @element_action
    def click(self, element):
        return element.click()

    @element_action
    def check(self, element):
        return element.check()

    @element_action
    def uncheck(self, element):
        return element.uncheck()

    @element_action
    def mouse_over(self, element):
        return element.mouse_over()

    @element_action
    def mouse_out(self, element):
        return element.mouse_out()

    def reload(self):
        return self._browser.reload()

    def go_back(self):
        return self._browser.back()

    def go_forward(self):
        return self._browser.forward()

    def execute_script(self, script):
        """This method is deprecated. Use `execute_javascript` instead.
        """
        return self._browser.evaluate_script(script)

    def execute_javascript(self, script):
        return self._browser.evaluate_script(script)

    def get_iframe(self, iframe_id):
        return self._browser.get_iframe(iframe_id)

    def get_alert(self):
        return self._browser.get_alert()

    def attach_file(self, input_name, file_path):
        return self._browser.attach_file(input_name, file_path)

    def wait_pageload(self, timeout=30):
        wait_interval = 0.05
        elapsed = 0

        while self.execute_javascript('document.readyState') != 'complete':
            self.wait(wait_interval)
            elapsed += wait_interval

            if elapsed > timeout:
                raise PageNotLoadedException

    def click_and_wait(self, element, timeout=30):
        self.click(element)
        self.wait_pageload(timeout)
Exemple #5
0
class SplinterBrowserDriver(BaseBrowserDriver):
    """
        This is a BrowserDriver for splinter
        (http://splinter.cobrateam.info)
        that implements the BaseBrowserDriver API.

        To use it, you must have splinter installed on your env.

        For itself it's a browser driver that supports multiple browsing
        technologies such as selenium, phantomjs, zope, etc.
    """

    driver_name = 'splinter'

    def __init__(self):
        super(SplinterBrowserDriver, self).__init__()
        if not splinter_available:
            raise ImportError(
                "In order to use splinter Base Driver you have to install it. "
                "Check the instructions at http://splinter.cobrateam.info")
        self._browser = Browser(config.default_browser)

    def _handle_empty_element_action(self, element):
        if not element:
            raise ActionNotPerformableException(
                "The action couldn't be perfomed because the element couldn't "
                "be found; Try checking if your element"
                "selector is correct and if the page is loaded properly.")

    @property
    def page_url(self):
        return self._browser.url

    @property
    def page_source(self):
        return self._browser.html

    @property
    def page_title(self):
        return self._browser.title

    def open_url(self, url):
        self._browser.driver.get(url)

    def quit(self):
        return self._browser.quit()

    def is_element_visible(self, element):
        return element.visible

    def get_element_text(self, element):
        return element.text

    def get_element_by_xpath(self, selector):
        return self._browser.find_by_xpath(selector)

    def get_element_by_css(self, selector):
        return self._browser.find_by_css(selector)

    def get_element_by_id(self, selector):
        return self._browser.find_by_id(selector)

    def get_element_by_tag(self, selector):
        return self._browser.find_by_tag(selector)

    @element_action
    def type(self, element, text, slowly=False):
        return element.type(text, slowly)

    @element_action
    def fill(self, element, text):
      return element.fill(text)

    @element_action
    def clear(self, element):
      self.fill(element, '')

    @element_action
    def click(self, element):
        return element.click()

    @element_action
    def check(self, element):
        return element.check()

    @element_action
    def uncheck(self, element):
        return element.uncheck()

    @element_action
    def mouse_over(self, element):
        return element.mouse_over()

    @element_action
    def mouse_out(self, element):
        return element.mouse_out()

    def reload(self):
        return self._browser.reload()

    def go_back(self):
        return self._browser.back()

    def go_forward(self):
        return self._browser.forward()

    def execute_script(self, script):
        return self._browser.evaluate_script(script)

    def get_iframe(self, iframe_id):
        return self._browser.get_iframe(iframe_id)

    def get_alert(self):
        return self._browser.get_alert()

    def attach_file(self, input_name, file_path):
        return self._browser.attach_file(input_name, file_path)

    def wait_pageload(self, timeout=30):
        wait_interval = 0.05
        elapsed = 0

        while self.execute_script('document.readyState') != 'complete':
            self.wait(wait_interval)
            elapsed += wait_interval

            if elapsed > timeout:
                raise PageNotLoadedException

    def click_and_wait(self, element, timeout=30):
        self.click(element)
        self.wait_pageload(timeout)
Exemple #6
0
from splinter import Browser

browser = Browser()

# visit site
browser.visit('http://google.com')
browser.visit('http://selenium-python.readthedocs.io/')
browser.visit('https://splinter.readthedocs.io/')

# browser navigation
browser.back()

# saving vars
titleOfBrowser = browser.title
urlOfBrowser = browser.url

browser.forward()

# testing wait_time
browser.is_text_present('splinter', wait_time=10)

# testing execution of simple script
#browser.execute_script("alert('Hello')")

browser.quit()

# should output the second site info
print titleOfBrowser
print urlOfBrowser
from splinter import Browser
from time import sleep

b = Browser()

b.visit('http://ddg.gg')

print(f'Título: {b.title}')
# print(f'html: {b.html}')
print(f'URL: {b.url}')

b.visit('http://google.com.br')
b.back()
sleep(3)
b.forward()