本文介绍了如何使用Rally的JAVA API将标签添加到Rally的TestCase?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力在Rally的测试用例中添加一个标签。标签已存在于标签集合中。但我不能将它添加到测试用例。

解决方案

div>

这是一个如何做的例子 - 这个例子显示了添加标签到一个现有的测试案例,以及创建一个新的测试案例和添加标签到新的测试案例。

  public class RestExample_AddTagsToTestCase {

public static void main(String [] args)throws URISyntaxException,IOException {

//创建并配置RallyRestApi的新实例
//连接参数
String rallyURL =https://rally1.rallydev.com;
String wsapiVersion =1.34;
String applicationName =RestExample_AddTagsToTestCase;

//凭据
String userName [email protected];
String userPassword =password;

RallyRestApi restApi = new RallyRestApi(
new URI(rallyURL),
userName,
userPassword);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);

//用户设置
String testerUserName [email protected];

//工作区和项目设置
String myWorkspace =我的工作区;
String myProject =我的项目;

//要查询的现有测试用例的格式化ID
String existsTestCaseFormattedID =TC172;

//标记感兴趣的名称
String tagNameAlpine =Alpine;
String tagNameMontane =Montane;

//读取用户
QueryRequest userRequest = new QueryRequest(User);
userRequest.setFetch(new Fetch(UserName,Subscription,DisplayName));
userRequest.setQueryFilter(new QueryFilter(UserName,=,testerUserName));
QueryResponse userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userQueryObject = userQueryElement.getAsJsonObject();
String userRef = userQueryObject.get(_ ref)。toString();

//获取对感兴趣的工作空间的引用
QueryRequest workspaceRequest = new QueryRequest(Workspace);
workspaceRequest.setFetch(new Fetch(Name,Owner,Projects)));
workspaceRequest.setQueryFilter(new QueryFilter(Name,=,myWorkspace));
QueryResponse workspaceQueryResponse = restApi.query(workspaceRequest);
String workspaceRef = workspaceQueryResponse.getResults()。get(0).getAsJsonObject()。get(_ ref)。toString();

//获取对感兴趣项目的引用
QueryRequest projectRequest = new QueryRequest(Project);
projectRequest.setFetch(new Fetch(Name,Owner,Projects)));
projectRequest.setQueryFilter(new QueryFilter(Name,=,myProject));
QueryResponse projectQueryResponse = restApi.query(projectRequest);
String projectRef = projectQueryResponse.getResults()。get(0).getAsJsonObject()。get(_ ref)。toString();

//获取对感兴趣的标签的引用
QueryRequest tagRequest = new QueryRequest(Tag);
tagRequest.setFetch(new Fetch(Name));

//查找名为Alpine的标签
tagRequest.setQueryFilter(new QueryFilter(Name,=,tagNameAlpine));
QueryResponse tagQueryResponse = restApi.query(tagRequest);
JsonObject tagAlpineJsonObject = tagQueryResponse.getResults()。get(0).getAsJsonObject();
JsonElement tagAlpine = tagQueryResponse.getResults()。get(0).getAsJsonObject();
String tagAlpineRef = tagAlpineJsonObject.get(_ ref)。toString();

//查找名为Montane的第二个标签
tagRequest.setQueryFilter(new QueryFilter(Name,=,tagNameMontane));
tagQueryResponse = restApi.query(tagRequest);
JsonObject tagMontaneJsonObject = tagQueryResponse.getResults()。get(0).getAsJsonObject();
JsonElement tagMontane = tagQueryResponse.getResults()。get(0).getAsJsonObject();
String tagMontaneRef = tagMontaneJsonObject.get(_ ref)。toString();

//查询现有测试用例
QueryRequest existsTestCaseRequest = new QueryRequest(TestCase);
existsTestCaseRequest.setFetch(new Fetch(FormattedID,Name,Tags));
existsTestCaseRequest.setQueryFilter(new QueryFilter(FormattedID,=,existsTestCaseFormattedID));
QueryResponse testCaseQueryResponse = restApi.query(existTestCaseRequest);
JsonObject existsTestCaseJsonObject = testCaseQueryResponse.getResults()。get(0).getAsJsonObject();
String existsTestCaseRef = testCaseQueryResponse.getResults()。get(0).getAsJsonObject()。get(_ ref)。toString();
JsonArray existsTagsJsonArray =(JsonArray)testCaseQueryResponse.getResults()。get(0).getAsJsonObject()。get(Tags);

