开发者

Java Script alert handling using java

I like to know how to handle Java script alerts pragmatically using Java. Am mean to say how to accept JavaScript alerts via Java code when clicking a particular link ...If po开发者_运维技巧ssible let me know soon...

Thanks ..

Vijay


@Test
public void alerts() throws Exception {

    final WebClient webClient = new WebClient();

    final List collectedAlerts = new ArrayList();
    webClient.setAlertHandler(new CollectingAlertHandler(collectedAlerts));

    // Since we aren't actually manipulating the page, we don't assign
    // it to a variable - it's enough to know that it loaded.
    webClient.getPage("http://tciludev01/test.html");

    final List expectedAlerts = Collections.singletonList("foo");
    assertEquals(expectedAlerts, collectedAlerts);
}

refferer http://htmlunit.sourceforge.net/javascript-howto.html


If you want to avoid leaving the page, for example, a user types in some text that is in error or needs validation and you want to do that work on the server, then you should look into AJAX (asynchronous javascript) which will allow you to send requests to the server behind the scenes. There are many good books.

If you just want to submit a form to the server and re-load the page on some javascript generated event (aka an alert), then you should just look into making a window.location('http://yourdomain.com&addSome=variables&toThe=urlString') call from within javascript. Alternatively you can submit a hidden form using javascript as well (a better idea if you have many variables).

Remember, javascript is code that happens in the browser. It has no relation to the server. Anything that happens on the server has to happen in response to a request coming from the browser. Whether that's a new page being requested, or a behind the scenes AJAX request is up to you (in either of those cases there is an HTTP request generated from the browser to the server).


I'm not sure whether it is possible to do this with Java in a cross-platform way.

In testing automation we used AutoHotkey tool for Windows, and it worked well. There are links to similar projects for Linux on AutoHotkey Wikipedia page.

Here is some info about interaction between AutoHotkey and Java program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