using hidden applet in zk - java

My task is using hidden signed applet which support cryptography functions. Applet has to be loaded dynamically.
I try to use this example: (no links, just open first google search result from Oracle website) "invoking Applet Methods From JavaScript". The problem is when the applet is loaded and deployed with "deployJava.js" ZK window is disappearing.
My code is:
function loadScript(url, callback)
{
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
callback();
}
function startApplet() {
var invokeApplet = function () {
var attributes = { id:'cryptoApplet', code:'CryptoApplet', width:1, height:1} ;
var parameters = { jar: 'clientcrypto.jar'} ;
deployJava.runApplet(attributes, parameters, '1.7');
};
loadScript("/js/deployJava.js", invokeApplet);
}

This problem happens because deployJava.js use "document.write(applet tag)" to add an applet.
I add div component to the page which has width="1", height="1", and rewrite deployJava.js to append applet in that div. And it's worked.

Related

DJ Native Web browser not opening GoogleMap html with js while Chrome does

I'm making an app with GoogleMap inside DJ Native webBrowser component. I load page as a string using webBrowser.setHTMLContent(String). HTML file contains JavaScript which add markers to map.
I made simple html file with google-maps-api functions.
It works perfect on Chrome as well as Firefox. But not in webBrowser (djnative).
I discovered that script without new marker statement(google.maps.Marker) works OK.
Have anyone got any idea what's wrong?
Is there any way to show console log from webBrowser (like ctrl+shift+J in Chrome)
This is script code:
<script type="text/javascript" src=https://maps.googleapis.com/maps/api/js?key=[MY_KEY]&sensor=false">
</script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(52.236302, 21.007636),
zoom: 10
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
var t = [];
var x = [];
var y = [];
var h = [];
t.push('Location Name 1');
x.push(52.232097);
y.push(20.927985);
h.push('<p><strong>Location Name 1</strong><br/>Address 1</p>');
t.push('Location Name 2');
x.push(52.245097);
y.push(20.945985);
h.push('<p><strong>Location Name 2</strong><br/>Address 2</p>');
/*this is error making code*/
var i = 0;
for ( item in t ) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(x[i], y[i]),
map: map,
title: t[i],
});
i++;
} /*this is end of error making code*/
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
1.Dj is using ie as default. did you try opening the html with ie?
2.In dj, you can not always setting the content and expect it run. for example, the tinymce editor, does not run if you set the editor.html (html containint tinymce) directly. That is why the author of dj made internal webserver for editors. You have to call it through an address (for editor ck and tinymce, dj calls localhost, http://127.0.0.1/tinymce/.. but the structure is too complex to be detailed here. you may try for testing purpose, putting your html to a simple web page (tomcat) and call it through loadURL (instead of setContent)

how to record testcase using selenium in IE

I am new to selenium.
My application is only IE compatible.
I know that we can run test cases in any any browsers using respective drivers but is there any way that we can use to record test case using selenium IDE in Internet Explorer ??
We have implemented our own Recorder which will be used only for recording in Internet Explorer. It is a javascript file.
The concept is to add listeners to each object in the source code of the GUI of web page.
Below code helps you to do that. As soon as the page is loaded these listeners will be added. When you perform click action, all its properties will be captured.
Here i have given an example of adding listener and getting properties of the object of type "SELECT", you can do the same thing for other types of objects.
var added_MClistener = false;
var tagn = dObj.tagName;
if(tagn == "SELECT")
{
dObj.attachEvent("onchange",so_showObjInfo);
dObj.added_OClistener = true;
dObj.so_prevBGColor = alll[i].style.backgroundColor;
}
if(tagn != "OPTION" && tagn != "SELECT" )
{
dObj.added_MClistener = true;
dObj.attachEvent("onclick",so_showObjInfo);
dObj.so_prevBGColor = alll[i].style.backgroundColor;
}
function so_showObjInfo(e) {
if(pause)return;
if(isActive)return;
var preE = e;
var e =e? e:window.event;
var ele=e.target?e.target:e.srcElement;
activeObj = ele;
var eltagn= activeObj.tagName;
var currentNode=activeObj;
var path=[];
while(currentNode){var pe=getNode(currentNode);if(pe){path.push(pe);if(pe.indexOf('#id')!=-1)break;}currentNode=currentNode.parentNode;}var xpath="//"+path.reverse().join('/');
var fff=0;
var xpath;
while(currentNode){var pe=getNode(currentNode);if(pe){path.push(pe);if(pe.indexOf('#id')!=-1){fff=1; break;}if(pe.indexOf('#name')!=-1){fff=1; break;}}currentNode=currentNode.parentNode;}if(fff==1){xpath="//"+path.reverse().join('/');}
var acurrentNode=activeObj;
var apath=[];
while(acurrentNode){var ape=agetNode(acurrentNode);if(ape){apath.push(ape);}acurrentNode=acurrentNode.parentNode;} var axpath="//"+apath.reverse().join('/');
var el=activeObj;
var cssPath = cssselect(el);
if (cssPath!=null)
{
cssPath="css="+cssPath;
}
var objval=activeObj.value;
var objname=activeObj.name;
var objidd=activeObj.id;
}
In this way we can add listeners to the objects on the webpage and get their properties. Now its up to you what to do next. You can either write it in an excel like we did(in a particular format) or you can create a notepad file.
Hope it helps....
Selenium IDE is only available on Firefox. There is noway to record your test on IE using Selenium IDE.
Recorder is for Firefox. Record with Firefox replay on ie. Tweak script as needed

Calling Java class from JSP button onclick

I am trying to to call a Java class when a button gets clicked from JSP. inside my JSP file I have the following:
<%
Object name = session.getAttribute("name");
Object ext = session.getAttribute("ext");
DBOps ops = new DBOps();
ReturnGetDisplayInfo GDI = ops.getDisplayInfo(ext);
%>
I have a method in DBOps that will delete a certain field so I added a button to teh table that displays the information and now I am trying to call the delete method when the button is clicked. so I tried doing the following but it did not work.
<td><button onclick=<% ops.delete(ext); %>>Delete</button></td>
I was looking at some examples that utilize javascript but it uses defiend functions in teh script rather than calling the Java class.
Thanks in advance
You can't do that directly. You need a roundtrip to the server.
The best option for that is AJAX:
make a servlet that handles performs the delete request when a certain url is invoked
use jQuery (or native XmlHttpRequest) to invoke that url
(DWR - direct web remoting is also an option, which is implemented using AJAX)
One example of my code, in javascript and ajax, if it can help you:
On my jsp i have a onClick"changeTimeZone(org)"
Javascript:
function changeTimeZone(org)
{
//Prepare a new ajaxRequest.
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
//state 4 is response ready.
//Status 200 is page found.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//fill the timezone field with the reponse of my servlet.
document.getElementById('timeZoneText').value = xmlhttp.responseText;
}
};
//send an ajax request.
//Go to my servlet
xmlhttp.open('GET','mainServlet?command=ajax.ChangeTimeZone&Org=' + org.value, true);
xmlhttp.send();
}

