基于selenium对网页接口进行自动化测试
执行环境:python3.6脚本
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://www.baidu.com/")
#driver.find_element_by_css_selector(".s_ipt").send_keys("西安工业大学")
#driver.find_element_by_css_selector("#su").click()
driver.find_element_by_id("kw").send_keys("西安工业大学")
driver.find_element_by_id("su").click()
#设置长宽
driver.set_window_size(400,400)
#智能等待
driver.implicitly_wait(10)
driver.find_element_by_partial_link_text("百度百科").click()
driver.maximize_window()
print("title:"+driver.title)
print("url:"+driver.current_url)
time.sleep(8)
driver.quit()
from selenium import webdriver
import time
import os
driver=webdriver.Chrome()
#指定文件路径下的html
file='file:///'+os.path.abspath("C:/Users/Administrator/Desktop/admin.html")
driver.get(file)
driver.find_element_by_id("su").click()
#设置长宽
driver.set_window_size(400,400)
#智能等待
driver.implicitly_wait(10)
driver.maximize_window()
print("title:"+driver.title)
print("url:"+driver.current_url)
time.sleep(8)
driver.quit()
from selenium import webdriver
import unittest
import time
class Baidu22(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.base_url="https://www.baidu.com/"
def test_CN(self):
driver=self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("西安工业大学")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_EN(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("NASA")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_DG(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("911")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main(verbosity=0)
from selenium import webdriver
import unittest
import time
class Baidu11(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.base_url="https://www.baidu.com/"
def test_CN(self):
driver=self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("西安")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_EN(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("china")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_DG(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("119")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main(verbosity=0)
from selenium import webdriver
import unittest
import time
class Baidu22(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.base_url="https://www.baidu.com/"
def test_CN(self):
driver=self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("b站")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_EN(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("NBA")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def test_DG(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_id("kw").send_keys("918")
driver.find_element_by_id("su").click()
time.sleep(3)
driver.find_element_by_partial_link_text("百度百科").click()
time.sleep(4)
def tearDown(self):
self.driver.quit()
if __name__=="__main__":
unittest.main(verbosity=0)
work为工程? baidu1 baidu2为封装的测试用例
import unittest
from work import baidu
from work import baidu1
from work import baidu2
def createsuite():
suite=unittest.TestSuite()
suite.addTest(baidu.baidu("test_CN"))
suite.addTest(baidu.baidu("test_EN"))
suite.addTest(baidu1.baidu11("test_CN"))
suite.addTest(baidu1.baidu11("test_EN"))
suite.addTest(baidu2.baidu22("test_CN"))
suite.addTest(baidu2.baidu22("test_EN"))
return suite
if __name__=="__main__":
suite=createsuite()
runner=unittest.TextTestRunner(verbosity=1)
runner.run(suite)
-
makesuit解决测试用例逐个添加,适用于大量测试用例
import unittest
from work import baidu
from work import baidu1
from work import baidu2
def createsuite():
suite=unittest.TestSuite()
suite.addTest(unittest.makeSuite(baidu1.baidu11))
suite.addTest(unittest.makeSuite(baidu2.baidu22))
return suite
if __name__=="__main__":
suite=createsuite()
runner=unittest.TextTestRunner(verbosity=1)
runner.run(suite)
-
discovers解决多个测试脚本,把文件夹下所有脚本中的test方法添加在测试套件中
import unittest
def createsuite():
discovers=unittest.defaultTestLoader.discover("../work",pattern="baidu*.py",top_level_dir=None)
print(discovers)
return discovers
if __name__=="__main__":
suite=createsuite()
runner=unittest.TextTestRunner(verbosity=1)
runner.run(suite)
|