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        WebElement upRadioButton = getSubElement("choiceupload");
070        Locator.waitUntilEnabledAndClick(upRadioButton);
071        WebElement fileInput = getSubElement("upload");
072        fileInput.sendKeys(fileToUploadPath);
073    }
074
075    public void removeFile() {
076        if (hasSubElement("choicenone")) {
077            WebElement delRadioButton = getSubElement("choicenone");
078            delRadioButton.click();
079        } else if (hasSubElement("choicedelete")) {
080            WebElement delRadioButton = getSubElement("choicedelete");
081            delRadioButton.click();
082        } else {
083            throw new NoSuchElementException("No delete choice available on widget");
084        }
085    }
086
087}