Skip to content

Commit

Permalink
#59 Sample code snippet to use DOMSubtreeModified event
Browse files Browse the repository at this point in the history
  • Loading branch information
ui4j committed Aug 8, 2015
1 parent f3efe1d commit e8783ed
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ui4j-sample/src/main/java/com/ui4j/sample/FormPost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ui4j.sample;

import java.util.concurrent.CountDownLatch;

import com.ui4j.api.browser.BrowserFactory;
import com.ui4j.api.browser.Page;
import com.ui4j.api.dom.Element;

public class FormPost {

private static String result;

public static void main(String[] args) throws Exception {
String url = FrameSample.class.getResource("/FormPost.html").toExternalForm();
Page page = BrowserFactory.getWebKit().navigate(url);

Element custname = page.getDocument().query("[name='custname']").get();
custname.setValue("foo");

page.getDocument().query("[type='submit']").get().click();

CountDownLatch latch = new CountDownLatch(1);

page.getDocument().query("label").get().bind("DOMSubtreeModified", e -> {
result = e.getTarget().getText().get();
latch.countDown();
});

latch.await();

System.out.println("Form post result: " + result);

page.close();

BrowserFactory.getWebKit().shutdown();
}
}
23 changes: 23 additions & 0 deletions ui4j-sample/src/main/resources/FormPost.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript">
$(function() {
$('[type="submit"]').click(function() {
var name = $('[name="custname"]').val();
$.ajax({
type: 'GET',
url: 'http://httpbin.org/get?name=' + name
}).done(function(data) {
$('label').text(data.args.name);
});
});
});
</script>
<body>
<input name="custname">
<input type="submit">
<br>
<label></label>
</body>
</html>

0 comments on commit e8783ed

Please sign in to comment.