package com.agile611.testng.webdriver;

import java.net.URL;
import java.time.Duration;

import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class BaseSaucelabsTest {
    public RemoteWebDriver driver;
    @BeforeClass(alwaysRun = true) //Inicialización del navegador
    public void setUp() throws Exception {
        String browser = System.getProperty("browser");
        if (browser != null && browser.equalsIgnoreCase("chrome")) {
            ChromeOptions options = new ChromeOptions();
            driver = new RemoteWebDriver(new URL("http://selgp:ee557a77-606f-451d-9c8b-a4bb3ef03c90@ondemand.saucelabs.com:80/wd/hub"), options);
        }
        else if(browser != null && browser.equalsIgnoreCase("safari")) {
            SafariOptions options = new SafariOptions();
            driver = new RemoteWebDriver(new URL("http://selgp:ee557a77-606f-451d-9c8b-a4bb3ef03c90@ondemand.saucelabs.com:80/wd/hub"), options);
        }
        else if(browser != null && browser.equalsIgnoreCase("ie")) {
            InternetExplorerOptions options = new InternetExplorerOptions();
            driver = new RemoteWebDriver(new URL("http://selgp:ee557a77-606f-451d-9c8b-a4bb3ef03c90@ondemand.saucelabs.com:80/wd/hub"), options);
        }
        else if(browser != null && browser.equalsIgnoreCase("edge")) {
            EdgeOptions options = new EdgeOptions();
            options.setCapability("platform", "Windows 10");
            options.setCapability("version", "15.15063");
            driver = new RemoteWebDriver(new URL("http://selgp:ee557a77-606f-451d-9c8b-a4bb3ef03c90@ondemand.saucelabs.com:80/wd/hub"), options);
        }
        else {
            FirefoxOptions options = new FirefoxOptions();
            driver = new RemoteWebDriver(new URL("http://selgp:ee557a77-606f-451d-9c8b-a4bb3ef03c90@ondemand.saucelabs.com:80/wd/hub"), options);
        }
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
    }

    @AfterClass(alwaysRun = true) //El cierre del navegador
    public void tearDown() throws Exception {
        driver.quit();
    }
}
