[Python] 纯文本查看 复制代码
# 导入 webdriver
import time
from selenium import webdriver
# 要想调用键盘按键操作需要引入keys包
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class jks_permission:
def __init__(self, jks_url, username, password):
self.jks_url = jks_url
self.username = username
self.password = password
self.login_driver = self.login_driver()
def login_driver(self):
chrome_options = Options()
# chrome_options.add_argument('--headless')
chrome_options.add_argument('ignore-certificate-errors')
driver = webdriver.Chrome(options=chrome_options)
# get方法会一直等到页面被完全加载,然后才会继续程序,通常测试会在这里选择 time.sleep(2)
driver.get(f"{self.jks_url}")
# 等待加载元素
# wait.until(EC.presence_of_element_located(locator))
time.sleep(2)
# 输入用户密码
driver.find_element(by=By.NAME, value="j_username").send_keys(f"{self.username}")
driver.find_element(by=By.NAME, value="j_password").send_keys(f"{self.password}")
time.sleep(2)
# click() 是模拟点击
driver.find_element(by=By.NAME, value="Submit").click()
time.sleep(2)
# 打印页面标题
print(driver.title)
print("login succeed.")
return driver
def driver_close(self):
# 关闭当前页面,如果只有一个页面,会关闭浏览器
print("close tab")
self.login_driver.close()
# 关闭浏览器
print("close browser")
self.login_driver.quit()
def add_permission(self, rolename, role_regexp):
self.login_driver.find_element(By.LINK_TEXT, value="系统管理").click()
self.login_driver.find_element(By.XPATH, value="//*[@id='main-panel']/section[2]/div/div[4]/a").click()
self.login_driver.find_element(By.LINK_TEXT, value="Manage Roles").click()
self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/div[1]/div[2]/input").send_keys(
f"{rolename}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/div[2]/div[2]/input").send_keys(
f"{role_regexp}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/div[3]/div[1]/span[1]/span/button").click()
wait = WebDriverWait(self.login_driver, timeout=2)
try:
rolename_status = 'true'
wait.until(EC.alert_is_present())
except:
rolename_status = 'false'
print(f"There is no rolename {rolename}")
if rolename_status == 'true':
print(self.login_driver.switch_to.alert.text)
self.login_driver.switch_to.alert.accept()
self.login_driver.find_element(By.LINK_TEXT, value="Dashboard").click()
return 'quit'
project_table = self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/table/tbody")
table_tr_list = project_table.find_elements_by_tag_name('tr')
for tr in table_tr_list:
if f'{rolename}' in tr.text:
if f'{rolename}' == tr.find_element(By.XPATH, value=f"td[2]").text:
for i in range(4, 28):
tr.find_element(By.XPATH, value=f"td[{i}]/input").send_keys(Keys.SPACE)
print("rolename info: " + tr.text)
self.login_driver.find_element(by=By.ID, value="yui-gen2-button").click()
print(f"Add rolename {rolename} succeed.")
self.login_driver.find_element(By.LINK_TEXT, value="Dashboard").click()
def create_user(self, new_user, new_pass, email):
self.login_driver.find_element(By.LINK_TEXT, value="系统管理").click()
self.login_driver.find_element(By.XPATH, value="/html/body/div[4]/div[2]/section[2]/div/div[5]/a").click()
self.login_driver.find_element(By.XPATH, value="/html/body/div[4]/div[1]/div/div[3]/span/a").click()
self.login_driver.find_element(By.XPATH,
value="/html/body/div[4]/div[2]/form/div[1]/table/tbody/tr[1]/td[2]/input").send_keys(
f"{new_user}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[4]/div[2]/form/div[1]/table/tbody/tr[2]/td[2]/input").send_keys(
f"{new_pass}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[4]/div[2]/form/div[1]/table/tbody/tr[3]/td[2]/input").send_keys(
f"{new_pass}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[4]/div[2]/form/div[1]/table/tbody/tr[4]/td[2]/input").send_keys(
f"{new_user}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[4]/div[2]/form/div[1]/table/tbody/tr[5]/td[2]/input").send_keys(
f"{email}")
self.login_driver.find_element(By.ID, value="yui-gen1-button").click()
try:
if self.login_driver.find_element_by_class_name("error"):
print(f"The user {new_user} is present")
except:
print(f"Add user {new_user} is succeed.")
self.login_driver.find_element(By.LINK_TEXT, value="Dashboard").click()
def assign_role(self, username, rolename):
self.login_driver.find_element(By.LINK_TEXT, value="系统管理").click()
self.login_driver.find_element(By.XPATH, value="//*[@id='main-panel']/section[2]/div/div[4]/a").click()
self.login_driver.find_element(By.XPATH, value="//*[@id='management-links']/tbody/tr[2]/td[2]/div[1]/a").click()
self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/div[1]/div[2]/input").send_keys(
f"{username}")
self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/div[2]/div[1]/span[1]").click()
wait = WebDriverWait(self.login_driver, timeout=2)
try:
assign_user_status = 'true'
wait.until(EC.alert_is_present())
except:
assign_user_status = 'false'
if assign_user_status == 'true':
print(f"user {username} {self.login_driver.switch_to.alert.text}")
self.login_driver.switch_to.alert.accept()
permission_table = self.login_driver.find_element(By.XPATH,
value="/html/body/div[5]/div[2]/form/div[1]/div[2]/div[2]/div[2]/div/div/table/tbody")
table_tr_list = permission_table.find_elements_by_tag_name('tr')
role_clo_nu = 1
for col_tag in table_tr_list[0].find_elements_by_tag_name('td'):
# print(col_tag.text)
if f'{rolename}' == col_tag.text:
print(f'rolename {rolename} is td {role_clo_nu}')
break
else:
role_clo_nu = role_clo_nu + 1
if role_clo_nu > len(table_tr_list[0].find_elements_by_tag_name('td')):
print(f"There is no {rolename} present.")
self.login_driver.find_element(By.LINK_TEXT, value="Dashboard").click()
return 'quit'
for tr in table_tr_list:
if f'{username}' in tr.text:
if f'{username}' == tr.find_element(By.XPATH, value=f"td[2]").text:
if tr.find_element(By.XPATH, value=f"td[{role_clo_nu}]/input").is_selected():
print(f"{username} assign rolename {rolename} is selected")
else:
tr.find_element(By.XPATH, value=f"td[{role_clo_nu}]/input").send_keys(Keys.SPACE)
print("rolename info: " + tr.text)
break
self.login_driver.find_element(By.ID, value="yui-gen2-button").click()
self.login_driver.find_element(By.LINK_TEXT, value="Dashboard").click()
permission_ma = jks_permission('jenkins_url/login', 'admin', 'admin')
try:
permission_ma.create_user('test', 'test','test@jks.cn')
permission_ma.add_permission('test2', 'test.*')
permission_ma.assign_role('test', 'test2')
finally:
permission_ma.driver_close()
print("finish")