zoukankan      html  css  js  c++  java
  • A Selenium and Python appetizer

    A Selenium and Python appetizer - Andi Albrecht

    A Selenium and Python appetizer

    Selenium is a browser automation framework and was released in version 2.0.0 just this week. In this post I'll show you how easy it is to get started with using this fine framework in Python.
    Peparing the kitchen

    Let's create and jump into a virtualenv. You don't really need it, but at least we don't need no sudo for anything :)

    $ virtualenv demo
    New python executable in demo/bin/python
    Installing distribute..................................................done.
    $ source demo/bin/activate
    (demo) $

    Installing the Selenium Python package is as easy as possible. Just use your favorite package manage and install it from PyPI:

    (demo) $ pip install selenium

    This installs the selenium Python package which includes all necessary drivers to run your browser automation using Firefox, Chrome or IE locally or anything that the Selenium Remote Control provides. We come back to that later.

    If you want to automate Google's Chrome on your local machine some little setup is needed. If you want to follow the examples in this post and if you want to use an installed Firefox feel free to skip this section. To automate Chrome make sure Chrome is installed and then download and unzip the chromedriver for your system from the Selenium download page. Place the chromedriver binary somewhere in your $PATH. That's it.
    Is it Google?

    Now that Selenium is up and running, let's write a simple script that verifies that the title of the Google page actually contains the word "Google". That's simple and obvious, but the script illustrates how to use Selenium in UnitTests.

    And you can do all this fancy stuff interactive in the Python shell:

    #!/usr/bin/env python

    import unittest

    from selenium import webdriver


    class TestTitle(unittest.TestCase):

    def setUp(self):
    # Toggle comments to test with a different browser
    self.driver = webdriver.Chrome()
    #self.driver = webdriver.Firefox()
    #self.driver = wevdriver.Ie()

    def tearDown(self):
    self.driver.close()

    def test_title_tag(self):
    self.driver.get('http://google.com')
    title_tag = self.driver.find_element_by_tag_name('title')
    self.assertEqual(title_tag.text, 'Google')


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

    This script has one little drawback. It initializes a new webdriver (aka browser) for each tests. That's very accurate to start with a fresh browser for each test, but very time consuming too. As an alternative you can modify this script to use a global webdriver instance that is initialized before unittest.main() and closed after that line.

    Since webdriver uses a network protocol to communicate with the browser, it's painless to use it in an interactive shell:

    (demo) $ python
    Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
    >>> from selenium import webdriver
    >>> driver = webdriver.Chrome()
    >>> driver.get('http://google.com')
    >>> driver.find_element_by_tag_name('title')


    That's a nice and easy way to figure out what webdriver commands are available and how they work.
    Going headless

    When using Selenium as part of your test suite, you don't have always an X environment around to start Firefox or Chrome. As you can see in the sample script above, it's easy to switch between different browsers without the need to change anything in your actual tests.

    As the name suggests the remote webdriver talks to a Remote Control Server that let's you choose a browser. The nice thing is that the standalone server comes with a headless browser that even supports JavaScript execution much like Firefox or Chrome.

    Download the standalone server JAR on the Selenium download page and simply run java -jar selenium-server-standalone-2.0.0.jar to start it. When the server is started you can initialize a headless browser in your scripts like this:

    from selenium import webdriver
    driver = webdriver.Remote(
    command_executor= 'http://127.0.0.1:4444/wd/hub', # that's the default
    desired_capabilities={'browserName': 'htmlunit',
    'version': '2',
    'javascriptEnabled': True})

    That's all to run your tests using a headless browser. Ready for your continuous integration system running on a server somewhere.

    Testing web pages is fun, isn't it?

    Edit 2011-07-12: Corey Goldberg posted a very similar article on his blog a few hours before I published this post.


  • 相关阅读:
    NX二次开发-创建直线(起点-向量方向-长度)UF_CURVE_create_line
    NX二次开发-创建圆弧(圆心-半径)UF_CURVE_create_arc
    NX二次开发-UFUN获取直线的两个端点UF_CURVE_ask_line_data
    NX-二次开发创建圆弧(三点圆弧)UF_CURVE_create_arc_3point
    NX二次开发-创建直线UF_CURVE_create_line与NXOpen->CreateLine
    NX二次开发-创建圆弧(起点-终点-半径)UF_CURVE_create_arc_point_point_radius
    NX二次开发-UFUN打开信息窗口UF_UI_open_listing_window()
    NX二次开发-UFUN信息窗口打印UF_UI_write_listing_window
    NX二次开发-UFUN单按钮模态对话框窗口打印uc1601用法
    NX二次开发-UFUN打开二进制STL文件函数UF_STD_open_binary_stl_file
  • 原文地址:https://www.cnblogs.com/lexus/p/2225151.html
Copyright © 2011-2022 走看看