如何使用Python在Mantis Bug Tracker中查询或创建缺陷
基于Selenium操作Mantis:查询与创建缺陷方案
一、基于Summary查询缺陷
登录成功后,按以下步骤实现查询逻辑:
- 跳转至缺陷搜索页面
- 在Summary筛选框填入目标关键词
- 提交搜索并提取匹配的缺陷列表
实现代码
将以下函数添加到你的现有脚本中:
def search_bug_by_summary(driver, summary_keyword): # 进入搜索页面 driver.get(f"{driver.current_url}view_all_bug_page.php") WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "bug-summary"))) # 输入Summary关键词 summary_input = driver.find_element(By.ID, "bug-summary") summary_input.clear() summary_input.send_keys(summary_keyword) # 提交搜索(按钮文本需根据实际页面调整) driver.find_element(By.CSS_SELECTOR, "input[type='submit'][value='应用筛选']").click() # 提取搜索结果 WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "buglist"))) bug_rows = driver.find_elements(By.CSS_SELECTOR, ".buglist tbody tr") results = [] for row in bug_rows[1:]: # 跳过表头行 bug_id = row.find_element(By.CSS_SELECTOR, "td:nth-child(1) a").text bug_summary = row.find_element(By.CSS_SELECTOR, "td:nth-child(7)").text results.append({"id": bug_id, "summary": bug_summary}) return results
二、创建新缺陷
登录成功后,按以下步骤完成缺陷创建:
- 进入缺陷报告页面
- 填写必填字段(项目、Summary、描述等)
- 提交表单并验证创建结果
实现代码
将以下函数添加到你的现有脚本中:
def create_new_bug(driver, project_name, summary, description): # 进入缺陷创建页面 driver.get(f"{driver.current_url}bug_report_page.php") WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "project_id"))) # 选择目标项目(根据项目名称匹配) project_select = Select(driver.find_element(By.ID, "project_id")) project_select.select_by_visible_text(project_name) # 填写核心字段 summary_input = driver.find_element(By.ID, "summary") summary_input.send_keys(summary) desc_input = driver.find_element(By.ID, "description") desc_input.send_keys(description) # 提交缺陷(按钮文本需根据实际页面调整) driver.find_element(By.CSS_SELECTOR, "input[type='submit'][value='提交报告']").click() # 验证创建成功并返回缺陷ID WebDriverWait(driver, 10).until(EC.url_contains("view.php")) new_bug_id = driver.current_url.split("id=")[1] return f"新缺陷创建成功,ID:{new_bug_id}"
三、整合到现有脚本
修改main函数,调用上述功能函数:
def main(args): credentials_file = "credentials.txt" try: username = args.username password = args.password except Exception as e: print(f"读取凭证失败: {e}") return mantisURL = "http://mantis-dev.*********.com/" try: driver = mantisLogin(mantisURL, username, password) # 示例:查询Summary包含"测试"的缺陷 search_results = search_bug_by_summary(driver, "测试") print("查询结果:") for bug in search_results: print(f"缺陷ID: {bug['id']}, Summary: {bug['summary']}") # 示例:创建新缺陷 create_result = create_new_bug(driver, "测试项目", "自动化创建缺陷测试", "这是通过Selenium自动化创建的缺陷描述") print(create_result) except Exception as e: print(f"操作失败: {e}") return finally: driver.quit()
注意事项
- 元素选择器(ID、CSS选择器等)可能因Mantis版本不同而变化,需根据实际页面的HTML结构调整
- 若页面加载缓慢,可适当延长
WebDriverWait的超时时间 - 部分场景下可能需要补充填写分类、优先级等其他必填字段
内容的提问来源于stack exchange,提问作者sugarakis
相关产品推荐
相关产品推荐

