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.openqa.selenium.NoSuchElementException;
025import org.openqa.selenium.WebDriver;
026import org.openqa.selenium.WebElement;
027
028/**
029 * Represents a file widget, with helper methods to retrieve/check its own elements.
030 *
031 * @since 5.7
032 */
033public class FileWidgetElement extends AbstractWidgetElement {
034
035    public FileWidgetElement(WebDriver driver, String id) {
036        super(driver, id);
037    }
038
039    enum InputFileChoice {
040        none, keep, upload, delete, tempKeep,
041    }
042
043    /**
044     * @since 7.1
045     */
046    public String getEditChoice() {
047        for (InputFileChoice choice : InputFileChoice.values()) {
048            String subid = "choice" + choice.name();
049            if (hasSubElement(subid) && getSubElement(subid).isSelected()) {
050                return choice.name();
051            }
052        }
053        return null;
054    }
055
056    public String getFilename(boolean isEdit) {
057        WebElement link;
058        if (isEdit) {
059            link = getSubElement("default_download:download");
060        } else {
061            link = getSubElement("download");
062        }
063        return link.getText();
064    }
065
066    public void uploadTestFile(String prefix, String suffix, String content) throws IOException {
067        String fileToUploadPath = AbstractTest.getTmpFileToUploadPath(prefix, suffix, content);
068        WebElement upRadioButton = getSubElement("choiceupload");
069        upRadioButton.click();
070        WebElement fileInput = getSubElement("upload");
071        fileInput.sendKeys(fileToUploadPath);
072    }
073
074    public void removeFile() {
075        if (hasSubElement("choicenone")) {
076            WebElement delRadioButton = getSubElement("choicenone");
077            delRadioButton.click();
078        } else if (hasSubElement("choicedelete")) {
079            WebElement delRadioButton = getSubElement("choicedelete");
080            delRadioButton.click();
081        } else {
082            throw new NoSuchElementException("No delete choice available on widget");
083        }
084    }
085
086}