package ru.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by Maria on 20.07.2016.
 */
public class SearchFilm extends TestNgTestBase {
    private WebDriverWait wait;
    private String filmIwant;

    @BeforeSuite
    public void login() {
        driver.get(baseUrl + "/php4dvd/");
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys("admin");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("admin");
        driver.findElement(By.name("submit")).click();
    }

    @Test // фильм найден
    public void testFoundFilm() throws Exception {
        List<WebElement> films = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box")));

        WebElement searchField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("q")));
        searchField.clear();
        filmIwant = "Ice Age";
        searchField.sendKeys(filmIwant + Keys.RETURN);

        Boolean rsltsInvsbl = wait.until(ExpectedConditions.invisibilityOfAllElements(films));
        Assert.assertTrue(rsltsInvsbl);

        List<WebElement> searchResult = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box")));
        for (WebElement webElement : searchResult) {
            Assert.assertTrue(filmIwant.equals(webElement.findElement(By.className("title")).getText()));
        }
    }

    @Test // фильм не найден
    public void testNotFoundFilm(){
        List<WebElement> films = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box")));

        WebElement searchField = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("q")));
        searchField.clear();
        filmIwant = "klflvk";
        searchField.sendKeys(filmIwant + Keys.RETURN);

        Boolean rsltsInvsbl = wait.until(ExpectedConditions.invisibilityOfAllElements(films));
        Assert.assertTrue(rsltsInvsbl);

        WebElement notFound = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("content")));
        String title = notFound.getText();

        Assert.assertTrue(title.equals("No movies where found."));
    }
}

@@ -1338,33 +1338,93 @@
 %22movie_box%22)));%0A
+        filmIwant = %22Ice Age%22;%0A//        addFilm(filmIwant);
 %0A
-
         WebEleme
@@ -1542,39 +1542,8 @@
 ();%0A
-        filmIwant = %22Ice Age%22;%0A
     
@@ -1585,33 +1585,104 @@
 + Keys.RETURN);%0A
+        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
 %0A
-
         Boolean 
@@ -1791,32 +1791,104 @@
 ue(rsltsInvsbl);
+%0A        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
 %0A%0A        List%3CW
@@ -2873,24 +2873,24 @@
 getText();%0A%0A
-
         Asse
@@ -2949,11 +2949,824 @@
 );%0A    %7D
+%0A%0A    private void addFilm(String filmName)%7B%0A%0A        WebElement addMovieBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(%22img%5Balt=%5C%22Add movie%5C%22%5D%22)));%0A        addMovieBtn.click();%0A%0A        WebElement imdbSearch = wait.until(ExpectedConditions.presenceOfElementLocated(By.id(%22imdbsearch%22)));%0A        imdbSearch.clear();%0A        imdbSearch.sendKeys(filmName);%0A%0A        driver.findElement(By.cssSelector(%22input%5Btype=%5C%22submit%5C%22%5D%22)).click();%0A        driver.findElement(By.linkText(filmName)).click();%0A%0A        WebElement saveBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(%22img%5Balt=%5C%22Save%5C%22%5D%22)));%0A        saveBtn.click();%0A%0A        WebElement logoBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.className(%22center%22)));%0A        logoBtn.click();%0A    %7D
 %0A%7D%0A

Add fılm ın test FılmFound