david hol david hol - 4 months ago 14
Scala Question

Loop over all pages via recursion

I have several pages and i want to loop over all pages via recursion:

So i have this

isNextPageExist
function to check if i have another page to visit:

def isNextPageExist(): Boolean = {

}


And get next page function that click on some
WebElement
and get the next page:

def getNextPage(): Unit = {

}


Now inside this function i want to visit first page and do my stuff and in case i have another page(s) call the function again and again, and inside each page make some calculation and return
Double
"

def getAllPages(value: Double):Double = {

if(isNextPageExist)
// get all elements form page inside `value`
getNextPage // get the next page
getAllPages(value) // call the function again
}


Usage:

getAllPages(0.0)


This is only the
pseudo
code and i will glad for some help.

Answer

David, try to avoid Unit return type if your function is named like getNextPage(). Better use Option[Page] and return either Some(page) or None. But the simplest code could be better like following:

case class Page(url:String)

def calculateValue:Double = {here you calculate your value on the current page}

def processPage(page: Option[Page]):List[Double]{
  page match{
    case Some(p) => {
      driver.get(page.url)
      val linksToNextPage = driver.findElements(By.xpath("//your xpath goes here")
      if(linksToNextPage.size > 0){
        val nextPageLink = Page(linksToNexPage.head.getText())
        calculateValue() :: processPage(Some(Page(nextPageLink))
      } else{
        calculateValue() :: processPage(None)
      }
    }
    case None => Nil
}

Then you call it with the first web page as processPage(Some(Page("url to first page"))) and in the result you will have a list of values calculated for each page. My code is also a pseudo-code but you should be able to make it work.