001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * <a href="mailto:grenard@nuxeo.com">Guillaume</a> 016 */ 017package org.nuxeo.functionaltests.fragment; 018 019import java.util.List; 020 021import org.apache.commons.lang.StringUtils; 022import org.nuxeo.functionaltests.AbstractTest; 023import org.nuxeo.functionaltests.Locator; 024import org.nuxeo.functionaltests.forms.Select2WidgetElement; 025import org.nuxeo.functionaltests.pages.DocumentBasePage; 026import org.openqa.selenium.By; 027import org.openqa.selenium.NoSuchElementException; 028import org.openqa.selenium.StaleElementReferenceException; 029import org.openqa.selenium.WebDriver; 030import org.openqa.selenium.WebElement; 031 032import com.google.common.base.Function; 033 034/** 035 * @since 5.9.3 036 */ 037public class AddToCollectionForm extends WebFragmentImpl { 038 039 private final static String ADD_ALL_BUTTON_ID = "document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:addAll"; 040 041 private final static String ADD_BUTTON_ID = "nxw_documentActionsUpperButtons_addToCollectionAction_fancy_subview:nxw_documentActionsUpperButtons_addToCollectionAction_fancyform:add"; 042 043 private final static String S2_CHOOSE_COLLECTION_ID = "s2id_nxw_documentActionsUpperButtons_addToCollectionAction_fancy_subview:nxw_documentActionsUpperButtons_addToCollectionAction_fancyform:nxw_singleDocumentSuggestion_1_select2"; 044 045 private final static String S2_CHOOSE_COLLECTION_MULTIPLE_ID = "s2id_document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:nxw_singleDocumentSuggestion_2_select2"; 046 047 private final static String NEW_COLLECTION_DESCRIPTION_ID = "nxw_documentActionsUpperButtons_addToCollectionAction_fancy_subview:nxw_documentActionsUpperButtons_addToCollectionAction_fancyform:description"; 048 049 private final static String NEW_COLLECTION_DESCRIPTION_MULTIPLE_ID = "document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:nxw_cvButton_addSelectedToCollectionAction_fancyform_collectionDescriptionsPanel"; 050 051 private static final String EXISTING_COLLECTION_DESCRIPTION_ID = "nxw_documentActionsUpperButtons_addToCollectionAction_fancy_subview:nxw_documentActionsUpperButtons_addToCollectionAction_fancyform:scd"; 052 053 private static final String EXISTING_COLLECTION_DESCRIPTION_MULTIPLE_ID = "document_content_buttons:nxw_cvButton_addSelectedToCollectionAction_fancy_subview:nxw_cvButton_addSelectedToCollectionAction_fancyform:scd"; 054 055 private boolean multiple = false; 056 057 public AddToCollectionForm(WebDriver driver, WebElement element) { 058 super(driver, element); 059 Locator.waitUntilGivenFunctionIgnoring(new Function<WebDriver, Boolean>() { 060 @Override 061 public Boolean apply(WebDriver driver) { 062 try { 063 driver.findElement(By.id(ADD_BUTTON_ID)); 064 multiple = false; 065 return true; 066 } catch (NoSuchElementException e) { 067 driver.findElement(By.id(ADD_ALL_BUTTON_ID)); 068 multiple = true; 069 return true; 070 } 071 } 072 }, NoSuchElementException.class); 073 } 074 075 public DocumentBasePage add() { 076 return add(DocumentBasePage.class); 077 } 078 079 public <T> T add(final Class<T> pageClassProxy) { 080 Locator.findElementWaitUntilEnabledAndClick(By.id(ADD_BUTTON_ID)); 081 return AbstractTest.asPage(pageClassProxy); 082 } 083 084 public DocumentBasePage addAll() { 085 return addAll(DocumentBasePage.class); 086 } 087 088 public <T> T addAll(final Class<T> pageClassProxy) { 089 Locator.findElementWaitUntilEnabledAndClick(By.id(ADD_ALL_BUTTON_ID)); 090 return AbstractTest.asPage(pageClassProxy); 091 } 092 093 public void setCollection(final String collectionName) { 094 Select2WidgetElement s2Collection = new Select2WidgetElement(driver, Locator.findElementWithTimeout( 095 By.id(multiple ? S2_CHOOSE_COLLECTION_MULTIPLE_ID : S2_CHOOSE_COLLECTION_ID), getElement()), false); 096 s2Collection.selectValue(collectionName, false, true); 097 Locator.waitUntilGivenFunctionIgnoring(new Function<WebDriver, Boolean>() { 098 @Override 099 public Boolean apply(WebDriver driver) { 100 return StringUtils.isBlank(driver.findElement(By.id(multiple ? ADD_ALL_BUTTON_ID : ADD_BUTTON_ID)).getAttribute( 101 "disabled")); 102 } 103 }, StaleElementReferenceException.class); 104 } 105 106 public void setNewDescription(final String collectionDescription) { 107 // TODO sort this sleep out. See NXP-14030. 108 try { 109 Thread.sleep(500); 110 } catch (InterruptedException e) { 111 } 112 Locator.findElementWithTimeout( 113 By.id(multiple ? NEW_COLLECTION_DESCRIPTION_MULTIPLE_ID : NEW_COLLECTION_DESCRIPTION_ID)).sendKeys( 114 collectionDescription); 115 } 116 117 public boolean isNewDescriptionVisible() { 118 try { 119 driver.findElement(By.id(multiple ? NEW_COLLECTION_DESCRIPTION_MULTIPLE_ID : NEW_COLLECTION_DESCRIPTION_ID)); 120 return true; 121 } catch (final NoSuchElementException e) { 122 return false; 123 } 124 } 125 126 public boolean isExistingDescriptionVisible() { 127 try { 128 driver.findElement(By.id(multiple ? EXISTING_COLLECTION_DESCRIPTION_MULTIPLE_ID 129 : EXISTING_COLLECTION_DESCRIPTION_ID)); 130 return true; 131 } catch (NoSuchElementException e) { 132 return false; 133 } 134 } 135 136 public String getExistingDescription() { 137 return driver.findElement( 138 By.id(multiple ? EXISTING_COLLECTION_DESCRIPTION_MULTIPLE_ID : EXISTING_COLLECTION_DESCRIPTION_ID)).getText(); 139 } 140 141 public void removeDocumentToBeAddedToCollection(int index) { 142 if (!multiple) { 143 throw new UnsupportedOperationException("You are not adding many documents to the collection"); 144 } 145 146 List<WebElement> docsToBeAdded = getElement().findElements(By.xpath("//div[@class='simpleBox']")); 147 148 final int docsToBeAddedSize = docsToBeAdded.size(); 149 150 docsToBeAdded.get(index).findElement(By.xpath("a")).click(); 151 152 Locator.waitUntilGivenFunctionIgnoring(new Function<WebDriver, Boolean>() { 153 @Override 154 public Boolean apply(WebDriver driver) { 155 return getElement().findElements(By.xpath("//div[@class='simpleBox']")).size() == docsToBeAddedSize - 1; 156 } 157 }, StaleElementReferenceException.class); 158 159 } 160}