原创,转载请注明本文出处。谢谢
selenium模块中相关方法在web测试中的使用,常用方法在本文中加粗显示:
#coding:utf-8
from selenium import selenium
import unittest
import time
class TestPageForSeleniumRemoteControl(unittest.TestCase):
MAX_WAIT_IN_MS=60000
BASE_URL = "http://bitmotif.com"
TEST_PAGE_URL = BASE_URL + "/test-page-for-selenium-remote-control"
TEST_PAGE_TITLE = u"Test Page For Selenium Remote Control \xab Bit Motif"
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("192.168.1.100",4444,"*firefox3",self.BASE_URL)
self.selenium.start()
def tearDown(self):
self.selenium.stop()
def test_is_element_exists(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
############### 使用is_element_present("id=xxx"),is_text_present()来验证元素的存在 ###################
self.assertTrue(sel. is_element_present("id=textInput"))
self.assertTrue(sel. is_text_present("Test Page For Selenium Remote Control"))
print sel. is_element_present("id=textInput")
################# 使用get_title() 检查网页Title ##############################
self.assertEqual(self.TEST_PAGE_TITLE,sel. get_title())
################ 使用get_text()和get_value()处理文本输入框Input #########
self.assertEqual("",sel. get_value("id=textInput"))
sel.type("id=textInput","Text In The Field")
print sel. get_text("id=textInput")
# get_text():
self.assertEqual("Text In The Field", sel.get_value("id=textInput"))
# get_value():
self.assertNotEqual("Text In The Field", sel.get_text("id=textInput"))
############################ 复选框checkBox 的处理 # ###############################
#### check和is_checked()方法来处理:
sel .check("id=checkBoxInput")
self.assertTrue( sel.is_checked("id=checkBoxInput"))
###click和 get_value()方法来处理:
#self.assertEqual("off",sel. get_value("id=checkBoxInput"))
#sel .click("id=checkBoxInput")
#self.assertEqual("on",sel. get_value("id=checkBoxInput"))
########################单选框 radioButton的处理################################
##########使用check的方式选中复选框并用is_checked()来检查:
self.assertFalse(sel. is_checked("name=radioButton value=a"))
self.assertFalse(sel. is_checked("name=radioButton value=b"))
#check方法:
sel.check("name=radioButton value=b")
self.assertTrue(sel.is_checked("name=radioButton value=b"))
self.assertFalse(sel.is_checked("name=radioButton value=a"))
###########使用click的方式选中复单选框并用get_value()来检查:
self.assertEqual("off",sel.get_value("name=radioButton"))
self.assertEqual("off",sel.get_value("name=radioButton value=a"))
self.assertEqual("off",sel.get_value("name=radioButton value=b"))
#click方法:
# sel.click("name=radioButton value=a")
self.assertEqual("off",sel.get_value("name=radioButton"))
self.assertEqual("off",sel.get_value("name=radioButton value=a"))
self.assertEqual("on",sel.get_value("name=radioButton value=b"))
############### 下拉列表select box 的处理方式#######################################
self.assertEqual("option one", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEqual("option one",sel.get_value("id=selectWithLabelsOnly"))
#使用label的方法处理select Box:
sel.select("id=selectWithLabelsOnly","label=option two")
#使用index的方法处理select Box:
# sel.select("id=selectWithLabelsOnly","index=1")
self.assertTrue(sel. is_something_selected("id=selectWithLabelsOnly"))
self.assertEqual("option two",sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEqual("option two",sel.get_value("id=selectWithLabelsOnly"))
## 处理带值的select:
self.assertTrue(sel. is_something_selected("id=selectWithLabelsAndValues"))
selectedLabel = sel. get_selected_label("id=selectWithLabelsAndValues")
self.assertEqual("option one",selectedLabel)
self.assertEqual("1",sel.get_value("id=selectWithLabelsAndValues"))
self.assertEqual("1",sel. get_selected_value("id=selectWithLabelsAndValues"))
sel.select("id=selectWithLabelsAndValues","label=option two")
self.assertTrue(sel.is_something_selected("id=selectWithLabelsAndValues"))
selectedLabel = sel.get_selected_label("id=selectWithLabelsAndValues")
self.assertEqual("option two",selectedLabel)
self.assertEqual("2",sel.get_value("id=selectWithLabelsAndValues"))
self.assertEqual("2",sel.get_selected_value("id=selectWithLabelsAndValues"))
###################### Alert Box# ######################################
def test_popup_creation(self):
sel=self.selenium
sel.open(self.TEST_PAGE_URL)
sel. wait_for_page_to_load(self.MAX_WAIT_IN_MS)
windowNames=sel. get_all_window_names()
print windowNames[0]
self.assertEqual(1,len(windowNames))
self.assertEqual("selenium_main_app_window",windowNames[0])
sel.click("link=This link opens a popup")
sel.wait_for_pop_up("popup",self.MAX_WAIT_IN_MS)
windowNames=sel. get_all_window_names()
print windowNames
self.assertEqual(2,len(windowNames))
self.assertTrue("selenium_main_app_window" in windowNames)
self.assertTrue("popup" in windowNames)
#关闭弹出窗口,回到最初的窗口
sel. close()
sel. select_window("null")
def test_window_selection_closepopup_returntomainwindow(self):
sel=self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEqual(self.TEST_PAGE_TITLE,sel.get_title())
sel.click("link=This link opens a popup")
sel. wait_for_pop_up("popup",self.MAX_WAIT_IN_MS)
sel. select_window("popup")
self.assertEqual("Bit Motif",sel.get_title())
#close.popup
sel.close()
#select original window
sel. select_window("null")
self.assertEqual(self.TEST_PAGE_TITLE,sel.get_title())
if __name__=="__main__":
unittest.main()