第一个python测试脚本

上一篇 / 下一篇  2011-04-29 16:45:43 / 个人分类:python

# encoding=gbk

from selenium import selenium
import unittest, time, re

class signin(unittest.TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*chrome", "http://192.168.0.80:9003/secure/login")
        self.selenium.start()
    
    def test_signin(self):
        sel = self.selenium
        sel.open("/secure/login")
        sel.type("username", "admin")
        sel.type("password", "admin")
        sel.click("signin")
        sel.wait_for_page_to_load("30000")
        self.assertEqual("admin [注销]", sel.get_text("tools"))   
       
    def tearDown(self):
        self.selenium.close()

if __name__ == "__main__":
    unittest.main()


get_text("tools")中tools的来源见下面html文本中加粗红色字体:
<divid="header">
<divid="logo">
守护进程. <span>后台管理系统
</div>
<ulid="tools">
<li>
admin <ahref="/secure/logout">[注销]</a> </li>
</ul>
<ulid="top-navigation">
<liclass="active"><span><span><ahref="/agentconfigs/show">系统设置</a></span></span></li>
<liclass=""><span><span><ahref="/smsadvertisings/list">广告和任务</a></span></span></li>
<liclass=""><span><span><ahref="#">用户管理</a></span></span></li>
<liclass=""><span><span><ahref="/agent/statistics/channel">统计</a></span></span></li>
</ul>
 </div>

高兴,其中 get_text()函数的使用费了好大的劲,不懂里面的locator参数的含义,在此注释,以备后面理解其他函数,该函数原型:
    def get_text(self,locator):
        """
        Gets the text of an element. This works for any element that contains
        text. This command uses either the textContent (Mozilla-like browsers) or
        the innerText (IE-like browsers) of the element, which is the rendered
        text shown to the user.
       
        'locator' is an element locator
        """
        return self.get_string("getText", [locator,])
看了半天,不知道locator到底需要传啥,以为是网址,试了不对,后台从
sel.get_eval("this.browserbot.getCurrentWindow().document.getElementById('logo').innerHTML")函数中getElementById('logo')得到灵感,locator可能是页面标签的id,尝试了下,ok!
注:目前发现该locate只支持div、ul标签的id,p,a标签的id都不支持,不知道为啥?
我使用了一个以下标签p标签的id:
<pid="crudListAdd"><ahref="/channels/blank">添加 渠道</a></p>
他提示:Exception: ERROR: Element crudListAdd not found

注:原因最终被我找到了,是因为页面还没加载完,在动作之后需要等待
(self.sel.wait_for_page_to_load("60000")),
其实他任何标签的id都是支持的,包括h标签。

"IndentationError: unexpected indent"错误最大可能是python缩进不对,在使用中已多次遇到该情况,避免该问题出现的办法是使用同一种风格的缩进方式,比如只用tab键。



TAG:

引用 删除 shuisutang   /   2012-06-05 10:13:57
研究了一天了 今天继续
哎 啥时候能写自己的第一段python脚本撒~
引用 删除 shuisutang   /   2012-06-05 10:13:13
 

评分:0

我来说两句

Open Toolbar