How to pass parameter from one project to another

2019-02-19 17:29发布

问题:

I am having 4 Rest API’s Projects as of attached screen shot.

I Need to pass a generated userID from project 1 (1_Admin Basics & Get APIs) to other project (2_Courses & Classes).

I am running each project by using test runner. Global property transfer is not working in this scenario. Can some 1 help me out to do the above mentioned scenario.

I don’t want to combine 4 projects into 1 project. It should be 4 projects and need to pass the parameter from 1 project to other.

回答1:

You can do it for example using the workspace property to access the other projects from the current one to get the properties from its. You can do it through a groovy script.

In the first project add a groovy script testStep in your testCase and set the property at project level as follows:

testRunner.testCase.testSuite.project.setPropertyValue('myProp','myValue')

In the second project add another groovy script in the testCase to get the value of your property through the workspace and assign at your desired level (for example in a testSuite, but you can add wherever you need it):

// get the other project
def otherProject = testRunner.testCase.testSuite.project.workspace.getProjectByName('SecondProjectName')
// get your property value
def value = otherProject.getPropertyValue('myProp')
log.info value
// set the var at some level in this case in testSuite
testRunner.testCase.testSuite.setPropertyValue('varFromOtherProject',value)

Then use the variable in the testStep of your second project using the follow notation:

${#TestSuite#varFromOtherProject}

UPDATE:

Seems that you're running your projects alone using testRunner therefore you can't access other projects using workspace, so as a possible workaround you can write your property value in a File in your first project and then read the File content in your second project.

So in your first project add a groovy script testStep with the follow code:

new File('./myProp').text = 'someValue'

Then in your second project read the File and set the property at some test level:

// get the value from the file
def value = new File('./myProp').text
log.info value
// set the var at some level in this case in testSuite
testRunner.testCase.testSuite.setPropertyValue('varFromOtherProject',value)

And as it's explained before use the follow notation in your SOAP testStep to get the property:

${#TestSuite#varFromOtherProject}

Hope it helps,