Skip to content

Commit

Permalink
[java] Adding a type-safe option for strictFileInteractability capabi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
barancev committed Feb 18, 2019
1 parent 7de490d commit 031cedd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
45 changes: 45 additions & 0 deletions common/src/web/upload_hidden.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<title>Upload Form</title>
<script>
var intervalId;
function onTick() {
var label = document.getElementById('upload_label');
label.innerHTML += '.';
}

function onUploadSubmit() {
document.getElementById('upload_target').contentWindow.document.body.
innerHTML = '';
var label = document.getElementById('upload_label');
label.innerHTML = 'Uploading "' + document.forms[0].upload.value + '"';
label.style.display = '';
intervalId = window.setInterval(onTick, 500);
return true;
}

function onUploadDone() {
var label = document.getElementById('upload_label');
label.style.display = 'none';
window.clearInterval(intervalId);
return true;
}
</script>
</head>
<body>
<form action="/common/upload" method="post" name="upload_form"
target="upload_target" enctype="multipart/form-data"
onsubmit="onUploadSubmit();">
<div style='height: 0; width: 0; overflow: hidden;'>
<div><input id="upload" name="upload" type="file"/></div>
</div>
<label id="visible_label" for="upload">Upload</label>
<div><input id="go" type="submit" value="Go!"/></div>
<div id="upload_label" style="display:none"></div>
<iframe src="" id="upload_target" name="upload_target"
style="width:300px;height:200px">
</iframe>
</form>
</body>
</html>
2 changes: 1 addition & 1 deletion common/src/web/upload_invisible.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<form action="/common/upload" method="post" name="upload_form"
target="upload_target" enctype="multipart/form-data"
onsubmit="onUploadSubmit();">
<div style='height: 0; width: 0; overflow: hidden;'>
<div style='height: 0; width: 0; display: none;'>
<div><input id="upload" name="upload" type="file"/></div>
</div>
<label id="visible_label" for="upload">Upload</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.openqa.selenium.remote.CapabilityType.ACCEPT_INSECURE_CERTS;
import static org.openqa.selenium.remote.CapabilityType.PAGE_LOAD_STRATEGY;
import static org.openqa.selenium.remote.CapabilityType.PROXY;
import static org.openqa.selenium.remote.CapabilityType.STRICT_FILE_INTERACTABILITY;
import static org.openqa.selenium.remote.CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR;
import static org.openqa.selenium.remote.CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR;

Expand Down Expand Up @@ -52,6 +53,11 @@ public DO setAcceptInsecureCerts(boolean acceptInsecureCerts) {
return (DO) this;
}

public DO setStrictFileInteractability(boolean strictFileInteractability) {
setCapability(STRICT_FILE_INTERACTABILITY, strictFileInteractability);
return (DO) this;
}

public DO setProxy(Proxy proxy) {
setCapability(PROXY, Objects.requireNonNull(proxy, "Proxy must not be null"));
return (DO) this;
Expand Down
37 changes: 37 additions & 0 deletions java/client/test/org/openqa/selenium/UploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.testing.Ignore;
import org.openqa.selenium.testing.JUnit4TestBase;
import org.openqa.selenium.testing.NotYetImplemented;
import org.openqa.selenium.testing.SwitchToTopAfterTest;
import org.openqa.selenium.testing.TestUtilities;
import org.openqa.selenium.testing.drivers.WebDriverBuilder;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -100,6 +103,23 @@ public void testClickFileInput() {
@Test
@Ignore(value = SAFARI, reason = "Hangs forever in sendKeys")
public void testUploadingWithHiddenFileInput() {
driver.get(appServer.whereIs("upload_hidden.html"));
driver.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());
driver.findElement(By.id("go")).click();

// Uploading files across a network may take a while, even if they're really small
WebElement label = driver.findElement(By.id("upload_label"));
wait.until(not(visibilityOf(label)));

driver.switchTo().frame("upload_target");

WebElement body = driver.findElement(By.xpath("//body"));
wait.until(elementTextToEqual(body, LOREM_IPSUM_TEXT));
}

@Test
@Ignore(value = SAFARI, reason = "Hangs forever in sendKeys")
public void testUploadingWithInvisibleFileInput() {
driver.get(appServer.whereIs("upload_invisible.html"));
driver.findElement(By.id("upload")).sendKeys(testFile.getAbsolutePath());
driver.findElement(By.id("go")).click();
Expand All @@ -112,6 +132,23 @@ public void testUploadingWithHiddenFileInput() {

WebElement body = driver.findElement(By.xpath("//body"));
wait.until(elementTextToEqual(body, LOREM_IPSUM_TEXT));
}

@Test
public void testUploadingWithInvisibleFileInputWhenStringFileInteractabilityIsOn() {
WebDriver driver2 = new WebDriverBuilder().get(
new ImmutableCapabilities(CapabilityType.STRICT_FILE_INTERACTABILITY, true));
try {
System.out.println(((RemoteWebDriver) driver2).getCapabilities());
driver2.get(appServer.whereIs("upload_invisible.html"));
WebElement input = driver2.findElement(By.id("upload"));
System.out.println(input.isDisplayed());

assertThatExceptionOfType(ElementNotInteractableException.class).isThrownBy(
() -> input.sendKeys(testFile.getAbsolutePath()));
} finally {
driver2.quit();
}

}

Expand Down

0 comments on commit 031cedd

Please sign in to comment.