/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2017 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.qa.galenium.util;

import static io.wcm.qa.galenium.reporting.GaleniumReportUtil.getLogger;
import static io.wcm.qa.galenium.util.GaleniumConfiguration.getAuthorPass;
import static io.wcm.qa.galenium.util.GaleniumConfiguration.getAuthorUser;
import static io.wcm.qa.galenium.util.InteractionUtil.click;
import static io.wcm.qa.galenium.util.InteractionUtil.enterText;
import static io.wcm.qa.galenium.util.InteractionUtil.loadUrl;
import static io.wcm.qa.galenium.util.InteractionUtil.waitForUrl;

import io.wcm.qa.galenium.selectors.Selector;
import io.wcm.qa.galenium.selectors.SelectorFactory;

/**
 * AEM specific utility methods.
 */
public final class AemUtil {

  private static final Selector DIV_LOGIN_BOX = SelectorFactory.fromCss("div#login-box");
  private static final Selector SELECTOR_AUTHOR_INPUT_PASSWORD = SelectorFactory.fromCss("#password");
  private static final Selector SELECTOR_AUTHOR_INPUT_USERNAME = SelectorFactory.fromCss("#username");
  private static final Selector SELECTOR_AUTHOR_LOGIN_BUTTON = SelectorFactory.fromCss("#submit-button");

  private AemUtil() {
    // do not instantiate
  }

  /**
   * @return whether current page is AEM author login page
   */
  public static boolean isAuthorLogin() {
    return InteractionUtil.getElementVisible(DIV_LOGIN_BOX) != null;
  }

  /**
   * Login to author if on AEM author login page.
   */
  public static void loginToAuthor() {
    if (isAuthorLogin()) {
      getLogger().info("Logging in to author instance");
      enterText(SELECTOR_AUTHOR_INPUT_USERNAME, getAuthorUser());
      enterText(SELECTOR_AUTHOR_INPUT_PASSWORD, getAuthorPass());
      click(SELECTOR_AUTHOR_LOGIN_BUTTON);
    }
  }

  /**
   * Load URL and login to AEM author if landing on login page.
   */
  public static void loginToAuthor(String targetUrl) {
    loadUrl(targetUrl);
    if (isAuthorLogin()) {
      loginToAuthor();
      waitForUrl(targetUrl);
    }
  }

}

@@ -2057,268 +2057,256 @@
     
-if (isAuthorLogin()) %7B%0A      getLogger().info(%22Logging in to author instance%22);%0A      enterText(SELECTOR_AUTHOR_INPUT_USERNAME, getAuthorUser());%0A      enterText(SELECTOR_AUTHOR_INPUT_PASSWORD, getAuthorPass());%0A      click(SELECTOR_AUTHOR_LOGIN_BUTTON);%0A    %7D
+loginToAuthor(getAuthorUser(), getAuthorPass());%0A  %7D%0A%0A  /**%0A   * Load URL and login to AEM author if landing on login page.%0A   */%0A  public static void loginToAuthor(String targetUrl) %7B%0A    loginToAuthor(targetUrl, getAuthorUser(), getAuthorPass());
 %0A  %7D
@@ -2343,32 +2343,49 @@
 n to AEM author 
+with credentials 
 if landing on lo
@@ -2386,32 +2386,100 @@
  on login page.%0A
+   * @param targetUrl%0A   * @param authorUser%0A   * @param authorPass%0A
    */%0A  public s
@@ -2511,32 +2511,70 @@
 String targetUrl
+, String authorUser, String authorPass
 ) %7B%0A    loadUrl(
@@ -2632,16 +2632,38 @@
 oAuthor(
+authorUser, authorPass
 );%0A     
@@ -2697,10 +2697,346 @@
  %7D%0A  %7D%0A%0A
+  private static void loginToAuthor(String authorUser, String authorPass) %7B%0A    if (isAuthorLogin()) %7B%0A      getLogger().info(%22Logging in to author instance%22);%0A      enterText(SELECTOR_AUTHOR_INPUT_USERNAME, authorUser);%0A      enterText(SELECTOR_AUTHOR_INPUT_PASSWORD, authorPass);%0A      click(SELECTOR_AUTHOR_LOGIN_BUTTON);%0A    %7D%0A  %7D%0A%0A
 %7D%0A

add credentials to author login