alexhacki
alexhacki

Reputation: 13

How to check if list element is present or not

I have page object I want to know how to write a method if no elemenets are displayed or present pass the test case otherwise keep testing.

I'm trying to figure out or we should do try and catch or assertTrue.

@AndroidFindBy(id = "lakjdfaj");
List<MobileElement> texts;

public boolean doesNotPresent() {
Boolean notDisplayed = texts.isEmpty();
if(notDisplayed){
return true;
} else {
return false; 
}

Upvotes: 0

Views: 332

Answers (1)

Amit Jain
Amit Jain

Reputation: 4587

@AndroidFindBy(id = "lakjdfaj");
List<MobileElement> texts;

// verify when no elements are shown

@Test      
public boolean verifyEmptyTexts() {
assertEquals(0,texts.size());
}

// verify when elements are shown, 2nd elements is verified here

@Test 
public boolean verifyTexts() {
assertEquals("textToVerify",texts.get(1).getText());
}

Upvotes: 3

Related Questions