How do I click a javascript button with htmlunit?

I'm working on an application that will automatically click a button on a webpage using htmlunit in Java. Only problem is that that button is a javascript button, so the standard getInputByName() won't work. Any suggestions with dealing with this? The code for the button is included below.
<a class="vote_1" id="1537385" href="/javascript%3Avoid%280%29/index"><img src="/images/parts/btn-vote.gif" alt="Btn-vote" /></a>
In addition, here's the other code for voting.
<div id="content"><script type="text/javascript" src="/js/scriptFeeds/voteArticle.js"></script>
Which leads to the following javascript:
var pressed = new Array();
$j(document).ready(function() {
var nr = $j("input#number_of_articles").val();
for(var i=1; i<=nr; i++){
$j("a.vote_"+i).click(function(){
var article = $j(this).attr("id");
$j('#'+article).hide();
if (!pressed[article]) {
pressed[article] = "yes";
jQuery.post('/vote-article', {
_token: $j("#_token").val(),
article_id: article
},function(data) {
$j("span.numberOfVotes_"+data.id).html(data.votes);
}, "json");
}
return false;
});
}
});
Try using this addOn for firefox, it records your actions and generates the HTMLUnit code for the same. may be it could help.
http://code.google.com/p/htmlunitscripter/
There's nothing special about clickable images. Something like this should work:
button = page.getHtmlElementById( "1537385" ) ;
page = button.click() ;
HtmlUnit will then run the Javascript and return the updated page.
If the id attribute of the 'a' tag isn't constant, you may need to use XPath to grab it.
I have a very similar link on one of my pages. If you can call .click() on any HtmlElement, it should be able to run associated Javascript. Here is my code (generated from HtmlUnitScripter):
HtmlElement element4 = null;
Iterable<HtmlElement> iterable5 = page.getAllHtmlChildElements();
Iterator<HtmlElement> i6 = iterable5.iterator();
while(i6.hasNext())
{
HtmlElement anElement = i6.next();
if(anElement instanceof HtmlImage)
{
HtmlImage input = (HtmlImage) anElement;
String[] elements = "http://example.com/pages/powerbutton.png".split( "/" );
if(input.getSrcAttribute().indexOf(elements[elements.length-1] )> -1 )
{
element4 = input;
break;
}
}
}
HtmlPage page = element4.click();

