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