/*
 * 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.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();
    usernameInput.clear();
    usernameInput.sendKeys(username);
    passwordInput.clear();
    passwordInput.sendKeys(password);
    loginButton.click();
    waitOnClose();
  }

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

    return true;
  }

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

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

@@ -827,16 +827,73 @@
 actory;%0A
+import org.openqa.selenium.support.ui.ExpectedCondition;%0A
 import o
@@ -1762,39 +1762,21 @@
 ();%0A
+%0A
     
-usernameInput.clear();%0A    
+rewrite(
 user
@@ -1784,26 +1784,18 @@
 ameInput
-.sendKeys(
+, 
 username
@@ -1801,39 +1801,21 @@
 e);%0A
+%0A
     
-passwordInput.clear();%0A    
+rewrite(
 pass
@@ -1827,18 +1827,10 @@
 nput
-.sendKeys(
+, 
 pass
@@ -1840,13 +1840,19 @@
 d);%0A
+%0A
     
-l
+clickL
 ogin
@@ -1861,14 +1861,8 @@
 tton
-.click
 ();%0A
@@ -2037,24 +2037,289 @@
  true;%0A  %7D%0A%0A
+  private void rewrite(WebElement field, String value) %7B%0A    field.clear();%0A    field.sendKeys(value);%0A    waitTextIsPresent(field, value);%0A  %7D%0A%0A  private void clickLoginButton() %7B%0A    webDriverWait.until(ExpectedConditions.visibilityOf(loginButton)).click();%0A  %7D%0A%0A
   private vo
@@ -2600,11 +2600,235 @@
 )));%0A  %7D
+%0A%0A  private void waitTextIsPresent(WebElement webElement, String expectedText) %7B%0A    webDriverWait.until(%0A        (ExpectedCondition%3CBoolean%3E)%0A            driver -%3E webElement.getAttribute(%22value%22).equals(expectedText));%0A  %7D
 %0A%7D%0A

Resolve problem with selenium random fails related to login page (#7341)