//更新现有测试用例中的标签
try {

//向现有测试用例的标签集添加其他标签
existsTagsJsonArray.add(tagMontane );

//添加标记
System.out.println(\\\
Updating Existing Test Case's tags ...);
JsonObject updateExistTestCase = new JsonObject();

updateExistTestCase.add(Tags,existTagsJsonArray);
UpdateRequest updateExistTestCaseRequest = new UpdateRequest(existsTestCaseRef,updateExistTestCase);
UpdateResponse updateExistTestCaseResponse = restApi.update(updateExistTestCaseRequest);

if(updateExistTestCaseResponse.wasSuccessful()){
System.out.println(Update Test Case with new Tags);
} else {
String [] updateExistTestCaseErrors;
updateExistTestCaseErrors = updateExistTestCaseResponse.getErrors();
System.out.println(更新测试用例时发生错误);
for(int i = 0; i< updateExistTestCaseErrors.length; i ++){
System.out.println(updateExistTestCaseErrors [i]);
}
}
} catch(Exception e){
System.out.println(更新现有测试用例上的标签时发生异常);
e.printStackTrace();
}

//创建一个新的测试用例并向其添加标签
try {

//创建测试用例
系统.out.println(Creating Test Case ...);
JsonObject newTestCase = new JsonObject();
newTestCase.addProperty(Name,TC0400-1200:SeleniumGroup004);
newTestCase.addProperty(Method,Automated);
newTestCase.addProperty(Type,Performance);
newTestCase.addProperty(Workspace,workspaceRef);
newTestCase.addProperty(Project,projectRef);
newTestCase.addProperty(Owner,userRef);

CreateRequest createRequest = new CreateRequest(testcase,newTestCase);
CreateResponse createResponse = restApi.create(createRequest);

if(createResponse.wasSuccessful()){

System.out.println(String.format(Created%s,createResponse.getObject _ref)。getAsString()));

//读测试用例
String newTestCaseRef = Ref.getRelativeRef(createResponse.getObject()。get(_ ref)。getAsString());
System.out.println(String.format(\\\
Reading Test Case%s ...,newTestCaseRef));
GetRequest getRequest = new GetRequest(newTestCaseRef);
getRequest.setFetch(new Fetch(FormattedID,Name,Tags));
GetResponse getResponse = restApi.get(getRequest);
JsonObject obj = getResponse.getObject();
System.out.println(String.format(Read Test Case。FormattedID =%s,Name =%s,
obj.get(FormattedID)。getAsString(),obj.get (Name)。getAsString()));

// JsonArray for Test Collection on Test Case
JsonArray newTagsJsonArray = new JsonArray();
newTagsJsonArray.add(tagAlpine);
newTagsJsonArray.add(tagMontane);

//添加标签
System.out.println(\\\
Updating Test Case tags ...);
JsonObject updatedTestCase = new JsonObject();

updatedTestCase.add(Tags,newTagsJsonArray);
UpdateRequest updateRequest = new UpdateRequest(newTestCaseRef,updatedTestCase);
UpdateResponse updateResponse = restApi.update(updateRequest);

if(updateResponse.wasSuccessful()){
System.out.println(Update Test Case with new Tags);
} else {
String [] updateErrors;
updateErrors = updateResponse.getErrors();
System.out.println(更新测试用例时发生错误);
for(int i = 0; i< updateErrors.length; i ++){
System.out.println(updateErrors [i]);
}
}

} else {
String [] createErrors;
createErrors = createResponse.getErrors();
System.out.println(创建测试用例时发生错误);
for(int i = 0; i< createErrors.length; i ++){
System.out.println(createErrors [i]);
}
}

} finally {
//释放所有资源
restApi.close();
}
}
}


