Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
4 years ago
Solved

[TechCorner Challenge #14] Filtering data retrieved from a DataSource

Hi ReadyAPI Community!

 

We’ve prepared an interesting task for you to solve today! Check out the participation rules and the TechCorner Leaderboard here.

 

Imagine that you are working with an app for setting up online conferences, and you need to filter data based on certain criteria. In our case - the location of an online conference participant.

 

Task: Create a script that will filter data retrieved from a DataSource

Difficulty: 

 

Here’s what your script should do:

1. Take an item in a DataSourse and check if it matches the criteria.

2. If it does, run the test. If it doesn't - skip the item.

 

The Excel file to be used as a DataSource is attached below.

 

Condition: Run the test only when participants of an online user conference are located in Boston.

 

Resources: Here are some examples of working with DataSources in Groovy scripts: https://support.smartbear.com/readyapi/docs/testing/scripts/samples/data.html
The full description of the class - here you can find all the properties and methods that the class has:
https://support.smartbear.com/readyapi/apidocs/pro/DefaultPackage/WsdlDataSourceTestStep.html

 

Good luck!

  • Task: Create a script that will filter data retrieved from a DataSource

     

    This is a solution created for [TechCorner Challenge #14]

     

    sonya_m :

     

    So, i have updated the script in order to meet the criteria, please give it a try:

     

     

    def Participant_Location = context.expand( '${DataSource#Participant_Location}' )
    
    if(Participant_Location.toLowerCase() == "boston"){
    	testRunner.testCase.getTestStepByName("Register User").run(testRunner, context);
    	testRunner.testCase.getTestStepByName("Buy a Ticket").run(testRunner, context);
    }
    else{
    //	Disabling them in order them not to run
    	testRunner.testCase.getTestStepByName("Register User").setDisabled(true)
    	testRunner.testCase.getTestStepByName("Buy a Ticket").setDisabled(true)
    }

     

     

7 Replies

  • sonya_m :

     

    Hope i have understand correctly i have created below small script as the solution, have a look :

    def Participant_Location = context.expand( '${DataSource#Participant_Location}' )
    
    if(Participant_Location.toLowerCase() == "boston"){
    	testRunner.testCase.getTestStepByName("TEST__STEP__NAME").run(testRunner, context);
    }
    else{
    //	Do Nothing
    }
    

     

    Below is the Screenshot how my Test Suite looks like:

     

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you HimanshuTayal ! This is a nice script, working great!

       

      However, one tiny change could improve it and make a perfect match for the task🙂

      In your script, one particular step is run, while we expect that all test steps after the script will be run if the criteria are met!

       

      Would you like to try implementing this?

       

      Let me provide an image for better understanding.

       

       

       

      • HimanshuTayal's avatar
        HimanshuTayal
        Community Hero

        Task: Create a script that will filter data retrieved from a DataSource

         

        This is a solution created for [TechCorner Challenge #14]

         

        sonya_m :

         

        So, i have updated the script in order to meet the criteria, please give it a try:

         

         

        def Participant_Location = context.expand( '${DataSource#Participant_Location}' )
        
        if(Participant_Location.toLowerCase() == "boston"){
        	testRunner.testCase.getTestStepByName("Register User").run(testRunner, context);
        	testRunner.testCase.getTestStepByName("Buy a Ticket").run(testRunner, context);
        }
        else{
        //	Disabling them in order them not to run
        	testRunner.testCase.getTestStepByName("Register User").setDisabled(true)
        	testRunner.testCase.getTestStepByName("Buy a Ticket").setDisabled(true)
        }