1. 程式人生 > >交管系統,介面,UI新增刪除課程

交管系統,介面,UI新增刪除課程

from selenium import webdriver
import time
import string, random
import requests
import pprint

sele刪除課程

def Delete_Course():
	driver = webdriver.Chrome()
	driver.implicitly_wait(10)
	driver.get('http://localhost/mgr/login/login.html')
	driver.find_element_by_id('username').send_keys('auto')
	driver.find_element_by_id('password').send_keys('sdfsdfsdf')
	driver.find_element_by_tag_name('button').click()
	tbody = driver.find_elements_by_css_selector("tbody")
	if tbody:               #判斷裡面有沒有課程,沒有的話,直接退出
		tr = driver.find_elements_by_css_selector("tbody>tr")
		for i in range(1,len(tr)+1):
			driver.find_element_by_css_selector("tbody>tr>td:nth-child(4)>button:nth-child(2)").click()
			driver.find_element_by_css_selector(".btn-primary").click()
			time.sleep(0.5)
			driver.refresh()
	driver.quit()

sele增加課程

def Add_Course(count):
	driver = webdriver.Chrome()
	driver.implicitly_wait(10)
	driver.get('http://localhost/mgr/login/login.html')
	driver.find_element_by_id('username').send_keys('auto')
	driver.find_element_by_id('password').send_keys('sdfsdfsdf')
	driver.find_element_by_tag_name('button').click()

	for i in range(count):
		Course_name = ''.join(random.choices(string.ascii_lowercase + string.digits+string.ascii_uppercase, k=6))

		driver.find_element_by_css_selector('[ng-click*="showAddOne=true"]').click()

		a = driver.find_element_by_css_selector('[ng-model*="addData.name"]')
		a.send_keys(Course_name)
		b = driver.find_element_by_css_selector('[ng-model*="addData.desc"]')
		b.send_keys(Course_name)
		addOne = driver.find_element_by_css_selector('[ng-click*="addOne()"]')
		addOne.click()
		time.sleep(1)
		a.clear()
		b.clear()
	driver.quit()

介面新增課程

def add_course(conut):
	def login_api():                  #登入獲取sessionid
		values = {
			"username": "auto",
			"password": "sdfsdfsdf"
		}

		res = requests.post("http://localhost/api/mgr/loginReq", data=values)
		a = res.cookies["sessionid"]
		# print(session_id)
		return a


	def api_add_course(sessionid,count):
		for i in range (1,count+1):
			name1 = ''.join(random.choices(string.ascii_lowercase + string.digits + string.ascii_uppercase, k=6))
			payload = {
				"action": "add_course",
				"data": """{
			            "name":"%s",
			            "desc":"%s",
			            "display_idx":"1"
			        }
			        """ % (name1+"_課程", name1)
			}
			res = requests.post("http://localhost/api/mgr/sq_mgr/", data=payload, cookies={"sessionid": sessionid})

			# retObj = res.json()
			# pprint.pprint(retObj)
			# return retObj

	sessionid = login_api()
	# print(sessionid)
	api_add_course(sessionid,conut)

介面刪除課程

def del_course():
	def login_api():                  #登入獲取sessionid
		values = {
			"username": "auto",
			"password": "sdfsdfsdf"
		}

		res = requests.post("http://localhost/api/mgr/loginReq", data=values)
		a = res.cookies["sessionid"]
		# print(session_id)
		return a



	def api_list_course(sessionid):         #列出課程列表

	    #傳送post請求 data接收的是字典型別的,會自動轉化成urlencode格式
	    res = requests.get("http://localhost/api/mgr/sq_mgr/?action=list_course&pagenum=1&pagesize=20",cookies={"sessionid":sessionid})    #返回的是request庫裡面的response物件


	    retObj = res.json()
	    # pprint.pprint(retObj)
	    #返回的是響應中訊息體對應的物件
	    return retObj


	sessionid = login_api()                 #登入
	res = api_list_course(sessionid)                #列出列表,獲取id

	res1 = res['retlist']           #resl 是一個這樣的值[{'id': 1369, 'name': 'vmVhKL_課程', 'desc': 'vmVhKL', 'display_idx': 1}, {'id': 1370, 'name': 'dQbstD_課程', 'desc': 'dQbstD', 'display_idx': 1}

	d=[]
	for i in res1:              #獲取單個課程資訊,把id放到d列表裡面
		d.append(i["id"])

	for k in d:                     #遍歷每個id,並刪除
		values = {
			"action": "delete_course",
			"id": k                 #   ?K是一個變化的數,有多少K就會執行多少次
		}
		res = requests.delete("http://localhost/api/mgr/sq_mgr/",cookies={"sessionid":sessionid},data=values)

add_course(200) #api介面新增kecheng,傳參

del_course() #api介面刪除課程

Add_Course(2) #UI增加課程。傳參

Delete_Course() #UI刪除課程**