zoukankan      html  css  js  c++  java
  • Selenium RC For Python:教程2

    为了全面测试一个Web系统,我们需要与系统UI相交互并做出相应的断言。

    最常用的交互是通过selenium.py中以下方法来实现的:

    1. open(url): Opens an URL in the test frame. This accepts both relative and absolute URLs. 
    2. 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.
    3. 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.
    4. select(selectLocator,optionLocator): Select an option from a drop-down using an option locator.
    5. check(locator): Check a toggle-button (checkbox/radio)
    6. wait_for_page_to_load(timeout): Waits for a new page to load.

    下面是一些从页面中获取信息的方法:

    1. get_title(): Gets the title of the current page.
    2. 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.
    3. 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.
    4. 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.
    5. is_element_present(locator):  Verifies that the specified element is somewhere on the page.
    6. get_selected_label(selectLocator): Gets option label (visible text) for selected option in the specified select element.
    7. get_selected_value(selectLocator): Gets option value (value attribute) for selected option in the specified select element.
    8. is_something_selected(selectLocator): Determines whether some option in a drop-down menu is selected.
    9. 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.
    10. 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方法运行测试

    代码例子如下:

    代码
    #! /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()


    作者:Shane
    出处:http://bluescorpio.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    wzplayer2 支持mac 了,最新谍报
    关于duilib的理解
    DMS的实现转载
    视频通话最新谍报
    新人补钙系列教程之:Function类的重要方法apply()
    新人补钙系列教程之:webgame好友模块原型开发一
    新人补钙系列教程之: 大型 webGame 开发系列之 pipes
    新人补钙系列教程之:模拟java多线程Thread类
    flash学习网站
    新人补钙系列教程之:AS3与服务器通信
  • 原文地址:https://www.cnblogs.com/bluescorpio/p/1741553.html
Copyright © 2011-2022 走看看