I have been struggling with adding a tag to a testcase in Rally. The tag is already present in the Tags collection. But I cannot add it to a testcase. Can some provide an example about how to do this?

Thanks a lot.

解决方案

Here's an example of how to do this - the example shows both adding Tags to an existing Test Case, as well as creating a new Test Case and adding Tags to the new one.

public class RestExample_AddTagsToTestCase {

    public static void main(String[] args) throws URISyntaxException, IOException {

        // Create and configure a new instance of RallyRestApi
        // Connection parameters
        String rallyURL = "https://rally1.rallydev.com";
        String wsapiVersion = "1.34";
        String applicationName = "RestExample_AddTagsToTestCase";

        // Credentials
        String userName = "[email protected]";
        String userPassword = "password";

        RallyRestApi restApi = new RallyRestApi(
                new URI(rallyURL),
                userName,
                userPassword);
        restApi.setWsapiVersion(wsapiVersion);
        restApi.setApplicationName(applicationName);

        // User settings
        String testerUserName = "[email protected]";

        // Workspace and Project Settings
        String myWorkspace = "My Workspace";
        String myProject = "My Project";

        // FormattedID of Existing Test Case to Query
        String existTestCaseFormattedID = "TC172";

        // Tag Names of Interest
        String tagNameAlpine = "Alpine";
        String tagNameMontane = "Montane";

        //Read User
        QueryRequest userRequest = new QueryRequest("User");
        userRequest.setFetch(new Fetch("UserName", "Subscription", "DisplayName"));
        userRequest.setQueryFilter(new QueryFilter("UserName", "=", testerUserName));
        QueryResponse userQueryResponse = restApi.query(userRequest);
        JsonArray userQueryResults = userQueryResponse.getResults();
        JsonElement userQueryElement = userQueryResults.get(0);
        JsonObject userQueryObject = userQueryElement.getAsJsonObject();
        String userRef = userQueryObject.get("_ref").toString();

        // Get reference to Workspace of interest
        QueryRequest workspaceRequest = new QueryRequest("Workspace");
        workspaceRequest.setFetch(new Fetch("Name", "Owner", "Projects"));
        workspaceRequest.setQueryFilter(new QueryFilter("Name", "=", myWorkspace));
        QueryResponse workspaceQueryResponse = restApi.query(workspaceRequest);
        String workspaceRef = workspaceQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();

        // Get reference to Project of interest
        QueryRequest projectRequest = new QueryRequest("Project");
        projectRequest.setFetch(new Fetch("Name", "Owner", "Projects"));
        projectRequest.setQueryFilter(new QueryFilter("Name", "=", myProject));
        QueryResponse projectQueryResponse = restApi.query(projectRequest);
        String projectRef = projectQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();

        // Get reference to Tag(s) of interest
        QueryRequest tagRequest = new QueryRequest("Tag");
        tagRequest.setFetch(new Fetch("Name"));

        // Look for Tag named "Alpine"
        tagRequest.setQueryFilter(new QueryFilter("Name", "=", tagNameAlpine));
        QueryResponse tagQueryResponse = restApi.query(tagRequest);
        JsonObject tagAlpineJsonObject = tagQueryResponse.getResults().get(0).getAsJsonObject();
        JsonElement tagAlpine = tagQueryResponse.getResults().get(0).getAsJsonObject();
        String tagAlpineRef = tagAlpineJsonObject.get("_ref").toString();

        // Look for a second Tag named "Montane"
        tagRequest.setQueryFilter(new QueryFilter("Name", "=", tagNameMontane));
        tagQueryResponse = restApi.query(tagRequest);
        JsonObject tagMontaneJsonObject = tagQueryResponse.getResults().get(0).getAsJsonObject();
        JsonElement tagMontane = tagQueryResponse.getResults().get(0).getAsJsonObject();
        String tagMontaneRef = tagMontaneJsonObject.get("_ref").toString();

        // Query for existing Test Case
        QueryRequest  existTestCaseRequest = new QueryRequest("TestCase");
        existTestCaseRequest.setFetch(new Fetch("FormattedID","Name","Tags"));
        existTestCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", existTestCaseFormattedID));
        QueryResponse testCaseQueryResponse = restApi.query(existTestCaseRequest);
        JsonObject existTestCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
        String existTestCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").toString();
        JsonArray existTagsJsonArray = (JsonArray) testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("Tags");               

        // Update Tags on Existing Test Case
        try {

            // Add additional Tag to Existing Test Case's Tag Collection
            existTagsJsonArray.add(tagMontane);

            //Add Tag
            System.out.println("\nUpdating Existing Test Case's tags...");
            JsonObject updateExistTestCase = new JsonObject();

            updateExistTestCase.add("Tags", existTagsJsonArray);
            UpdateRequest updateExistTestCaseRequest = new UpdateRequest(existTestCaseRef, updateExistTestCase);
            UpdateResponse updateExistTestCaseResponse = restApi.update(updateExistTestCaseRequest);

            if (updateExistTestCaseResponse.wasSuccessful()) {
                System.out.println("Updated Test Case with new Tags");
            } else {
                String[] updateExistTestCaseErrors;
                updateExistTestCaseErrors = updateExistTestCaseResponse.getErrors();
                System.out.println("Error occurred updating Test Case: ");
                for (int i=0; i<updateExistTestCaseErrors.length;i++) {
                    System.out.println(updateExistTestCaseErrors[i]);
                }                   
            }
        } catch (Exception e) {
            System.out.println("Exception occurred while updating Tags on existing Test Case: ");
            e.printStackTrace();            
        }

        // Create a New Test Case and Add Tags to it
        try {

            //Create a Test Case                    
            System.out.println("Creating Test Case...");
            JsonObject newTestCase = new JsonObject();
            newTestCase.addProperty("Name", "TC0400-1200: SeleniumGroup004");
            newTestCase.addProperty("Method", "Automated");
            newTestCase.addProperty("Type", "Performance"); 
            newTestCase.addProperty("Workspace", workspaceRef);
            newTestCase.addProperty("Project", projectRef);
            newTestCase.addProperty("Owner", userRef);  

            CreateRequest createRequest = new CreateRequest("testcase", newTestCase);
            CreateResponse createResponse = restApi.create(createRequest);            

            if (createResponse.wasSuccessful()) {

                System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));          

                //Read Test Case
                String newTestCaseRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
                System.out.println(String.format("\nReading Test Case %s...", newTestCaseRef));
                GetRequest getRequest = new GetRequest(newTestCaseRef);
                getRequest.setFetch(new Fetch("FormattedID", "Name", "Tags"));
                GetResponse getResponse = restApi.get(getRequest);
                JsonObject obj = getResponse.getObject();
                System.out.println(String.format("Read Test Case. FormattedID = %s, Name = %s",
                        obj.get("FormattedID").getAsString(), obj.get("Name").getAsString()));

                // JsonArray for Tags collection on Test Case
                JsonArray newTagsJsonArray = new JsonArray();
                newTagsJsonArray.add(tagAlpine);
                newTagsJsonArray.add(tagMontane);

                //Add Tag
                System.out.println("\nUpdating Test Case tags...");
                JsonObject updatedTestCase = new JsonObject();

                updatedTestCase.add("Tags", newTagsJsonArray);
                UpdateRequest updateRequest = new UpdateRequest(newTestCaseRef, updatedTestCase);
                UpdateResponse updateResponse = restApi.update(updateRequest);

                if (updateResponse.wasSuccessful()) {
                    System.out.println("Updated Test Case with new Tags");
                } else {
                    String[] updateErrors;
                    updateErrors = updateResponse.getErrors();
                    System.out.println("Error occurred updating Test Case: ");
                    for (int i=0; i<updateErrors.length;i++) {
                        System.out.println(updateErrors[i]);
                    }                   
                }   

            } else {
                String[] createErrors;
                createErrors = createResponse.getErrors();
                System.out.println("Error occurred creating Test Case: ");
                for (int i=0; i<createErrors.length;i++) {
                    System.out.println(createErrors[i]);
                }
            }

        } finally {
            //Release all resources
            restApi.close();
        }                
    }
}

这篇关于如何使用Rally的JAVA API将标签添加到Rally的TestCase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:58