Selenium RC For Python:教程4
checkbox:
使用check和is_checked方法
代码
def test_click_checkbox_with_ischecked(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
sel.check("id=checkBoxInput")
self.assertTrue(sel.is_checked("id=checkBoxInput"))
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
sel.check("id=checkBoxInput")
self.assertTrue(sel.is_checked("id=checkBoxInput"))
使用click和get_value方法
代码
def test_checkbox_with_getvalue(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("off", sel.get_value("id=checkBoxInput"))
sel.click("id=checkBoxInput")
self.assertEquals("on", sel.get_value("id=checkBoxInput"))
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("off", sel.get_value("id=checkBoxInput"))
sel.click("id=checkBoxInput")
self.assertEquals("on", sel.get_value("id=checkBoxInput"))
Radio Button
处理button的时候,在locator中,需要同时提供name(input name)以及value(radio button的value)
代码
def test_clickradiobutton_use_checkandischeck(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertFalse(sel.is_checked("name=radioButton value=a"))
self.assertFalse(sel.is_checked("name=radioButton value=b"))
sel.check("name=radioButton value=b")
self.assertTrue(sel.is_checked("name=radioButton value=b"))
self.assertFalse(sel.is_checked("name=radioButton value=a"))
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertFalse(sel.is_checked("name=radioButton value=a"))
self.assertFalse(sel.is_checked("name=radioButton value=b"))
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方法,不过要注意的是单独的button有两个值:"on"和"off"
代码
def test_clickradiobutton_use_clickabdgetvalue(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("off", sel.get_value("name=radioButton"))
self.assertEquals("off", sel.get_value("name=radioButton value=a"))
self.assertEquals("off", sel.get_value("name=radioButton value=b"))
sel.click("name=radioButton value=b")
self.assertEquals("off", sel.get_value("name=radioButton"))
self.assertEquals("off", sel.get_value("name=radioButton value=a"))
self.assertEquals("on", sel.get_value("name=radioButton value=b"))
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("off", sel.get_value("name=radioButton"))
self.assertEquals("off", sel.get_value("name=radioButton value=a"))
self.assertEquals("off", sel.get_value("name=radioButton value=b"))
sel.click("name=radioButton value=b")
self.assertEquals("off", sel.get_value("name=radioButton"))
self.assertEquals("off", sel.get_value("name=radioButton value=a"))
self.assertEquals("on", sel.get_value("name=radioButton value=b"))
Select
Select比一般的input有点复杂,首先,必须先确定select元素,接着要确定select里面的选项。select里面的option可以用ids和values来确定。所以我们必须用locator来确定select本身以及我们感兴趣的选项。选项的locator可以是选项元素的id,值,标签或者在select里的index。
当选择之后,我们可以使用get_value,get_selected_value, get_selected_label等方法
代码
def test_selectfromdropdown_novaluesinselect_uselabeloptionlocator(self):
sel =self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("option one", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option one", sel.get_value("id=selectWithLabelsOnly"))
sel.select("id=selectWithLabelsOnly", "label=option two")
self.assertTrue(sel.is_something_selected("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_value("id=selectWithLabelsOnly"))
sel =self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("option one", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option one", sel.get_value("id=selectWithLabelsOnly"))
sel.select("id=selectWithLabelsOnly", "label=option two")
self.assertTrue(sel.is_something_selected("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_value("id=selectWithLabelsOnly"))
使用index的版本
代码
def test_selectfromdropdown_novaluesinselect_useindexoptionlocator(self):
sel =self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("option one", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option one", sel.get_value("id=selectWithLabelsOnly"))
sel.select("id=selectWithLabelsOnly", "index=1")
self.assertTrue(sel.is_something_selected("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_value("id=selectWithLabelsOnly"))
sel =self.selenium
sel.open(self.TEST_PAGE_URL)
sel.wait_for_page_to_load(self.MAX_WAIT_IN_MS)
self.assertEquals("option one", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option one", sel.get_value("id=selectWithLabelsOnly"))
sel.select("id=selectWithLabelsOnly", "index=1")
self.assertTrue(sel.is_something_selected("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_selected_label("id=selectWithLabelsOnly"))
self.assertEquals("option two", sel.get_value("id=selectWithLabelsOnly"))
处理带值的select
代码
def test_selectfromdropdown_labelsandvaluesinselect_uselabellocator(self):
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
self.assertTrue(sel.is_something_selected("id=selectWithLabelsAndValues"))
selectedLabel = sel.get_selected_label("id=selectWithLabelsAndValues")
self.assertEquals("option one", selectedLabel)
self.assertEquals("1", sel.get_value("id=selectWithLabelsAndValues"))
self.assertEquals("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.assertEquals("option two", selectedLabel)
self.assertEquals("2", sel.get_value("id=selectWithLabelsAndValues"))
self.assertEquals("2", sel.get_selected_value("id=selectWithLabelsAndValues"))
sel = self.selenium
sel.open(self.TEST_PAGE_URL)
self.assertTrue(sel.is_something_selected("id=selectWithLabelsAndValues"))
selectedLabel = sel.get_selected_label("id=selectWithLabelsAndValues")
self.assertEquals("option one", selectedLabel)
self.assertEquals("1", sel.get_value("id=selectWithLabelsAndValues"))
self.assertEquals("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.assertEquals("option two", selectedLabel)
self.assertEquals("2", sel.get_value("id=selectWithLabelsAndValues"))
self.assertEquals("2", sel.get_selected_value("id=selectWithLabelsAndValues"))
Alert Box
主要使用get_alert方法处理alert box,这个方法和点OK 按钮有相同的效果,同时还返回alert box上的文本。
代码
作者:Shane
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://bluescorpio.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。