例子一:
# coding=utf-8
'''created:2018/3/29
author:star
project:lianxi canshuhua'''
from selenium import webdriver
import unittest,os,time
import HTMLTestRunner
class baidu002(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(20)
self.baseurl="http://www.baidu.com/?tn=90702711_hao_pg"
def login1(self,text):
driver=self.driver
driver.get(self.baseurl)
driver.find_element_by_id("kw").clear()
driver.find_element_by_id("kw").send_keys(text)
driver.find_element_by_id("su").click()
time.sleep(2)
def duanyan(self,title1):
driver=self.driver
title=driver.title ###通过titile来判断
self.assertEqual(title,title1)
def test001(self):
self.login1("selenium")
self.duanyan(title1=u"selenium_百度搜索")
def test002(self):
self.login1("python")
self.duanyan(title1=u"python_百度搜索")
def test003(self):
self.login1("java")
self.duanyan(title1=u"java_百度搜索")
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
例子二:
# coding=utf-8
'''created:2018/3/29
author:star
project:canshuhua'''
from selenium import webdriver
import unittest,time,os
import HTMLTestRunner
class lingzhuyun(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(20)
self.baseurl="http://www.*********“
def login(self,username,password):
driver=self.driver
driver.get(self.baseurl)
driver.find_element_by_id("login").clear()
driver.find_element_by_id("login").send_keys(username) ##用户名
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys(password) ###密码
time.sleep(2)
driver.find_element_by_xpath("//div[@class='clearfix oe_login_buttons']/button[@class='btn btn-primary']").click() ####登录
time.sleep(3)
def duanyan(self):
driver=self.driver
try:
result_text=driver.find_element_by_xpath("//a[@class='oe_menu_toggler']/span[@class='oe_menu_text']").text ####通过文本内容来判断登录是否成功
print (result_text)
return True
except:
return False
def test1(self):
self.login(u"大猩猩","134*****")
a=self.duanyan()
self.assertTrue(a)
def test2(self):
self.login(u"大猩猩","135*******")
a=self.duanyan()
self.assertTrue(a)
def test3(self):
self.login("aaaaaa","134*****")
a=self.duanyan()
self.assertTrue(a)
def test4(self):
self.login("aaaaaa","135******")
a=self.duanyan()
self.assertTrue(a)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()