设为首页 收藏本站
查看: 52|回复: 0

[资源发布] python selenium 管理jenkins用户权限

[复制链接]
累计签到:86 天
连续签到:1 天
发表于 2022-9-19 18:09:52 | 显示全部楼层 |阅读模式
[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")



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-1002536-1-1.html 上篇帖子: python脚本管理jenkins 下篇帖子: 没有了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则  允许回帖邮件提醒楼主

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 在线客服QQ:点击这里给我发消息


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud bjyun

快速回复 返回顶部 返回列表