将多个class放在一个文件内一起运行,这是一个多用例不同网站进行测试的方法
#encoding=utf-8
from selenium import webdriver
import time,unittest, re,sys
from HTMLTestRunner import HTMLTestRunner
class YoudaoIdeTest(unittest.TestCase):
'''有道翻译测试'''
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://www.youdao.com/"
def test_youdao_ide(self):
'''中英翻译测试'''
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("translateContent").clear()
driver.find_element_by_id("translateContent").send_keys(u"自动化测试报告")
driver.find_element_by_css_selector("button").click()
time.sleep(5)
self.assertEqual(u"【自动化测试报告】", driver.title)
def tearDown(self):
self.driver.quit()
class Test(unittest.TestCase):
'''百度登录'''
@classmethod
def setUpClass(self):
self.driver=webdriver.Firefox()
self.driver.get("http://www.baidu.com")
def test_login(self):
self.driver.add_cookie({u'name':u'BDUSS', u'value':自己的值})
time.sleep(3)
self.driver.refresh()
def test_login_successful(self):
text=self.driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[3]/a[7]/span").text#xpath必须双引号
#text=self.driver.find_element_by_id('s_username_top').text
target='Dysania_GGG'
str(text)
#print text
self.assertEqual(text,target)
@classmethod
def tearDownClass(self):
self.driver.quit()
if __name__=='__main__':
unittest.main()
检验结果: