Forum Discussion

_lauren11's avatar
_lauren11
Occasional Contributor
4 years ago

Verification of data having page number

In my web service, It displays a list of items and there is a page number concept implemented.

request parameter can be of type

{

"page_number":""

}

page size:-50 

So, suppose, we have total of 467(no. can vary) items Name in database to be displayed. 

If we insert input as 2, items from 51 to 100 will be displayed, and so on

In Soap U I  automation, 

how can I verify the 'names' of all the items displayed in web service, against what being stored in database. 

Please help me with the coding

2 Replies

  • appliedMagic's avatar
    appliedMagic
    Occasional Contributor

    Hello,

    since there is not a lot of information, e.g. about the response structure, I went and created a solution for the open library API, this here: https://openlibrary.org/developers/api

     

    If we search for http://openlibrary.org/search.json?q=hobbit&page=1 we get a response in this form:

     

    {
        "numFound": 287,
        "start": 0,
        "docs": [
            {
                "has_fulltext": false,
                "title": "Hobbit",
                ...
            },
            {
                "has_fulltext": false,
                "title": "Hobbit Virtues",
                ...
            }]...
    }

     

     

    So "start" represents the current page/offset, "numFound" the total amount, "docs/title" is the value we want to grab. The project looks like this:

    The script in "script" looks like this:

     

    import com.eviware.soapui.support.XmlHolder
    import net.sf.*
    import net.sf.json.*
    import net.sf.json.groovy.*
    
    
    // here is a list of unique entries we expect to find
    // we left out almost all titles on purpose
    def uniqueTitles = ["Hobbit", "The Hobbit", "Untangling Tolkien"]
    
    
    def missedTitles = [] // a list of titles we do not have in our uniqueTitles list, e.g. should include "Der Hobbit" or "El Hobbit" etc.
    
    
    //since we did NOT fire any requests till now, everything is set to 0
    def numFound = 0
    def start = -1		// set to -1 since groovy has no do .. while, at least not in the current version used here?
    def docsFound = 0
    
    def pageCount = 1	//page count starts with 1 NOT 0
    def request = context.testCase.testSuite.getTestCaseByName('request').getTestStepByName('request')
    def params = request.getHttpRequest().getParams()
    params.getProperty('q').value = 'hobbit'	// search for hobbit, we should find around 290 entries
    params.getProperty('page').value = pageCount	// start with first page
    
    //request pages until we found every entry
    while((start + docsFound) < numFound) {
    	//request page x and read response
    	request.run(null, context)
    	def response = request.testRequest.response.contentAsString
    	def jsonSlurper = new JsonSlurper().parseText(response)
    
    	//get book titles
    	def titles = jsonSlurper.docs.collect{ it.title}
    	titles.removeAll(uniqueTitles)
    
    	if(!titles.isEmpty()) {
    		missedTitles.addAll(titles)
    	}
    
    	//get all necessary params to check if we still have to request further entities
    	numFound = Integer.valueOf(jsonSlurper.numFound)
    	start = Integer.valueOf(jsonSlurper.start)
    	docsFound = Integer.valueOf(jsonSlurper.docs.size())
    	params.getProperty('page').value = ++pageCount
    }
    
    //all titles we defined at the start
    log.info "${uniqueTitles.size()}: unique titles: ${uniqueTitles}"
    //all titles that we found but were NOT present in uniqueTitles
    log.info "${missedTitles.size()}: missed titles: ${missedTitles}"
    
    log.info("done")

     

     

    The output, at the end, after the script has run, should look something like this:

     

     

    3: unique titles: [Hobbit, The Hobbit, Untangling Hobbit]
    287: missed titles: [Hobbit Sketchbook, Hobbit Virtues, Hobbit lessons,...

     

     

    I added the project.

     

    EDIT

    Forgot to mention, now you can go over uniqueTitles and missedTitles to compare the entries and do further checks/corrections depending on what exactly you need.