如何同步Django的“Client”和Selenium的webdriver之间使用的html / session [英] How can I sync the html/session used between Django's 'Client' and Selenium's webdriver

查看:109
本文介绍了如何同步Django的“Client”和Selenium的webdriver之间使用的html / session的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的terrain.py中,我已经有以下内容:

我正在尝试测试登录的用户可以使用生菜,Selenium和lettuce_webdriver在Django网站上注销。

  @ before.all 
def setup_browser():
profile = webdriver.FirefoxProfile()
profile.set_preference('network.dns.disableIPv6',True)
world.browser = webdriver.Firefox(配置文件)
world.client =客户端(HTTP_USER_AGENT ='Mozilla / 5.0')

然后当我登录时:

  @step(r'我以(\w *)'身份登录)
def log_in(step,name):
world.client.login (username = name,password = name)

我去我的网站:

 我去localhost:8000
我找到一个名为注销的链接那就去/ logout

@step(r'我找到一个名为(。*?)的链接去(。*?)$')
def find_link(step,link_name,link_url):
print(world.browser.page_source)
elem = world.browser.find_element_by_xpath(r'// a [@href =%s]'%link_url )
eq_(elem.text,link_name)

但是我的page_source显示我没有记录在这种有意义的情况下,在客户端浏览器中不会互相交谈。但是这是否可能,或者我需要通过点击selenium等链接登录



我想这样做:

  world.browser.page_source = world.client.get(world.browser.current_url).content 
/ pre>

但是,page_source无法更改。我可以从django的客户端喂Selenium吗?



编辑:遵循 Loius' 在下面的建议,我的我以...登录的步骤如下。我添加了if / else来检查我的怀疑。我的客户端仍然如上所述(请参阅 setup_browser 步骤)

  @step(r'我以(\w *)'身份登录)
def log_in(step,name):
world.client.login(username = name,password = name)
if world.client.cookies:
session_key = world.client.cookies [sessionid]。value
world.browser.add_cookie({'name':'sessionid','value ':session_key})
world.browser.refresh()
else:
raise异常(No Cookies!)

我看到的所有建议都是先登录。没有我的支票,我得到这个:

 场景:登录用户可以注销#\gantt_charts\features\index。特征:12 
鉴于我以elsepeth身份登录#\gantt_charts\features\steps.py:25
追溯(最近的最后一次呼叫):
文件C: \Python34\lib\site-packages\lettuce\core.py,第144行,__call__
ret = self.function(self.step,* args,** kw)
文件D:\Django_Projects\gAnttlr\gantt_charts\features\steps.py,第27行,log_in
session_key = world.client.cookies [sessionid]。value
KeyError:'sessionid'


解决方案

做你正在努力做的事情,但我做了类似的事情。在您使用您的客户端实例登录Django站点后,您需要做的是在Selenium实例上设置一个这样的cookie:

  driver.add_cookie({'name':'sessionid','value':session_key})

该名称应等于Django网站上的 SESSION_COOKIE_NAME 设置( sessionid 是默认值)。您需要找出 session_key 的值。



您可以从客户端实例获取此类信息:

  session_key = client.cookies [sessionid] value 

请注意,如果 SESSION_COOKIE_SECURE True ,Selenium将无法为某些浏览器设置 / code>。您应该为您的生产服务器设置此设置为 True ,但如果您希望Selenium测试设置您的会话cookie,则必须使其 False 用于测试。



一旦你的Selenium实例有cookie,它会看起来像Django,就像你已经用Selenium一样登录。正如我所说,我在我的测试套件中做了类似的 。 (我使用的东西不是客户端,但原则是一样的。)


I'm trying to test that logged in users can log out on my Django site with Lettuce, Selenium and lettuce_webdriver.

In my terrain.py I have:

@before.all
def setup_browser():
    profile = webdriver.FirefoxProfile()
    profile.set_preference('network.dns.disableIPv6', True)
    world.browser = webdriver.Firefox(profile)
    world.client = Client(HTTP_USER_AGENT='Mozilla/5.0')

And then when I 'login':

@step(r'I am logged in as "(\w*)"')
def log_in(step, name):
    world.client.login(username=name, password=name)

And I go to my site:

And I go to "localhost:8000"
    I find a link called "Logout ?" that goes to "/logout"

@step(r'I find a link called "(.*?)" that goes to "(.*?)"$')
def find_link(step, link_name, link_url):
    print(world.browser.page_source)
    elem = world.browser.find_element_by_xpath(r'//a[@href="%s"]' % link_url)
    eq_(elem.text, link_name)

But my page_source shows I am not logged in. This kind of makes sense...in that client and browser aren't talking to each other. But is this possible, or do I need to login 'manually' by clicking links with selenium etc?

I'd like to do this:

 world.browser.page_source = world.client.get(world.browser.current_url).content

But page_source can't be changed. Can I feed Selenium from django's client some how?

Edit: Following Loius' advice below, my 'I am logged in as ...' step is as below. I added the if/else to just check my suspicions. My client is still setup as above (see setup_browser step above)

@step(r'I am logged in as "(\w*)"')
def log_in(step, name):
    world.client.login(username=name, password=name)
    if world.client.cookies:
        session_key = world.client.cookies["sessionid"].value
        world.browser.add_cookie({'name':'sessionid', 'value':session_key})
        world.browser.refresh()
    else: 
        raise Exception("No Cookies!")

All the advice I've seen is to login first. Without my check, I get this:

  Scenario: Logged in users can logout                       # \gantt_charts\features\index.feature:12
    Given I am logged in as "elsepeth"                                # \gantt_charts\features\steps.py:25
    Traceback (most recent call last):
      File "C:\Python34\lib\site-packages\lettuce\core.py", line 144, in __call__
        ret = self.function(self.step, *args, **kw)
      File "D:\Django_Projects\gAnttlr\gantt_charts\features\steps.py", line 27, in log_in
        session_key = world.client.cookies["sessionid"].value
    KeyError: 'sessionid'

解决方案

I've not tried doing exactly what you are trying to do but I've done something similar. What you need to do is set a cookie like this on your Selenium instance after you've logged into your Django site with your Client instance:

driver.add_cookie({'name': 'sessionid', 'value': session_key})

The name should be equal to your SESSION_COOKIE_NAME setting on the Django site (sessionid is the default). You need to figure out the value of session_key.

You can obtain it from a Client instance like this:

    session_key = client.cookies["sessionid"].value

Note that Selenium won't be able to set the cookie for some browsers if SESSION_COOKIE_SECURE is True. You should have this setting be True for your production server but if you want your Selenium tests to set your session cookie, then you have to make it False for testing.

Once your Selenium instance has the cookie, it will look to Django as if you had logged into it with Selenium. As I said, I do something similar in my test suites. (I use something else than Client but the principle is the same.)

这篇关于如何同步Django的“Client”和Selenium的webdriver之间使用的html / session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