package pt.fccn.mobile.arquivo.tests.pagesearch;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

import pt.fccn.arquivo.selenium.Retry;
import pt.fccn.arquivo.selenium.WebDriverTestBaseParalell;
import pt.fccn.mobile.arquivo.utils.LocaleUtils;

/**
 *
 * @author Ivo Branco <ivo.branco@fccn.pt>
 *
 */
public class PageSearchEmptyTest extends WebDriverTestBaseParalell {

	private static final String QUERY = "xptoxptoxptoxptoxptoxptoxptoxptoxpto";

	public PageSearchEmptyTest(String os, String version, String browser, String deviceName, String deviceOrientation) {
		super(os, version, browser, deviceName, deviceOrientation);
	}

	@Test
	@Retry
	public void pageSearchTestPT() {
		LocaleUtils.changeLanguageToPT(this);
		pageSearchTest("Não foram encontrados resultados para a sua pesquisa");
	}

	@Test
	@Retry
	public void pageSearchTestEN() {
		LocaleUtils.changeLanguageToEN(this);
		pageSearchTest("No results were found for the query");
	}

	private void pageSearchTest(String noResultsMessage) {
		run("Search with " + QUERY, () -> {
			driver.findElement(By.id("txtSearch")).clear();
			driver.findElement(By.id("txtSearch")).sendKeys(QUERY);
			driver.findElement(By.xpath("//*[@id=\"buttonSearch\"]/button")).click();
		});

		run("Wait for search results appear", () -> waitUntilElementIsVisibleAndGet(By.id("resultados-lista")));

		appendError("Check result count should be zero", () -> {
			int count = driver.findElementsByXPath("//*[@id=\"resultados-lista\"]/ul/li").size();
			assertEquals("Check result count should be zero", 0, count);
		});

		By emptyResultMessageBy = By.id("conteudo-pesquisa-erro");

		appendError("Empty result message should be visible",
				() -> ExpectedConditions.visibilityOfElementLocated(emptyResultMessageBy));

		appendError(() -> assertThat("Empty result message should contains specific text",
				driver.findElement(By.id("conteudo-pesquisa-erro")).getText(), containsString(noResultsMessage)));

		appendError(() -> assertThat("Empty result message should show search criteria",
				driver.findElement(By.id("conteudo-pesquisa-erro")).getText(), containsString(QUERY)));
	}

}

@@ -1782,16 +1782,18 @@
 %09%7D);%0A%0A%09%09
+//
 By empty
@@ -1842,16 +1842,73 @@
 -erro%22);
+%0A%09%09By emptyResultMessageBy = By.id(%22conteudo-resultado%22);
 %0A%0A%09%09appe
@@ -2033,25 +2033,27 @@
 essageBy));%0A
+%09%09
 %0A
-
 %09%09appendErro
@@ -2143,35 +2143,48 @@
 .findElement(By.
-id(
+xpath(%22//*%5B@id=%5C
 %22conteudo-pesqui
@@ -2173,37 +2173,43 @@
 =%5C%22conteudo-
-pesquisa-erro
+resultado%5C%22%5D/div%5B3%5D
 %22)).getText(
@@ -2243,24 +2243,26 @@
 Message)));%0A
+%09%09
 %0A%09%09appendErr
@@ -2359,19 +2359,32 @@
 ment(By.
-id(
+xpath(%22//*%5B@id=%5C
 %22conteud
@@ -2385,29 +2385,35 @@
 onteudo-
-pesquisa-erro
+resultado%5C%22%5D/div%5B3%5D
 %22)).getT

Fix PageSearchEmptyTest