|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.bidi.input; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.openqa.selenium.testing.drivers.Browser.EDGE; |
| 22 | +import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; |
| 23 | +import static org.openqa.selenium.testing.drivers.Browser.IE; |
| 24 | +import static org.openqa.selenium.testing.drivers.Browser.SAFARI; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.openqa.selenium.By; |
| 33 | +import org.openqa.selenium.WebElement; |
| 34 | +import org.openqa.selenium.bidi.module.Input; |
| 35 | +import org.openqa.selenium.bidi.script.RemoteReference; |
| 36 | +import org.openqa.selenium.environment.webserver.AppServer; |
| 37 | +import org.openqa.selenium.environment.webserver.NettyAppServer; |
| 38 | +import org.openqa.selenium.remote.RemoteWebElement; |
| 39 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 40 | +import org.openqa.selenium.testing.NotYetImplemented; |
| 41 | + |
| 42 | +public class SetFilesCommandTest extends JupiterTestBase { |
| 43 | + private Input input; |
| 44 | + |
| 45 | + private String windowHandle; |
| 46 | + |
| 47 | + private AppServer server; |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + public void setUp() { |
| 51 | + windowHandle = driver.getWindowHandle(); |
| 52 | + input = new Input(driver); |
| 53 | + server = new NettyAppServer(); |
| 54 | + server.start(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + @NotYetImplemented(SAFARI) |
| 59 | + @NotYetImplemented(IE) |
| 60 | + @NotYetImplemented(EDGE) |
| 61 | + @NotYetImplemented(FIREFOX) |
| 62 | + void canSetFiles() throws IOException { |
| 63 | + driver.get(pages.formPage); |
| 64 | + WebElement uploadElement = driver.findElement(By.id("upload")); |
| 65 | + assertThat(uploadElement.getAttribute("value")).isEmpty(); |
| 66 | + |
| 67 | + File file = File.createTempFile("test", "txt"); |
| 68 | + file.deleteOnExit(); |
| 69 | + |
| 70 | + List<String> paths = new ArrayList<>(); |
| 71 | + paths.add(file.getAbsolutePath()); |
| 72 | + |
| 73 | + input.setFiles( |
| 74 | + windowHandle, |
| 75 | + new RemoteReference( |
| 76 | + RemoteReference.Type.SHARED_ID, ((RemoteWebElement) uploadElement).getId()), |
| 77 | + paths); |
| 78 | + |
| 79 | + assertThat(uploadElement.getAttribute("value")).endsWith(file.getName()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @NotYetImplemented(SAFARI) |
| 84 | + @NotYetImplemented(IE) |
| 85 | + @NotYetImplemented(EDGE) |
| 86 | + @NotYetImplemented(FIREFOX) |
| 87 | + public void canSetFilesWithElementId() throws IOException { |
| 88 | + driver.get(pages.formPage); |
| 89 | + WebElement uploadElement = driver.findElement(By.id("upload")); |
| 90 | + assertThat(uploadElement.getAttribute("value")).isEmpty(); |
| 91 | + |
| 92 | + File file = File.createTempFile("test", "txt"); |
| 93 | + file.deleteOnExit(); |
| 94 | + |
| 95 | + List<String> paths = new ArrayList<>(); |
| 96 | + paths.add(file.getAbsolutePath()); |
| 97 | + |
| 98 | + input.setFiles(windowHandle, ((RemoteWebElement) uploadElement).getId(), paths); |
| 99 | + |
| 100 | + assertThat(uploadElement.getAttribute("value")).endsWith(file.getName()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @NotYetImplemented(SAFARI) |
| 105 | + @NotYetImplemented(IE) |
| 106 | + @NotYetImplemented(EDGE) |
| 107 | + @NotYetImplemented(FIREFOX) |
| 108 | + void canSetFile() throws IOException { |
| 109 | + driver.get(pages.formPage); |
| 110 | + WebElement uploadElement = driver.findElement(By.id("upload")); |
| 111 | + assertThat(uploadElement.getAttribute("value")).isEmpty(); |
| 112 | + |
| 113 | + File file = File.createTempFile("test", "txt"); |
| 114 | + file.deleteOnExit(); |
| 115 | + |
| 116 | + input.setFiles( |
| 117 | + windowHandle, |
| 118 | + new RemoteReference( |
| 119 | + RemoteReference.Type.SHARED_ID, ((RemoteWebElement) uploadElement).getId()), |
| 120 | + file.getAbsolutePath()); |
| 121 | + |
| 122 | + assertThat(uploadElement.getAttribute("value")).endsWith(file.getName()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @NotYetImplemented(SAFARI) |
| 127 | + @NotYetImplemented(IE) |
| 128 | + @NotYetImplemented(EDGE) |
| 129 | + @NotYetImplemented(FIREFOX) |
| 130 | + void canSetFileWithElementId() throws IOException { |
| 131 | + driver.get(pages.formPage); |
| 132 | + WebElement uploadElement = driver.findElement(By.id("upload")); |
| 133 | + assertThat(uploadElement.getAttribute("value")).isEmpty(); |
| 134 | + |
| 135 | + File file = File.createTempFile("test", "txt"); |
| 136 | + file.deleteOnExit(); |
| 137 | + |
| 138 | + input.setFiles( |
| 139 | + windowHandle, ((RemoteWebElement) uploadElement).getId(), file.getAbsolutePath()); |
| 140 | + |
| 141 | + assertThat(uploadElement.getAttribute("value")).endsWith(file.getName()); |
| 142 | + } |
| 143 | +} |
0 commit comments