1

I've implemented this method:

@FindBy(partialLinkText = "Avocode")
List<WebElement> results;

public List<String> getSearchResults (){
    List<String> text = results.stream().map(WebElement::getText).collect(Collectors.toList());
    return text;
}

Then I have a test trying to find any match with 'avocode':

assertThat(find.getSearchResults(),IsCollectionContaining.hasItem("Avocode"));

My imported libraries:

import org.hamcrest.core.IsCollectionContaining;
import org.testng.Assert;
import org.testng.annotations.Test;
import pageObjects.Search;

import static org.junit.Assert.assertThat;

++ My dependencies:

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

I expect that test will run successfully, although I'm getting:

java.lang.AssertionError:
Expected: a collection containing "Avocode"

but: was "Avocode \"ready to eat\" DUO pack", was "Avocode Hass, was "Avocode Fuerte", was "Avocode Bacon"

I believe I have followed all tips here in stack-overflow, but nothing is working - Avocode is there, so it should pass...

I will appreciate any help (I'm new in programming).

Thanks, Lijana

Lijana
  • 11
  • 2
  • use startsWith("Avocado") `assertThat(find.getSearchResults(),IsCollectionContaining.hasItem(startsWith("Avocado") ));` – Dirk Deyne Apr 27 '18 at 19:44
  • Hello Dirk, thanks a lot, that passed! But I can imagine situations when it doesn't start with that specified string, it could be anywhere, I just need one good match. So I don't really understand why this is having error: ...,IsCollectionContaining.hasItem("Avocode")); ..you do? – Lijana Apr 28 '18 at 09:41
  • List ingredients = Arrays.asList("Appel",**"Avocado"**,"Rice"); assertThat(ingredients,IsCollectionContaining.hasItem(**"Avocado"**)); List menu = Arrays.asList("Appel pie",**"Avocado salade"**,"Rice & chicken"); assertThat(menu,IsCollectionContaining.hasItem(**startsWith("Avocado") )**); ` – Dirk Deyne Apr 28 '18 at 17:50
  • _**Avocado**_ is not equals to _**Avocado "ready to eat" DUO pack**_, but **Avocado "ready to eat" DUO pack** starts with _**Avocado**_ – Dirk Deyne Apr 28 '18 at 17:59
  • Yeah, I know I have to use only **hasItem("Avocode"));**, but for that I'm getting an error :( Maybe it's connected to *List* results; but I solve it by converting to list... .collect(Collectors.toList()); Hope you understand me now. – Lijana Apr 28 '18 at 21:12

0 Answers0