Forum Discussion

krogold's avatar
krogold
Regular Contributor
6 years ago

Is it possible to overload default environment propgrammatically ?

Hello, My project contain tests that can be executed in several environments. So far I've defined three environments pointing to different equipments that have different endpoints and credentials. ...
  • krogold's avatar
    krogold
    6 years ago

    Well,

    I did not manage to understand it enough to apply it to my case, so I ended up with a dynamic setting.

    I removed the use of Enviroment parameter in testRunner (now it uses default) and I provide as additional parameters the endpoint and credentials for my new environment.

    Then I parse my requests and when I hit the first one, if data is provided and the endpoint is different from the default one, I update values with the parameters provided.

    I noticed that changing it on one API impacts all the other APIs so I do it only once. As it is done with testRunner, modification is not persistent which perfectly suits my needs.

     

    Here's the procedure :

    in all my setup scripts I call configuration test case that I provide with the parameters and then I set the properties to the step

    def endpoint = context.expand( '${#TestCase#endpoint}' )
    def username = context.expand( '${#TestCase#username}' )
    def password = context.expand( '${#TestCase#password}' )
    
    ... <checking the completness of data>
    
    	endpoint = "http://"+endpoint
    	
    	// updating endpoint and credentials require to modify only once
    	found = false 
    	project.testSuites.find(){
    		ts ->
    		ts.getValue().getTestCaseList().find(){
    			tc ->
    			
    			tc.getTestStepList().find(){
    				step ->
    				//log.info step.config.type
    
    				if ((tc.name.contains("/m2m/fim/items"))&&( step.config.type == "restrequest"))
    				{
    					current_endpoint = step.getPropertyValue("endpoint")
    	
    					// update environment if it is not the one expected 
    					if (current_endpoint != endpoint)
    					{
    						step.setPropertyValue("endpoint", endpoint)
    	
    						if( (username == "")||(password == ""))
    						{
    							msg = "the current Username and/or Password have not been set for environment $endpoint"
    							testRunner.fail(msg)
    							log.error msg
    						}
    						else
    						{
    							step.setPropertyValue("Username", username)
    							step.setPropertyValue("Password", password)
    							log.info "updated environment to $endpoint"
    						}
    					}
    					found = true
    				}
    			}
    		}
    	}
    

    Thanks for the help nmrao