def setup_login(base_url, selenium: Remote):
    selenium.get(base_url)
    selenium.delete_all_cookies()
    file = join(dirname(dirname(__file__)), 'cookies.pkl')
    cookies = load(open(file, "rb"))
    for cookie in cookies:
        selenium.add_cookie(cookie)
Example #2
0
    def get_sso_session(
            self,
            selenium: webdriver.Remote,
            impersonate_user=settings.TEST_IMPERSONATE_USER
    ) -> webdriver.Remote:

        cookies = self.keycloak.impersonate(impersonate_user).cookies
        # Note(knikolla): We must open a page in order to set cookies.
        # Doesn't have to exist, but must be in same domain as SSO.
        selenium.get(f'{settings.KEYCLOAK_URL}/auth/test')
        for k in [x for x in cookies.keys() if 'KEYCLOAK' in x]:
            selenium.add_cookie({
                'name':
                k,
                'value':
                cookies[k],
                'domain':
                parse.urlparse(settings.KEYCLOAK_URL).netloc.split(':')[0],
            })

        selenium.get(f'{settings.KEYCLOAK_URL}/auth/realms/master/account/')

        selenium.implicitly_wait(5)
        username = selenium.find_element_by_xpath('//*[@id="username"]')
        assert username.get_attribute('value') == impersonate_user
        return selenium
def admin():
    rendered = ''
    if request.method == 'POST':
        url = request.form.get('url') or ''
        try:
            browser = Remote('http://selenium:4444/wd/hub', DesiredCapabilities.CHROME)
            # TODO: Replace with appropriate hostname and port
            browser.get(HOST)
            browser.add_cookie({'name': 'session', 'value': request.cookies.get('session')})
            browser.get(url)
            renderer_wait_js(browser)
            rendered = browser.page_source
        except Exception as e:
            pass
    return render_template('authenticated/admin.html', rendered=rendered)
Example #4
0
class Browser(object):
    def __init__(self, harness, selenium_server_url, selenium_browser, width=1024, height=600):
        self.harness = harness
        self.selenium_server_url = selenium_server_url
        self.selenium_browser = selenium_browser
        self.width = width
        self.height = height

        self.selenium = Remote(
            self.selenium_server_url.encode('ascii'),
            desired_capabilities = {
                'browserName': self.selenium_browser,
            },
        )
        self.selenium.set_window_size(width, height)

    def __enter__(self):
        self.harness.browser_stack.append(self)
        return self

    def __exit__(self, type, value, traceback):
        if self.harness.browser_stack[-1] != self:
            raise Exception("Unexpected browser on the top of the stack")
        self.harness.browser_stack.pop()
        return

    def shutdown(self):
        self.selenium.quit()

    def back(self):
        self.selenium.back()

    def refresh(self):
        self.selenium.refresh()

    def title(self):
        return self.selenium.title

    def source(self):
        return self.selenium.page_source

    def get(self, uri):
        self.selenium.get(uri)

    def find(self, selector):
        return WebElementSet(self, elements=self.selenium).find(selector)

    def add_cookie(self, cookie_dict):
        self.selenium.add_cookie(cookie_dict)

    def get_cookie(self, name):
        return self.selenium.get_cookie(name)

    def endpoint(self):
        return self.find('html').attr('id').replace('endpoint-', '').replace('-', '.')

    def endpoint_is(self, endpoint):
        is_equal(self.endpoint(), endpoint, "Endpoint is correct")

    def wait_for_bootstrap_modal(self):
        last = [None]

        def inner_wait(driver):
            value = self.find('.modal')[-1].css('top')
            if last[0] and last[0] == value:
                return True
            last[0] = value
            return False
        WebDriverWait(self.selenium, 3).until(inner_wait)
        return self

    def url(self):
        return furl(self.selenium.current_url)

    def execute_script(self, script):
        return self.selenium.execute_script(script)

    def wait_for_ajax(self):
        WebDriverWait(self.selenium, 10).until_not(lambda x: x.execute_script('return jQuery.active'))
        return self

    def wait_for_jquery(self):
        WebDriverWait(self.selenium, 10).until(lambda x: x.execute_script('return window.jQuery ? true : false'))
        return self

    def screenshot(self, message="Screenshot: "):
        if 's3_access_key' not in app.settings.options('test'):
            print "No screenshot S3 instance configured - skipping screenshot"
            return

        if not hasattr(self, 's3_connection'):
            if 's3_host' in app.settings.options('test'):
                self.s3_connection = boto.s3.connection.S3Connection(
                    app.settings.get('test', 's3_access_key'),
                    app.settings.get('test', 's3_secret_key'),
                    host = app.settings.get('test', 's3_host'),
                )
            else:
                self.s3_connection = boto.s3.connection.S3Connection(
                    app.settings.get('test', 's3_access_key'),
                    app.settings.get('test', 's3_secret_key'),
                )

        bucket = self.s3_connection.get_bucket(app.settings.get('test', 's3_bucket'))
        filename = "%s-%s.png" % (token.create_url_token(), self.harness.current_test_object.__class__.__name__)

        key = bucket.new_key(filename)
        key.metadata['Content-Type'] = 'image/png'
        key.metadata['Cache-Control'] = 'public, max-age=86400'
        key.set_contents_from_string(self.selenium.get_screenshot_as_png())
        key.make_public()
        print "%s%s" % (message, key.generate_url(expires_in=0, query_auth=False))