设为首页 收藏本站
查看: 1123|回复: 0

[经验分享] Selenium RC For Python:教程2

[复制链接]

尚未签到

发表于 2015-4-23 05:12:02 | 显示全部楼层 |阅读模式
  为了全面测试一个Web系统,我们需要与系统UI相交互并做出相应的断言。
  最常用的交互是通过selenium.py中以下方法来实现的:

  • open(url): Opens an URL in the test frame. This accepts both relative and absolute URLs.
  • click(locator): Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call    waitForPageToLoad.
  • type(locator, value):  Sets the value of an input field, as though you typed it in. Can also be used to set the value of combo boxes, check boxes, etc. In these cases, value should be the value of the option selected, not the visible text.
  • select(selectLocator,optionLocator): Select an option from a drop-down using an option locator.
  • check(locator): Check a toggle-button (checkbox/radio)
  • wait_for_page_to_load(timeout): Waits for a new page to load.
  下面是一些从页面中获取信息的方法:


  • get_title(): Gets the title of the current page.
  • get_text(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.
  • get_value(locator): Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter). For checkbox/radio elements, the value will be "on" or "off" depending on whether the element is checked or not.
  • is_editable(locator): Determines whether the specified input element is editable, ie hasn't been disabled. This method will fail if the specified element isn't an input element.
  • is_element_present(locator):  Verifies that the specified element is somewhere on the page.
  • get_selected_label(selectLocator): Gets option label (visible text) for selected option in the specified select element.
  • get_selected_value(selectLocator): Gets option value (value attribute) for selected option in the specified select element.
  • is_something_selected(selectLocator): Determines whether some option in a drop-down menu is selected.
  • is_checked(locator): Gets whether a toggle-button (checkbox/radio) is checked.  Fails if the specified element doesn't exist or isn't a toggle-button.
  • get_alert(): Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts.Getting an alert has the same effect as manually clicking OK. If an alert is generated but you do not consume it with getAlert, the next Selenium action       will fail. Under Selenium, JavaScript alerts will NOT pop up a visible alert dialog.
  如上一篇文章所写,大部分的测试类的结构是类似的:

  • from..import... or import..
  • class的定义
  • 常用变量的定义,引用这些变量要用self.常量的方式
  • setUp()和tearDown的定义
  • 测试方法的定义,这是测试的主要内容
  • 使用unittest的main方法运行测试
  代码例子如下:

DSC0000.gif DSC0001.gif 代码

#! /usr/bin/env python
#coding=utf-8
from selenium import selenium
import unittest
class TestPageForSeleniumRemoteControl(unittest.TestCase):
    MAX_WAIT_IN_MS = 60000
    BASE_URL = "http://www.bitmotif.com"
    TEST_PAGE_URL = BASE_URL + "/test-page-for-selenium-remote-control"
    TEST_PAGE_TITLE = u"Test Page For Selenium Remote Control « Bit Motif"
   
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox3 E:/Program Files/Mozilla Firefox/firefox.exe", self.BASE_URL)
        self.selenium.start()
        
    def tearDown(self):
        self.selenium.stop()
        
    def test_function(self):
        passs
      
if __name__ == '__main__':
    unittest.main()  

  
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59660-1-1.html 上篇帖子: Python图像处理库(PIL)--简介和基本概念 下篇帖子: python单例模式(转)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表