Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How do I attach a screenshot while creating the issue using Jira rest api in Pytest framework

I am using a Pytest-BDD framework for automation. I have written code for creating the issue, transition the issue to pass or fail, add comment and link the issue with the test case. Now I need to attach the screenshot from a directory in framework(where the screenshot is saved while running the test case ) and add the issue description from an output.txt file.

How do I attach the screenshot and add the content of output.txt file in the issue? Please anyone help me..

For this I have created a Jira class with code: 

import requests
from requests.auth import HTTPBasicAuth
import json

class Jira:

def __init__(self):

self.auth = HTTPBasicAuth("username", "password")
self.headers = {
"Accept": "application/json",
"Content-Type": "application/json"}

# 1
def create_issue(self):
url = "https://<url>.atlassian.net/rest/api/3/issue"
payload = json.dumps({
"fields": {
"project": {
"key": "TE"
},
"issuetype": {
"name": "Automated Test Execution"
},
"summary": " google search Execution",
"labels": [
"Sanity"
],
"description": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "test execution for automated google search "
}
]
}
]
},
"environment": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": "Stage",
"type": "text"
}
]
}
]
}
}
}
)
response = requests.request(
"POST",
url,
data=payload,
headers=self.headers,
auth=self.auth

)
# print(response.text)
y = json.loads(response.text)
self.key = y['key']
# return the key of the created issue
return self.key

# 2

def link_issue(self,test_case_id,key):
self.test_case_id = test_case_id
self.key = key
url = "https://<url>.atlassian.net/rest/api/3/issueLink"
payload = json.dumps({
"outwardIssue": {
"key": test_case_id
},
"inwardIssue": {
"key": key
},
"type": {
"name": "execution",
"inward": "is executed from",
"outward": "execution"
}
})
response = requests.request(
"POST",
url,
data=payload,
headers=self.headers,
auth=self.auth
)
# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

# 4
def test_result(self, key, status):
self.key = key
self.status = status
url = f"https://<url>.atlassian.net/rest/api/3/issue/{key}/transitions"
if status is True:
payload = json.dumps({
"transition": {
"id": "61"
}
})
else:
payload = json.dumps({
"transition": {
"id": "51"
}
})

response = requests.request(
"POST",
url,
data=payload,
headers=self.headers,
auth=self.auth
)
# no need to print anything as the response of this call doesn't return anything
# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

# 5
def add_comment(self,key):
url = f"https://<url>.atlassian.net/rest/api/3/issue/{key}/comment"
payload = json.dumps({
"body": {
"type": "doc",
"version": 1,
"content": [
{
"type": "paragraph",
"content": [
{
"text": f"new automated comment for {key} ",
"type": "text"
}
]
}
]
}
})
response = requests.request(
"POST",
url,
data=payload,
headers=self.headers,
auth=self.auth
)
print(response.text)
# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

  and the content of conftest.py is: 

import time
import pytest
from BrowserType.BrowserType import BrowserType
from Utilities.OutputUtil import OutputUtil
from Config import config
# importing Jira class from the jirautility file in utilities directory
from Utilities.jiraUtility import Jira
from functools import partial

driver = None
jr = None
status = True
test_case_id = None
key = None

# Pass browser name in the command line as parameter , if not passed the chrome is the default
# --browser_name firefox

def pytest_addoption(parser):
parser.addoption(
"--browser", action="store", default="chrome"
)
parser.addoption(
"--env_name", action="store", default="test"
)

@pytest.fixture
def browser(request):
global driver
browser_name = request.config.getoption("browser")
if browser_name == "chrome":
driver = BrowserType.get_browser_value(request, "chrome")
elif browser_name == "firefox":
driver = BrowserType.get_browser_value(request, "firefox")
elif browser_name == "ie":
driver = BrowserType.get_browser_value(request, "ie")

driver.implicitly_wait(2)
driver.maximize_window()
return driver

#--------Hooks--------------
# hooks have a pytest prefix
def pytest_bdd_before_scenario(request, scenario):
OutputUtil.OpenFileInWriteMode()


# run at teardown of test case
def pytest_bdd_after_scenario(request,scenario):
if driver is not None:
driver.get_screenshot_as_file(config.SCREENSHOT_FILE)
OutputUtil.CloseFile()

# get the test case value from scenario
print(scenario.name)
scenarioName = scenario.name
# get the test case name(scenario name)
test_case = scenarioName.split(":")
# get the test case ID to link with the respective test case
test_case_id = test_case[0]

# get the test result of pass/fail
scenario_report = request.node.__scenario_report__.serialize()
for step in range(len(scenario_report['steps'])):
if scenario_report['steps'][step]['failed']:
status = False
print(f'steps failed to execute: {status}')
break
else:
status = True
print(f'steps executed successfully: {status}')
if driver is not None:
driver.close()
driver.quit()

### object of Jira class
obj = Jira()
key = obj.create_issue()
obj.link_issue(test_case_id, key)
obj.test_result(key, status)
obj.add_comment(key)

@pytest.fixture(scope='function')
def context():
return {}


I am saving screenshot in a variable in config directory.  And I have output.txt, the contents of which are saved in a variable in config directory. Please help me,  I'm a little stuck here.

1 answer

0 votes

Sorry for a little mistake while posting the question. Actually in screenshot and output variable in config.py, the path of these files are stored not the content.  

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

378 views 1 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you