/*
 * Copyright (c) 2012-2017 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.selenium.pageobject.site;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.che.selenium.core.SeleniumWebDriver;
import org.eclipse.che.selenium.core.constant.TestTimeoutsConstants;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/** @author Dmytro Nochevnov */
@Singleton
public class CheLoginPage implements LoginPage {

  private final SeleniumWebDriver seleniumWebDriver;
  private final WebDriverWait webDriverWait;

  @FindBy(name = "username")
  private WebElement usernameInput;

  @FindBy(name = "password")
  private WebElement passwordInput;

  @FindBy(name = "login")
  private WebElement loginButton;

  @Inject
  public CheLoginPage(SeleniumWebDriver seleniumWebDriver) {
    this.seleniumWebDriver = seleniumWebDriver;
    PageFactory.initElements(seleniumWebDriver, this);

    webDriverWait =
        new WebDriverWait(seleniumWebDriver, TestTimeoutsConstants.LOAD_PAGE_TIMEOUT_SEC);
  }

  @Override
  public void login(String username, String password) {
    waitOnOpen();

    rewrite(usernameInput, username);

    rewrite(passwordInput, password);

    clickLoginButton();
    waitOnClose();
  }

  @Override
  public boolean isOpened() {
    try {
      waitOnOpen();
    } catch (TimeoutException e) {
      return false;
    }

    return true;
  }

  private void rewrite(WebElement field, String value) {
    webDriverWait.until(ExpectedConditions.visibilityOf(field)).clear();
    waitTextIsPresent(field, "");
    webDriverWait.until(ExpectedConditions.visibilityOf(field)).sendKeys(value);
    waitTextIsPresent(field, value);
  }

  private void clickLoginButton() {
    webDriverWait.until(ExpectedConditions.visibilityOf(loginButton)).click();
  }

  private void waitOnOpen() {
    webDriverWait.until(
        ExpectedConditions.visibilityOfAllElements(
            ImmutableList.of(loginButton, passwordInput, usernameInput)));
  }

  private void waitOnClose() {
    webDriverWait.until(
        ExpectedConditions.invisibilityOfAllElements(
            ImmutableList.of(loginButton, passwordInput, usernameInput)));
  }

  private void waitTextIsPresent(WebElement webElement, String expectedText) {
    webDriverWait.until(
        (ExpectedCondition<Boolean>)
            driver -> webElement.getAttribute("value").equals(expectedText));
  }
}

@@ -421,48 +421,609 @@
 ort 
-com.google.common.collect.ImmutableList;
+static java.util.Arrays.asList;%0Aimport static org.eclipse.che.selenium.pageobject.site.CheLoginPage.LoginPageLocators.LOGIN_BUTTON;%0Aimport static org.eclipse.che.selenium.pageobject.site.CheLoginPage.LoginPageLocators.PASSWORD_FIELD;%0Aimport static org.eclipse.che.selenium.pageobject.site.CheLoginPage.LoginPageLocators.USER_NAME_FIELD;%0Aimport static org.openqa.selenium.support.ui.ExpectedConditions.invisibilityOfElementLocated;%0Aimport static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;%0Aimport static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;%0A
 %0Aimp
@@ -1213,16 +1213,47 @@
 stants;%0A
+import org.openqa.selenium.By;%0A
 import o
@@ -1480,66 +1480,8 @@
 on;%0A
-import org.openqa.selenium.support.ui.ExpectedConditions;%0A
 impo
@@ -1622,17 +1622,16 @@
 nPage %7B%0A
-%0A
   privat
@@ -1727,33 +1727,201 @@
 %0A%0A  
-@FindBy(name = %22username%22
+protected interface LoginPageLocators %7B%0A    String USER_NAME_FIELD = %22username%22;%0A    String PASSWORD_FIELD = %22password%22;%0A    String LOGIN_BUTTON = %22login%22;%0A  %7D%0A%0A  @FindBy(name = USER_NAME_FIELD
 )%0A  
@@ -1972,26 +1972,30 @@
 (name = 
-%22password%22
+PASSWORD_FIELD
 )%0A  priv
@@ -2042,23 +2042,28 @@
 (name = 
-%22login%22
+LOGIN_BUTTON
 )%0A  priv
@@ -2832,35 +2832,16 @@
 t.until(
-ExpectedConditions.
 visibili
@@ -2920,35 +2920,16 @@
 t.until(
-ExpectedConditions.
 visibili
@@ -3060,35 +3060,16 @@
 t.until(
-ExpectedConditions.
 visibili
@@ -3139,56 +3139,137 @@
     
-webDriverWait.until(%0A        ExpectedConditions.
+asList(By.name(USER_NAME_FIELD), By.name(PASSWORD_FIELD), By.name(LOGIN_BUTTON))%0A        .forEach(locator -%3E webDriverWait.until(
 visi
@@ -3280,184 +3280,204 @@
 tyOf
-All
 Element
-s(%0A            ImmutableList.of(loginButton, passwordInput, usernameInput)));%0A  %7D%0A%0A  private void waitOnClose() %7B%0A    webDriverWait.until(%0A        ExpectedConditions.
+Located(locator)));%0A  %7D%0A%0A  private void waitOnClose() %7B%0A    asList(By.name(USER_NAME_FIELD), By.name(PASSWORD_FIELD), By.name(LOGIN_BUTTON))%0A        .forEach(locator -%3E webDriverWait.until(
 invi
@@ -3490,91 +3490,30 @@
 tyOf
-All
 Element
-s(%0A            ImmutableList.of(loginButton, passwordInput, usernameInput
+Located(locator
 )));

rework CheLoginPage selenium page-object (#7637)