package pt.fccn.sobre.arquivo.tests;

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

import java.util.List;

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

import pt.fccn.arquivo.selenium.Retry;
import pt.fccn.arquivo.selenium.WebDriverTestBaseParalell;


/**
 * 
 * @author pedro.gomes.fccn@gmail.com
 *
 */

public class Soft404MessageTest extends WebDriverTestBaseParalell {

	private static final String WAYBACK_404_PAGE_EXAMPLE = "/about-the-archive/publications-1/documents/peopleware-rarc-article-in-jpg/image_preview";
	
	public Soft404MessageTest(String os, String version, String browser, String deviceName, String deviceOrientation) {
		super(os, version, browser, deviceName, deviceOrientation);
		
	}

	@Test
	@Retry
	public void soft404MessageTest() {
		
		//The test does not work in preprod.sobre.arquivo.pt. Since the Arquivo.pt only collect the sobre.arquivo.pt.
		if (!this.testURL.toLowerCase().contains("preprod.sobre.arquivo.pt")) {
			driver.get(this.testURL + WAYBACK_404_PAGE_EXAMPLE);
			
			waitUntilElementIsVisibleAndGet(By.id("post-9818"));
			 
			//The language of the page is not based on the "English or Portugues" button, but depends on the language of the search engine. 
			
			appendError(() -> assertThat("Verify text from Arquivo.pt link",
					driver.findElement(By.id("onArquivo")).getText(),
					containsString("Visite uma versão anterior desta página em 28 Março, 2017")));

			List<WebElement> wes = driver.findElementsByXPath("//*[@id=\"onArquivo\"]");
			WebElement we = wes.get(0);
			String href = we.getAttribute("href");
			assertEquals("Check link to wayback", href, "https://arquivo.pt/wayback/20170227184149/https://sobre.arquivo.pt" + WAYBACK_404_PAGE_EXAMPLE);
			
			//Test search
			appendError(() -> assertEquals("Verify text from search", "Maybe try searching?",
					driver.findElement(By.id("messageSearch")).getText()));
			
			appendError("Check if page is not archived", () -> new WebDriverWait(driver, 20)
					.until(ExpectedConditions.visibilityOfElementLocated(By.id("___gcse_2"))));
			
		}
	}
}
@@ -1570,108 +1570,144 @@
 (By.
-id(%22onArquivo%22)).getText(),%0A%09%09%09%09%09containsString(%22Visite uma vers%C3%A3o anterior desta p%C3%A1gina em 28 Mar%C3%A7o
+xpath(%22//*%5B@id=%5C%22post-9818%5C%22%5D/div/div/p%5B2%5D/div/a%22)).getText(),%0A%09%09%09%09%09containsString(%22Visit an earlier version of this page on 27 February
 , 20
@@ -1783,20 +1783,39 @@
 d=%5C%22
-onArquivo%5C%22%5D
+post-9818%5C%22%5D/div/div/p%5B2%5D/div/a
 %22);%0A
@@ -1980,14 +1980,16 @@
 4149
+mp_
 /http
-s
 ://s
@@ -2094,55 +2094,33 @@
 ls(%22
-Verify text from search%22, %22Maybe try searching?
+Ou experimente pesquisar.
 %22,%0A%09

fix 404Test