From 2d0ddc14ec7d1db7727e09c9c5940f360885670f Mon Sep 17 00:00:00 2001 From: Jj! Date: Fri, 1 Oct 2021 13:43:48 -0500 Subject: [PATCH] needed to fix windows file path case --- .../selenium/selenese/command/AttachFile.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 backend/src/integration/groovy/jp/vmi/selenium/selenese/command/AttachFile.java diff --git a/backend/src/integration/groovy/jp/vmi/selenium/selenese/command/AttachFile.java b/backend/src/integration/groovy/jp/vmi/selenium/selenese/command/AttachFile.java new file mode 100644 index 000000000..02bf54d65 --- /dev/null +++ b/backend/src/integration/groovy/jp/vmi/selenium/selenese/command/AttachFile.java @@ -0,0 +1,112 @@ +// This code is based on: +// +// com.thoughtworks.selenium.webdriven.commands.AttachFile +// +// in Selenium WebDriver. +// +// The following copyright is copied from original. +// --- +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package jp.vmi.selenium.selenese.command; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; + +import org.apache.commons.io.FilenameUtils; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.io.TemporaryFilesystem; + +import com.google.common.io.Resources; + +import jp.vmi.selenium.selenese.Context; +import jp.vmi.selenium.selenese.result.Error; +import jp.vmi.selenium.selenese.result.Result; +import jp.vmi.selenium.selenese.result.Warning; + +import static jp.vmi.selenium.selenese.command.ArgumentType.*; +import static jp.vmi.selenium.selenese.result.Success.*; + +/** + * Re-implementation of AttachFile. + */ +public class AttachFile extends AbstractCommand { + + private static final int ARG_LOCATOR = 0; + private static final int ARG_FILENAME = 1; + + AttachFile(int index, String name, String... args) { + super(index, name, args, LOCATOR, VALUE); + } + + @Override + protected Result executeImpl(Context context, String... curArgs) { + String name = curArgs[ARG_FILENAME]; + File outputTo = null; + if (name.contains("://")) { + // process (remote) url + URL url; + try { + url = new URL(name); + } catch (MalformedURLException e) { + return new Error("Malformed URL: " + name); + } + File dir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("attachFile", "dir"); + outputTo = new File(dir, new File(url.getFile()).getName()); + FileOutputStream fos = null; + try { + fos = new FileOutputStream(outputTo); + Resources.copy(url, fos); + } catch (IOException e) { + return new Error("Can't access file to upload: " + url, e); + } finally { + try { + if (fos != null) { + fos.close(); + } + } catch (IOException e) { + return new Warning("Unable to close stream used for reading file: " + name, e); + } + } + } else { + // process file besides testcase file + // okay, let's fix some cases... + if (name.matches("([a-zA-Z]:)?\\\\.+")) { + // windows absolute path + outputTo = new File(name); + } else { + outputTo = new File(FilenameUtils.getPath(context.getCurrentTestCase().getFilename()), name); + } + if (!outputTo.exists()) { + return new Error("Can't access file: " + outputTo); + } + } + + WebElement element = context.findElement(curArgs[ARG_LOCATOR]); + try { + element.clear(); + } catch (Exception e) { + // ignore exceptions from some drivers when file-input cannot be cleared; + } + element.sendKeys(outputTo.getAbsolutePath()); + return SUCCESS; + } +}