If I call findEventWithStatus(“Comment”,”Completed”) instead of returning a single event it returns the entire list of events generated by the getEvents() method. Why is this so? Applicable code is below.
Definition of the Event object:-
class Event {
String description
String date
String status
WebElement editEvent
WebElement deleteEvent
}
Get the events from the HTML table using selenium:-
def getEvents() {
def eventSize = driver.findElements(By.xpath('//div[@id[contains(.,"eventsReviewTable")]]/table/tbody/tr')).size()
def eventList = new ArrayList()
def event = new Event()
def x
for (y in 0..eventSize-1) {
x = (y+1).toString()
event.date = driver.findElement(By.xpath("//div[@id[contains(.,'eventsReviewTable')]]/table/tbody/tr["+ x + "]/td[1]/div/span")).getText()
event.description = driver.findElement(By.xpath("//div[@id[contains(.,'eventsReviewTable')]]/table/tbody/tr["+ x + "]/td[2]/div/span")).getText()
event.status = driver.findElement(By.xpath("//div[@id[contains(.,'eventsReviewTable')]]/table/tbody/tr["+ x + "]/td[4]/div/span")).getText()
event.editEvent = driver.findElement(By.xpath("//div[@id[contains(.,'eventsReviewTable')]]/table/tbody/tr["+ x + "]/td[6]/div/a[@id[contains(.,'editEventLink')]]"))
event.deleteEvent = driver.findElement(By.xpath("//div[@id[contains(.,'eventsReviewTable')]]/table/tbody/tr["+ x + "]/td[6]/div/a[@id[contains(.,'deleteEventLink')]]"))
eventList.add(event)
event = new Event()
}
return eventList
}
Finding an event with a specific status and description:-
def findEventWithStatus(String desc, String status) {
def events = getEvents()
events.each() {
if(it.description == desc && it.status == status) {
return it
}
}
}
The problem is that the
return itfrom thefindEventWithStatusmethod is returning from the closure passed toevent.eachinstead of returning from thefindEventWithStatusmethod. So, theeachmethod is not doing anything really, and aseachreturns the collection in which it was invoked, it is returning theeventsvalue, which is what is finally being returned fromfindEventWithStatusas theevents.each { ... }is the last statement in that method. I hope that wasn’t too confusing hehe.Anyway, it can be fixed using a
forstatement instead ofeach, but, as you are searching for an object, thefindmethod will work much better: