Search code examples
pythonmantis

How to browse bugs or create a bug in Mantis Bug Tracker using python?


I have a small Python script that logs in to my company's Mantis Bug Tracking system. I surfed around for ways to search based on the Summary field or create a new Mantis bug, but nothing helped me so far. Can someone please steer me to the correct direction?

FYI The log-in code I tried is the following

import argparse
import re
import logging
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from datetime import datetime, timedelta


logging.basicConfig(level=logging.INFO)

def mantisLogin(baseURL, fsa_user, fsa_pwd):
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')

    driver = webdriver.Chrome(options=options)
    driver.get(baseURL)
    WebDriverWait(driver, 5).until(EC.url_contains("login"))
    username = driver.find_element("id", "id_username")
    password = driver.find_element("id", "id_password")
    username.clear()
    password.clear()
    username.send_keys(fsa_user)
    password.send_keys(fsa_pwd)
    driver.find_element(By.CLASS_NAME, "submit").click()
    WebDriverWait(driver, 5).until(EC.url_contains("main_page.php"))

    return driver

def main(args):
    credentials_file = "credentials.txt"
    try:
        username = args.username
        password = args.password
    except Exception as e:
        print(f"Failed to read credentials: {e}")
        return

    mantisURL = "http://mantis-dev.*********.com/"

    try:
        driver = mantisLogin(mantisURL, username, password)
    except Exception as e:
        print(f"Failed to login: {e}")
        return
    
    print(str(driver))
    driver.quit()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("username")
    parser.add_argument("password")
    main(parser.parse_args())

Thank you


Solution

  • After a lot of searching and trial and error, I found the solution that worked for my case. I do not think there is "one solution, works for all", because it depends on the Mantis Bug HTML (code behind) for the dropdown menus used. In my case the dropdown is a <div class that has a <ul class and it contains a number of <li class. The code I used to create anew Mantis Bug is listed below:

    (It requires your own Mantis Login code to create the "driver" which is a from selenium import webdriver)

    def createMantisBug(driver, pOutbreakName, pSummary, pDescription):
        global bug_summary, category, reported_version, earliest_build_number, description
    
        # Wait for the dropdown to be clickable
        dropdown = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.CSS_SELECTOR, '.chosen-single'))
        )
        # Click on the dropdown to open the options
        dropdown.click()
        # Locate and click on the "Outbreak Tracking" option
        outbreak_tracking_option = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//li[text()="Process Automation"]'))
        )
        outbreak_tracking_option.click()
    
        # Fill in the bug details
        summary_field = driver.find_element(By.NAME, "summary")
        reported_version_field = driver.find_element(By.NAME, "review_required")
        earliest_build_number_field = driver.find_element(By.NAME, "reported_buildnumber")
        description_field = driver.find_element(By.NAME, "description")
    
        summary_field.send_keys(pOutbreakName + ": " + pSummary)
        reported_version_field.send_keys(reported_version)
        earliest_build_number_field.send_keys(earliest_build_number)
        description_field.send_keys(pDescription)
    
        # Submit the bug report
        # Find the button by its ID using the find_element_by_id method
        submit_button = driver.find_element(By.ID, "mfsubmit")
        # Click the button to submit the form
        submit_button.click()
    

    The following code retrieves the just created Mantis Bug ID number. It could be used later on for an Update function:

    def getLatestBugId(driver):
        global newMantisBugID
    
        import time
        time.sleep(10)
    
        checkbox_value = "" #initialize
    
        # Find the bug_list table
        bug_table = driver.find_element(By.ID, "bug_list")
        # Find all rows in the table
        rows = bug_table.find_elements(By.TAG_NAME, "tr")
        # Initialize variables to store the most recent bug ID and timestamp
        most_recent_bug_id = None
        most_recent_timestamp = 0
    
        # Iterate through the rows to find the most recent bug ID
        for row in rows:
            # Find the checkbox element by its name attribute
            checkbox = driver.find_element(By.NAME, 'bug_arr[]')
            # Get the value attribute of the checkbox
            checkbox_value = checkbox.get_attribute('value')
            #DEBUG
            #print("Checkbox value:", checkbox_value)
    
        most_recent_bug_id = checkbox_value
        newMantisBugID = checkbox_value
        # Print the most recent bug ID
        print("Most Recent Bug ID:", most_recent_bug_id)
        return newMantisBugID
    
        # Close the browser
        driver.quit()
    

    I hope my code above can help anyone who is looking for similar answers :)