2011-01-12 72 views
3

我正在使用硒與掛架來測試用戶交互。一切都很好,直到我添加了一個認證用戶界面的測試 - 一個包含用戶名和密碼的登錄界面。看來,Chrome(我在硒下使用的瀏覽器)彈出一個對話框來請求是否存儲憑據。在這之後所有的測試都失敗了。在硒中測試驗證UI

有沒有人有任何使用硒認證/用戶界面,可能會彈出對話框指針?我知道this selenium warning。 「不要這樣做,而是做另一件事」將是一個可以接受的答案。


項目/測試/ lib中/硒/ login_page.py

""" 
LoginPage represents the page where users are authenticated for the application 
""" 


class LoginPage(object): 
    """This is the LoginPage class.""" 

    login = '/authentication/login' 
    logout = '/authentication/logout' 
    home = '/home' 

    def __init__(self, browser): 
     self._browser = browser 

    def goto_login(self, baseurl): 
     return self._browser.go_to(baseurl + self.login) 

    def goto_logout(self, baseurl): 
     return self._browser.go_to(baseurl + self.logout) 

    def goto_home(self, baseurl): 
     return self._browser.go_to(baseurl + self.home) 

    def enter_credentials(self, username, password): 
     element_by_id = self._browser.get_element_by_id 
     element_by_id("login").value = username 
     element_by_id("password").value = password 

    def submit_form(self): 
     element = self._browser.get_element_by_id('submit') 
     return self._browser.click(element) 

項目/測試/硒/ test_login_page.py

"""Tests the login page.""" 

import nose.tools as nt 
from nose.plugins.skip import SkipTest 

from assess.tests.selenium import SeleniumTestBase 
from assess.tests.lib.selenium.login_page import LoginPage 


class TestLoginPage(SeleniumTestBase): 
    """Tests the login page.""" 

    def _login_page(self): 
     return self.baseurl + '/authentication/login' 

    def setUp(self): 
     nt.set_trace() 
     super(TestLoginPage, self).setUp() 
     self.page = LoginPage(self.browser) 

    def tearDown(self): 
     super(TestLoginPage, self).tearDown() 
     self.page = None 

    def test_login_page_fail(self): 
     # Logout before beginning test 
     self.page.goto_logout(self.baseurl) 

     self.page.goto_login(self.baseurl) 
     nt.assert_true(self.browser.get_url().startswith(self._login_page())) 
     self.page.enter_credentials('foo', 'random') 
     self.page.submit_form() 

    def test_login_page_success(self): 
     # Logout before beginning test 
     self.page.goto_logout(self.baseurl) 

     self.page.goto_login(self.baseurl) 
     nt.assert_true(self.browser.get_url().startswith(self._login_page())) 
     self.page.enter_credentials('user', 'good-password') 
     self.page.submit_form() 

項目/模板/ login.html.mako

<%inherit file="/layout.html.mako" /> 

${h.stylesheet_link('login.css')} 

<form action="/__do_login" method="POST"> 
    <fieldset> 
     <p> 
     <label for="login">User name</label><br /> 
     <input id="login" name="login" type="text" maxlength="40" value="" /> 
     </p> 
     <p> 
     <label for="password">Password</label><br /> 
     <input id="password" name="password" type="password" maxlength="40" value="" /> 
     </p> 
     <p class="submit"> 
     <input id="submit" type="submit" value="Submit" /> 
     </p> 
    </fieldset> 
</form> 

etc/who.ini

[general] 
request_classifier = repoze.who.classifiers:default_request_classifier 
challenge_decider = repoze.who.classifiers:default_challenge_decider 

[identifiers] 
plugins = foo 
      friendly_form;browser 

[authenticators] 
plugins = foo 

[challengers] 
plugins = friendly_form;browser 
      foo 

[plugin:foo] 
use = ... 
host = ... 
logout_path_regex = /authentication/logout 
httponly = True 

[plugin:friendly_form] 
use = repoze.who.plugins.friendlyform:FriendlyFormPlugin 
login_form_url = /authentication/login 
login_handler_path = /__do_login 
post_login_url = /home 
logout_handler_path = /authentication/logout 
post_logout_url = /authentication/login 

項目/配置/ routing.py

def make_map(config): 
    """Create, configure and return the routes Mapper""" 
    mapper = Mapper(directory=config['pylons.paths']['controllers'], 
       always_scan=config['debug']) 
    mapper.minimization = False 
    mapper.explicit = False 

    # The ErrorController route (handles 404/500 error pages); it should 
    # likely stay at the top, ensuring it can always be resolved 
    mapper.connect('/error/{action}', controller='error') 
    mapper.connect('/error/{action}/{sid}', controller='error') 

    # CUSTOM ROUTES HERE 

... 
    mapper.connect('/authentication/login', 
        controller='authentication', 
        action='index') 
    mapper.connect('/authentication/logout', 
        controller='authentication', 
        action='logout') 

項目/控制器/ authentication.py

""" 
This module contains the login controller. 
""" 

import logging 

from pylons.controllers.util import redirect 
from pylons import url, tmpl_context as c, request 

from project.lib.base import BaseController 
from project.lib.authorize import user_is_authenticated 

logger = logging.getLogger(__name__) 


class AuthenticationController(BaseController): 
    """ This controller serves the login page.""" 
    template = '/login.html.mako' 

    def index(self): 
     return self.render(self.template) 

    def validate(self): 
     """ render a login page if we're not logged in """ 
     c.came_from = request.params.get("came_from", url("home")) 

     # If we're already authenticated, redirect us to where we started. 
     if user_is_authenticated(): 
      msg = "User is authenticated: redirecting to %s" % c.came_from 
      logger.info(msg) 
      redirect(c.came_from) 

     msg = "User is not authenticated: rendering %s" % self.template 
     logger.info(msg) 
     return self.render(self.template) 

項目/ LIB/authorize.py

''' Helper functions for the authorization and authentication mechanisms. ''' 
from pylons import request 
from decorator import decorator 
from pylons.controllers.util import abort 


def user_is_authenticated(): 
    """ Returns True if is authenticated, else returns False. 
    """ 
    identity = request.environ.get('repoze.who.identity') 
    return identity and 'xxx' in identity 


@decorator 
def authenticated(func, *args, **kwargs): 
    """ Check if is authenticated. If not authenticated, abort with 
     401 status. 
    """ 
    if not user_is_authenticated(): 
     abort(401, 'You are not authenticated') 
    return func(*args, **kwargs) 
+0

不通常Chrome會打開一個欄在「Save password」或「Never for this site」按鈕的頁面頂部?這些是本機控制,所以沒有太多的事情要做。你有沒有發現一個網站,彈出一個阻止對話框? – pnewhook 2011-01-12 17:50:04

+0

這不是一個阻塞對話框,但硒似乎使鉻在一個新的選項卡,並且所有後續測試失敗,因爲硒找不到按鈕,輸入元素輸入文本等等。我相信,在我的開發機器上,我必須說「從不爲這個網站」(我甚至不認爲這有幫助),但我們運行的測試版服務器並不是。所有真的很煩人。 – hughdbrown 2011-01-12 17:59:25

回答

1

在理想情況下你應該使用一個配置文件進行測試,並在這個p rofile你應該指定永遠不要求保存憑據。不幸的是,使用Selenium 1指定一個自定義的Chrome配置文件是不可能的,但是可以在Selenium 1中使用Firefox或者轉移到Selenium 2(WebDriver) - 有關更多信息,請參閱this thread