import cz.fi.muni.automation.tester.test.AbstractTest;
import cz.fi.muni.automation.core.configuration.*;
import cz.fi.muni.automation.converter.entity.*;
import cz.fi.muni.automation.tester.db.*;
import java.io.*;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.junit.Assert;
import java.util.List;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.io.*;
import org.openqa.selenium.*;

public class csfd extends AbstractTest {

@Override
public String runTest() throws InterruptedException {
if (usingDatabase && DbName.NO_DATABASE.toString().equals(ConfigurationService.getValueFromConfiguration(ConfigurationConstants.CONF_DBNAME).toUpperCase())) {
return "Test needs database and no database is configured";
}
try {
int i = 1;
List<Action.Path> list = new ArrayList<>();
Action.Path p = new Action.Path();
ResultSet set;
try {
super.before();
super.prepareLogFileForTest("csfd");
driver.get("http://www.csfd.cz");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (otevře stránku csfd)" + ConfigurationService.newline()).getBytes());
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(ConfigurationConstants.LOGS_FOLDER + this.getClass().getName().toLowerCase() + "/" + this.getClass().getName().toLowerCase() + "_" + new SimpleDateFormat("yyyy_MM_dd_HH-mm-ss").format(Calendar.getInstance().getTime()) + "_obrazek.png"));
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='main-menu']/li[1]/a"); list.add(p);
super.getElement(list).click();
Thread.sleep(3000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
try {
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='news-4591']/div[1]/a"); list.add(p);
super.getElement(list).click();
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
} catch (final org.openqa.selenium.WebDriverException ex) {
logFile.write(("Test step " + i++ + " finished with result: FAIL " + ConfigurationService.newline()).getBytes());
logFile.write((ex.getMessage() + ConfigurationService.newline()).getBytes());
}
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.ID); p.setValue("mainSearch"); list.add(p);
super.getElement(list).sendKeys("Kingsman");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS (vyhledá film Kingsman)" + ConfigurationService.newline()).getBytes());
list = new ArrayList<>();p = new Action.Path(); p.setType(PathType.XPATH); p.setValue(".//*[@id='main-search']/form/div/input[2]"); list.add(p);
super.getElement(list).click();
Thread.sleep(1000);
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
driver.get("http://www.seznam.cz");
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
driver.navigate().back();
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
driver.navigate().forward();
logFile.write(("Test step " + i++ + " finished with result: SUCCESS " + ConfigurationService.newline()).getBytes());
} catch (final org.openqa.selenium.WebDriverException ex) {
logFile.write(("Test step " + i++ + " finished with result: FAIL" + ConfigurationService.newline()).getBytes());
logFile.write((ex.getMessage() + ConfigurationService.newline()).getBytes());
} catch (final Exception ex) {
logFile.write(("Test step " + i++ + " finished with result: FAIL" + ConfigurationService.newline()).getBytes());
logFile.write((ex.getMessage() + ConfigurationService.newline()).getBytes());
} finally {
super.after();
logFile.flush();
logFile.close();
}
} catch (final IOException ex) {
System.err.println(ex.toString());
}
return "Test finished";
}

}
