const { Builder, By, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

const webdriver = process.env.CHROME_DRIVER
const service = new chrome.ServiceBuilder(webdriver);

describe("Find Landing Page", () => {
    let driver

    beforeAll(async () => {
        driver = new Builder().forBrowser('chrome').setChromeService(service).build();
    })

    afterAll(async () => {
        await driver.quit();
    })

    it("should find the landing page", async () => {

        await driver.get("http://localhost:3000")
        const landingPage = await driver.wait(until.elementLocated(By.id("LandingPage")), 5000)
        expect(landingPage).toBeDefined();
        console.log("Landing page found")
    })

})

describe("Login Tests", () => {
    let driver

    beforeAll(async () => {
        driver = new Builder().forBrowser('chrome').setChromeService(service).build();
        await driver.get("http://localhost:3000")
    })

    afterEach(async () => {
        await driver.quit();
    })

    it("should find the landing page", async () => {
        const loginDialogButton = await driver.wait(until.elementLocated(By.id("OpenLoginDialogButton")), 5000)
        expect(loginDialogButton).toBeDefined();
        console.log("OpenLoginDialogButton found")
    })

    it("should be able to click the Login-Dialog-Button", async () => {
        const loginDialogButton = await driver.wait(until.elementLocated(By.id("OpenLoginDialogButton")), 5000)
        expect(loginDialogButton.isEnabled()).toBeTruthy();
        loginDialogButton.click();
        console.log("OpenLoginDialogButton can be clicked")
    })

    it("should find the login dialog", async () => {
        const loginDialogButton = await driver.wait(until.elementLocated(By.id("OpenLoginDialogButton")), 5000)
        loginDialogButton.click();
        const loginDialog = await driver.wait(until.elementLocated(By.id("LoginDialog")), 5000)
        expect(loginDialog).toBeDefined();
    })

    it("should be able to enter username (admin) and password (123)", async () => {
        const loginDialogButton = await driver.wait(until.elementLocated(By.id("OpenLoginDialogButton")), 5000)
        loginDialogButton.click();
        const userIdTextField = await driver.wait(until.elementLocated(By.id("LoginDialogUserIDText")), 5000)
        expect(userIdTextField).toBeDefined();
        await userIdTextField.sendKeys("admin");
        const passwordTextField = await driver.wait(until.elementLocated(By.id("LoginDialogPasswordText")), 5000)
        expect(passwordTextField).toBeDefined();
        await passwordTextField.sendKeys("123");
    })

    it("should be able to click Login Button", async () => {
        const loginDialogButton = await driver.wait(until.elementLocated(By.id("OpenLoginDialogButton")), 5000)
        loginDialogButton.click();
        const userIdTextField = await driver.wait(until.elementLocated(By.id("LoginDialogUserIDText")), 5000)
        expect(userIdTextField).toBeDefined();
        await userIdTextField.sendKeys("admin");
        const passwordTextField = await driver.wait(until.elementLocated(By.id("LoginDialogPasswordText")), 5000)
        expect(passwordTextField).toBeDefined();
        await passwordTextField.sendKeys("123");
        const loginButton = await driver.wait(until.elementLocated(By.id("PerformLoginButton")), 5000)
        expect(loginButton).toBeDefined();
    })
})