package gui;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

public class Analyse {
  public static void main(String[] args) {
    setUpOnce();
  }

  @BeforeAll
  public static void setUpOnce() {
    File pfad = new File("");
    System.out.println(pfad.getAbsolutePath());
    System.setProperty("webdriver.gecko.driver",
        pfad.getAbsolutePath() 
        +"\\lib\\geckodriver.exe"
        );
    FirefoxProfile profile = new FirefoxProfile();
    FirefoxOptions options = new FirefoxOptions();
    options.setProfile(profile); 

    File pfad3 = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    System.out.println("firefox: " + pfad3.exists());

    options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");

    //driver = new FirefoxDriver(service,options);

    FirefoxDriver fire= new FirefoxDriver(options);

    fire.getCapabilities().asMap().forEach((k,v) -> System.out.println(k + " : " + v));

    //WebDriver fd = fire;
    WebDriver fd = new EdgeDriver();  // Alternative, vorherige Zeile auskommentieren
    
    Path path = FileSystems.getDefault().getPath("");
    String dirName = path.toAbsolutePath().toString();
    fd.get("file:///" + dirName + "/webseite/produkteingabe.html");
    WebElement element = fd.findElement(By.id("name"));
    element.clear();
    element.sendKeys("1234");
    element = fd.findElement(By.id("preis"));
    element.clear();
    element.sendKeys("1234");
    fd.findElement(By.id("uebernehmen")).click();
    element = fd.findElement(By.xpath("//*//table/tbody/tr[1]/td[2]"));
    System.out.println("el: " + element.getText()); 
    //fd.close();   // sonst Programm manuell beenden (rotes Quadrat)
    }    
}
