tfs icon indicating copy to clipboard operation
tfs copied to clipboard

Help needed in understanding how to use TFSHTTPClient

Open LakshmiAntin opened this issue 5 years ago • 20 comments

Hi,

Can someone explain the following line below mentioned in Advanced usage example? I am getting WorkItem undefined error.

new_task = WorkItem(client, new_task_raw)

LakshmiAntin avatar Mar 23 '20 18:03 LakshmiAntin

@LakshmiAntin, this line is used to create a WorkItem instance from a raw JSON that you would get from via client.rest_client.send_post.

Make sure you have imported the library first: from tfs import TFSAPI.

If that does not help, please provide a complete code example we can use to reproduce your error.

SAnCherepan avatar Mar 23 '20 21:03 SAnCherepan

I want to upload test case steps using send_patch method. I am able to do it through postman, but I am not sure if I should use this line( new_task = WorkItem(client, new_task_raw) ) to upload test case steps.

Please find my code below:

import requests from tfs import TFSAPI from requests_ntlm import HttpNtlmAuth

username = 'username' password = 'password' client = TFSAPI("https://TFSLINK:8080/tfs", project="Collection name", user=username, password=password,auth_type=HttpNtlmAuth)

data = [] obj = {} obj["op"] = "add" obj["path"] = "/fields/Microsoft.VSTS.TCM.Steps" obj["value"] = "<steps id="0"> <step id="2" type="ValidateStep"><parameterizedString isformatted="true">Input step 1</parameterizedString><parameterizedString isformatted="true">Expectation step 1</parameterizedString> <step id="3" type="ValidateStep"><parameterizedString isformatted="true">Input step 2</parameterizedString><parameterizedString isformatted="true">Expectation step 2</parameterizedString>" data.append(obj) headers = {} headers['Content-Type'] = 'application/json-patch+json' headers['Cache-Control'] = 'no-cache' payload = {} payload['validateOnly'] = True payload['api-version'] = '1.0' project = True

test_patch = client.rest_client.send_patch("https://TFSLINK:8080/tfs/Collection name/_apis/wit/workitems/XXXXX",data,headers = headers,payload = payload,project = project) new_task = WorkItem(client, test_patch)

XXXXX is workitem id for which I would like to add test case steps.

LakshmiAntin avatar Mar 23 '20 22:03 LakshmiAntin

@LakshmiAntin,

PATCH-requests are used to modify existing work items. POST-requests are used to create new work items.

If your task at hand is to modify already existing test cases, you do not need to initialize them via raw data. Steps should be added here: test_patch = client.rest_client.send_patch("https://TFSLINK:8080/tfs/Collection name/_apis/wit/workitems/XXXXX",data,headers = headers,payload = payload,project = project)

If you want to further modify your test case, init it via test_case = client.get_workitem(XXXXX) and you should have access to all of its fields. If no further modification is required, then send_patch should be your last command.

Let me know if that helps.

SAnCherepan avatar Mar 24 '20 13:03 SAnCherepan

