Skip to content

Commit d9531ed

Browse files
committed
Started implementing page.fill(...)
1 parent a3c3dc4 commit d9531ed

File tree

3 files changed

+643
-0
lines changed

3 files changed

+643
-0
lines changed

src/main/java/com/osiris/headlessbrowser/windows/PlaywrightWindow.java

+53
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ public PlaywrightWindow load(String url) throws NodeJsCodeException {
191191
return this;
192192
}
193193

194+
/**
195+
* Load html from a file.
196+
*/
197+
public PlaywrightWindow load(File file) throws NodeJsCodeException {
198+
String url = "file:///" + file.getAbsolutePath().replace("\\", "/");
199+
jsContext.executeJavaScript("" +
200+
"var response = await page.goto('"+url+"');\n");
201+
this.url = url;
202+
return this;
203+
}
204+
205+
194206
/**
195207
* Returns a copy of the currently loaded html document. <br>
196208
*/
@@ -449,6 +461,47 @@ public PlaywrightWindow click(String selector, String type, int clickCount, int
449461
return this;
450462
}
451463

464+
/**
465+
* Fills form fields. <br>
466+
* Defaults used: force=false,noWaitAfter=false,strict=false,timeout=30000. <br>
467+
* See {@link #fill(String, String, boolean, boolean, boolean, int)} for details. <br>
468+
* @param selector A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used.
469+
* @param value Value to fill for the input, textarea or [contenteditable] element.
470+
*/
471+
public PlaywrightWindow fill(String selector, String value) throws NodeJsCodeException {
472+
fill(selector, value, false, false, false, 30000);
473+
return this;
474+
}
475+
476+
/**
477+
* Fills form fields. <br>
478+
* Note that strict is set to true which means the operation will fail if the selector returns more than one element. <br>
479+
* Defaults used: force=false,noWaitAfter=false,strict=true,timeout=30000. <br>
480+
* See {@link #fill(String, String, boolean, boolean, boolean, int)} for details. <br>
481+
* @param selector A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used.
482+
* @param value Value to fill for the input, textarea or [contenteditable] element.
483+
*/
484+
public PlaywrightWindow fillStrict(String selector, String value) throws NodeJsCodeException {
485+
fill(selector, value, false, false, true, 30000);
486+
return this;
487+
}
488+
489+
/**
490+
* Fills form fields.
491+
* @param selector A selector to search for an element. If there are multiple elements satisfying the selector, the first will be used. <br><br>
492+
* @param value Value to fill for the input, textarea or [contenteditable] element. <br><br>
493+
* @param force Whether to bypass the actionability checks. <br><br>
494+
* @param noWaitAfter Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. <br><br>
495+
* @param strict When true, the call requires selector to resolve to a single element. If given selector resolves to more then one element, the call throws an exception. <br><br>
496+
* @param timeout Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the browserContext.setDefaultTimeout(timeout) or page.setDefaultTimeout(timeout) methods. <br><br>
497+
*/
498+
public PlaywrightWindow fill(String selector, String value,
499+
boolean force, boolean noWaitAfter, boolean strict, int timeout) throws NodeJsCodeException {
500+
jsContext.executeJavaScript("await page.fill('"+selector+"', '"+value+"', {force: "+force
501+
+",noWaitAfter:" +noWaitAfter+",strict:"+strict+",timeout:"+timeout+"});");
502+
return this;
503+
}
504+
452505
@Override
453506
public void close() throws Exception {
454507
// Running js: browser.close() here causes a weird exception: https://github.com/isaacs/rimraf/issues/221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.osiris.headlessbrowser.windows;
2+
3+
import com.osiris.headlessbrowser.HBrowser;
4+
import com.osiris.headlessbrowser.exceptions.NodeJsCodeException;
5+
import org.jsoup.nodes.Attribute;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.File;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class PlaywrightWindowTest {
13+
14+
/**
15+
* Perform tests on all methods.
16+
*/
17+
@Test
18+
void fullTest() throws Exception {
19+
HBrowser hBrowser = new HBrowser();
20+
try(PlaywrightWindow window = hBrowser.openCustomWindow().debugOutputStream(System.out).headless(false).buildPlaywrightWindow()){
21+
window.load(new File(System.getProperty("user.dir")+"/test.html"));
22+
formFillingTest(window);
23+
}
24+
}
25+
26+
private void formFillingTest(PlaywrightWindow window) throws NodeJsCodeException, InterruptedException {
27+
String expected = "This is the expected value!";
28+
String actual = null;
29+
window.fill("id=input__text", expected);
30+
actual = window.getDocument().getElementById("input__text").attr("value");
31+
// TODO seems like it sets the text correctly. Just got to find another way of retrieving the value from the form.
32+
Thread.sleep(60000);
33+
//assertEquals(expected, actual);
34+
}
35+
}

0 commit comments

Comments
 (0)