001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.functionaltests.forms;
020
021import java.io.IOException;
022
023import org.nuxeo.functionaltests.AbstractTest;
024import org.nuxeo.functionaltests.Locator;
025import org.openqa.selenium.NoSuchElementException;
026import org.openqa.selenium.WebDriver;
027import org.openqa.selenium.WebElement;
028
029/**
030 * Represents a file widget, with helper methods to retrieve/check its own elements.
031 *
032 * @since 5.7
033 */
034public class FileWidgetElement extends AbstractWidgetElement {
035
036    public FileWidgetElement(WebDriver driver, String id) {
037        super(driver, id);
038    }
039
040    public enum InputFileChoice {
041        none, keep, upload, delete, tempKeep;
042
043        public String getOptionId() {
044            return "choice" + name();
045        }
046    }
047
048    /**
049     * @since 7.1
050     */
051    public String getEditChoice() {
052        for (InputFileChoice choice : InputFileChoice.values()) {
053            String subid = choice.getOptionId();
054            if (hasSubElement(subid) && getSubElement(subid).isSelected()) {
055                return choice.name();
056            }
057        }
058        return null;
059    }
060
061    public String getFilename(boolean isEdit) {
062        WebElement link;
063        if (isEdit) {
064            link = getSubElement("default_download:download");
065        } else {
066            link = getSubElement("download");
067        }
068        return link.getText();
069    }
070
071    public void uploadTestFile(String prefix, String suffix, String content) throws IOException {
072        String fileToUploadPath = AbstractTest.getTmpFileToUploadPath(prefix, suffix, content);
073        uploadFile(fileToUploadPath);
074    }
075
076    /**
077     * Uploads file with given path.
078     *
079     * @throws IOException
080     * @since 9.1
081     */
082    public void uploadFile(String filePath) throws IOException {
083        selectChoice(InputFileChoice.upload);
084        WebElement fileInput = getSubElement("upload");
085        fileInput.sendKeys(filePath);
086    }
087
088    /**
089     * Selects available option that should result in no file being present on the document.
090     */
091    public void removeFile() {
092        if (hasChoice(InputFileChoice.none)) {
093            selectChoice(InputFileChoice.none);
094        } else if (hasChoice(InputFileChoice.delete)) {
095            selectChoice(InputFileChoice.delete);
096        } else {
097            throw new NoSuchElementException("No delete choice available on widget");
098        }
099    }
100
101    /**
102     * Selects available option that should result in keeping selected file.
103     *
104     * @since 10.1
105     */
106    public void keepFile() {
107        if (hasChoice(InputFileChoice.keep)) {
108            selectChoice(InputFileChoice.keep);
109        } else if (hasChoice(InputFileChoice.tempKeep)) {
110            selectChoice(InputFileChoice.tempKeep);
111        } else {
112            throw new NoSuchElementException("No keep choice available on widget");
113        }
114    }
115
116    /**
117     * Returns true if given option is presented on file widget radio options.
118     *
119     * @since 10.1
120     */
121    public boolean hasChoice(InputFileChoice choice) {
122        return hasSubElement(choice.getOptionId());
123    }
124
125    /**
126     * Select given choice on file widget radio options.
127     *
128     * @since 10.1
129     * @throws NoSuchElementException if option is not available.
130     */
131    public void selectChoice(InputFileChoice choice) {
132        String id = choice.getOptionId();
133        if (hasSubElement(id)) {
134            Locator.waitUntilEnabledAndClick(getSubElement(id));
135        } else {
136            throw new NoSuchElementException("No '" + choice.name() + "' choice available on widget");
137        }
138    }
139
140}