Selenium RC For Python: tutorial 2

Source: Internet
Author: User

To fully test a Web system, we need to interact with the system UI and make corresponding assertions.

The most common interaction is implemented through the following methods in 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 shocould 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.

The following are some methods to obtain information from the page:

  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.

As written in the previous article, the structure of most test classes is similar:

  • From... import... or import ..
  • Class Definition
  • Definition of common variables. to reference these variables, use the self. Constant method.
  • SetUp () and tearDown Definitions
  • Definition of the test method, which is the main content of the test
  • Run the test using the main method of unittest

The code example is as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.