question

ezy avatar image
ezy asked ezy posted

How to code javascript into iTest to get the browser name and version.

How to insert javascript into iTest to get the web browser name and version when I use the external browser ?

Example Javascript
<script type="text/javascript">
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
document.write("Browser name: "+ browser);
document.write("<br />");
document.write("Browser version: "+ version);
</script>

Example Output:
Browser name: Netscape
Browser version: 5
iTestGUI Testing(Web-Java Swing-etc)
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
PreetS avatar image
PreetS answered PreetS posted

iTest only supports the built in internal browser (mozilla) and Internet Explorer 7. So, I am not sure why you would want to check the browser type.

 

Anyway, to run any JavaScript in the web browser use the web application's 'eval' action.

The command body will take in the JavaScript you wish to run. It is recommended that you put each statement in a separate line;

I notice that your JavaScript is actually displaying the browser name by overwriting the content of the page.

You could use JavaScript alert to show you the value:

alert(browser);

 

You can get all the alert messages in iTest by running the "listPrompts" action. 

 

So your test case steps would look like this:

 

eval   var browser = navigator.appName;

eval   var version =  navigator.appVersion;

eval   alert(browser);

eval   alert(version);

listPrompts

 

 

The response of listPrompts will have the values you are looking for.

1 comment
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

PreetS avatar image PreetS commented ·

Another interesting way of calling javascript which might span multiple lines.
You can wrap all your steps in a function.

Set the function in the web page's context using  'eval' action

The call the function using another 'eval' action

 

e.g. if you have a javascript function is:


var browser = navigator.appName;
var result;
if (browser == "Microsoft Internet Explorer") {
result = 10;
} else {
result = 20;
}
alert(result);

 

You could wrap this into an eval action:

 

eval  var myFunction = new Function("var browser = navigator.appName; var result; if (browser == 'Microsoft Internet Explorer') { result = 10; } else { result = 20; } alert(result);");

eval  myFunction();

 


This is also useful if you wish to call the function multiple times on the page.
Message Edited by PreetS on 04-17-2009 12:01 PM
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.