java equivalent of swfobject

looking for a javascript class like swfobject to embed java and have a simple fallback if the user doesn't have java or refuses the security prompt.
thanks,
Josh
You could build one pretty easily.
Have something like a div set up like this:
<div id="java-applet">
Message to user saying that they need Java here
</div>
Then add Java Plugin Detection (builder) to your JavaScript. Then if that returns true, then do something like:
document.getElementById("java-applet").innerHTML = "<applet>stuff here</applet>";
appletobject may work, but I have not used it.
Just embed the applet like you normally do and insert the fallback inside or insert a javascript snippet to remove the object: Besides param, you can add other elements, e.g. paragraphs with text or javascript calling some function to replace the object.
<script type="text/javascript">
function replace_object(x) {
$(x)...
}
</script>
<object x="y" id="some_applet">
<param name="y" value="z">
<p>java not available. some alternative here. <!-- option 1 --></p>
<script type="text/javascript">
replace_object('some_applet'); // option 2
</script>
</object>
This helps!
I got a very strange problem while using applet to do batch file downloading from the server side.
The Ajax request seems conflict with applet request, the applet file downloading interrupted with some socket exception.
The applet works fine under JRE5.0, it might be caused by our recent upgrade to JRE6.0.
<div id="java-applet"></div>
<script>
var t;
function startApplet() {
var attributes = {codebase:'<%=request.getContextPath()%>',
code:'<%=appletClass%>',
archive:'applet/SignedApplet.jar',
width:0,
height:0} ;
var parameters = {para1:'value1',
para2:'value2',
java_arguments:'-Xms64m -Xmx512m'
} ;
var version = '1.6' ;
var buildAppletTag = function() {
var tag = '<applet';
for (var attribute in attributes){
tag += (' ' + attribute + '="' + attributes[attribute] + '"');
}
tag += ">";
for (var parameter in parameters){
tag += '<param name="' + parameter + '" value="' + parameters[parameter] + '"/>';
}
tag += '</applet>';
return tag;
};
document.getElementById("java-applet").innerHTML = buildAppletTag(attributes, parameters, version);
clearTimeout(t);
}
t = setTimeout("startApplet()", 1000); // delayed
</script>

Categories

Resources