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    enum InputFileChoice {
041        none, keep, upload, delete, tempKeep,
042    }
043
044    /**
045     * @since 7.1
046     */
047    public String getEditChoice() {
048        for (InputFileChoice choice : InputFileChoice.values()) {
049            String subid = "choice" + choice.name();
050            if (hasSubElement(subid) && getSubElement(subid).isSelected()) {
051                return choice.name();
052            }
053        }
054        return null;
055    }
056
057    public String getFilename(boolean isEdit) {
058        WebElement link;
059        if (isEdit) {
060            link = getSubElement("default_download:download");
061        } else {
062            link = getSubElement("download");
063        }
064        return link.getText();
065    }
066
067    public void uploadTestFile(String prefix, String suffix, String content) throws IOException {
068        String fileToUploadPath = AbstractTest.getTmpFileToUploadPath(prefix, suffix, content);
069        uploadFile(fileToUploadPath);
070    }
071
072    /**
073     * Uploads file with given path.
074     *
075     * @throws IOException
076     * @since 9.1
077     */
078    public void uploadFile(String filePath) throws IOException {
079        WebElement upRadioButton = getSubElement("choiceupload");
080        Locator.waitUntilEnabledAndClick(upRadioButton);
081        WebElement fileInput = getSubElement("upload");
082        fileInput.sendKeys(filePath);
083    }
084
085    public void removeFile() {
086        if (hasSubElement("choicenone")) {
087            WebElement delRadioButton = getSubElement("choicenone");
088            delRadioButton.click();
089        } else if (hasSubElement("choicedelete")) {
090            WebElement delRadioButton = getSubElement("choicedelete");
091            delRadioButton.click();
092        } else {
093            throw new NoSuchElementException("No delete choice available on widget");
094        }
095    }
096
097}