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:
- 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 shocould 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.
The following are some methods to obtain information from the page:
- 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.
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 ()