{ "id": XXXXX, "rev": 1, "fields": { "System.AreaPath": "TFS", "System.TeamProject": "TFS", "System.IterationPath": "TFS", "System.WorkItemType": "Test Case", "System.State": "Design", "System.Reason": "New", "System.AssignedTo": "ABC <TFS\ABC>", "System.CreatedDate": "2019-01-09T08:00:50.51Z", "System.CreatedBy": "ABC <TFS\ABC>", "System.ChangedDate": "2019-01-09T08:00:50.51Z", "System.ChangedBy": "ABC <TFS\ABC>", "System.Title": "Sample task 2", "Microsoft.VSTS.Common.StateChangeDate": "2019-01-09T08:00:50.51Z", "Microsoft.VSTS.Common.ActivatedDate": "2019-01-09T08:00:50.51Z", "Microsoft.VSTS.Common.ActivatedBy": "ABC <TFS\ABC>", "Microsoft.VSTS.Common.Priority": 2, "Microsoft.VSTS.TCM.AutomationStatus": "Not Automated" 'XYZCorp.RegressionTest': 'No', 'Microsoft.VSTS.TCM.Steps': ' <parameterizedString isformatted="true">Input step 1</parameterizedString><parameterizedString isformatted="true">Expectation step 1</parameterizedString> <parameterizedString isformatted="true">Input step 2</parameterizedString><parameterizedString isformatted="true">Expectation step 2</parameterizedString>' }, "_links": { "self": { "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/XXXXX" }, "workItemUpdates": { "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/XXXXX/updates" }, "workItemRevisions": { "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/XXXXX/revisions" }, "workItemHistory": { "href": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/XXXXX/history" }, "html": { "href": "https://TFSLINK/web/wi.aspx?pcguid=07b658c4-97e5-416f-b32d-3dd48d7f56cc&id=XXXXX" }, "workItemType": { "href": "https://TFSLINK/DefaultCollection/18ca0a74-cf78-45bf-b163-d8dd4345b418/_apis/wit/workItemTypes/Test%20Case" }, "fields": { "href": "https://TFSLINK/DefaultCollection/_apis/wit/fields" } }, "url": "https://TFSLINK/DefaultCollection/_apis/wit/workItems/XXXXX" }

I get the above response when I use send_patch request. I can see test case steps are returned back in the response, however it is not shown on the Web interface of TFS.

LakshmiAntin avatar Mar 24 '20 18:03 LakshmiAntin

'Microsoft.VSTS.TCM.Steps': '<steps id="0"> <step id="2" type="ValidateStep"><parameterizedString isformatted="true">Input step 1</parameterizedString><parameterizedString isformatted="true">Expectation step 1</parameterizedString><description/></step> <step id="3" type="ValidateStep"><parameterizedString isformatted="true">Input step 2</parameterizedString><parameterizedString isformatted="true">Expectation step 2</parameterizedString><description/></step></steps>'

Above steps xml representation was changed by browser I guess. Adding it again for clarity.

LakshmiAntin avatar Mar 24 '20 18:03 LakshmiAntin

I will ask just in case: have you tried updating the web page after recieving positive response from patch?

Also try to get test case via client.get_workitem(XXXXX) after patching and see the content of its fields. Microsoft.VSTS.TCM.Steps in particular.

SAnCherepan avatar Mar 26 '20 13:03 SAnCherepan

Yes. I have tried updating the web page after receiving positive response from patch. But steps were not shown even after a successful patch response.

I tried workitem = client.get_workitem(XXXXX) print(workitem['fields']) and the output is None. Is the way I am accessing 'fields' wrong? How do I see all contents of a workitem object?

Thanks

LakshmiAntin avatar Mar 26 '20 16:03 LakshmiAntin

Try print(workitem['Microsoft.VSTS.TCM.Steps']) or try looking for values in debugger if you have the opportunity.

SAnCherepan avatar Mar 26 '20 17:03 SAnCherepan

No. The data returned by workitem = client.get_workitem(XXXXX) does not have Microsoft.VSTS.TCM.Steps field. But the response the patch request sends back does have it. Could you please help me figure out the problem?

Thanks.

LakshmiAntin avatar Mar 26 '20 18:03 LakshmiAntin

Another just-in-case-check: make sure the field you see in web actually corresponds to Microsoft.VSTS.TCM.Steps and no other HTML field.

Try this algorithm:

  1. Add steps to your test case manually via web and save it.
  2. Get test case via client.get_workitem() and print workitem['Microsoft.VSTS.TCM.Steps']. Remember/copy the value.
  3. Remove steps via web and save the test case.
  4. Add the same steps via workitem['Microsoft.VSTS.TCM.Steps'] = <copy_value_from_step_2>
  5. See if the steps appear in web.

SAnCherepan avatar Mar 27 '20 08:03 SAnCherepan

I tried the above algorithm and steps still do not appear on web.

LakshmiAntin avatar Mar 27 '20 19:03 LakshmiAntin

I really should have mentioned it sooner. If your TFS is 2017 or newer, try authenticating via personal access token: client = TFSAPI("https://TFSLINK:8080/tfs", project="Collection name", pat=personal_access_token) as password authentification was disabled for API-requests in 2017.

SAnCherepan avatar Mar 28 '20 02:03 SAnCherepan

I used personal_access_token and still test steps did not show up on web.

LakshmiAntin avatar Mar 30 '20 21:03 LakshmiAntin

The very last advice i can give you will be a generic one: Same actions provide same results. If result is different, something is done differently. Whether you use slightly different value, put it into a different field, or use a different account that has permission issues.

  • Check the value by using the steps 1&2 from this comment. Even an extra space could be the problem.
  • Check the field name by downloading test case WITD and looking the field up in VIEW section. The field used in VIEW section could be different from Microsoft.VSTS.TCM.Steps. Like Custom.Steps or even System.Description.
  • Use the same account for web access (login and password) and API (login and password for tfs 2015 and older, access token otherwise).

SAnCherepan avatar Mar 31 '20 07:03 SAnCherepan

Is project ID required for a PATCH request like it is required for creating work items mentioned in #77 ?

LakshmiAntin avatar Mar 31 '20 20:03 LakshmiAntin

Yes it is required. Because TFS needs to know which WITD to use when creating a work item.

SAnCherepan avatar Mar 31 '20 21:03 SAnCherepan

No it still did not help. The following is returned as a response from PATCH request 'Microsoft.VSTS.TCM.Steps':'<steps id="0"> <step id="1" type="ValidateStep"><parameterizedString isformatted="true">Input step 1</parameterizedString><parameterizedString isformatted="true">Passed</parameterizedString><description/></step> <step id="2" type="ValidateStep"><parameterizedString isformatted="true">Input step 2</parameterizedString><parameterizedString isformatted="true">Failed</parameterizedString><description/></step></steps>'

However, after calling client.get_workitem(XXXXX) the following is returned for 'Microsoft.VSTS.TCM.Steps': '<steps id="0" last="0"/>'

for the same workitem XXXXX

LakshmiAntin avatar Mar 31 '20 21:03 LakshmiAntin

Could you share the test case WITD? Do not forget to remove any sensitive data from it.

SAnCherepan avatar Mar 31 '20 21:03 SAnCherepan

client.rest_client.send_patch(URL,data,headers = headers,payload = payload,project = project)

Does the above URL needs to have a path project/projectId/_apis/wit/workitems/XXXXX ?``

LakshmiAntin avatar Mar 31 '20 23:03 LakshmiAntin

Based on REST API doc, update URL can have both collection and project specified but only collection is required.

SAnCherepan avatar Apr 01 '20 07:04 SAnCherepan