18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
is there import in sahi like java
Hi All,
we can use java code in sahi script
examlpe
if we want to use java code in sahi script then we have to write like below
var robot = new java.awt.Robot();
var toolkit = new java.awt.Toolkit.getDefaultToolkit();
where ever we are using java code there we have use like "java.util.*"
instead of writing every time like "java.awt"
is there any way to write once and use many times like "import" in java
thanks in advance
we can use java code in sahi script
examlpe
if we want to use java code in sahi script then we have to write like below
var robot = new java.awt.Robot();
var toolkit = new java.awt.Toolkit.getDefaultToolkit();
where ever we are using java code there we have use like "java.util.*"
instead of writing every time like "java.awt"
is there any way to write once and use many times like "import" in java
thanks in advance
Comments
You could try to include the above two lines in an .sah file and use _include API to use it in other scripts.
Regards
Theeran.
thanks for your reply
could u pls tell me one example, im unable to understand
thanks in advance
Here is the example:
Save is in a .sah file:
You can now call this in another .sah file using _include() API as:
For more details about _include() API:
http://sahi.co.in/w/sahi-scripting-basics
Let me know if you face any problem.
Regards
Sumitra
thanks for your reply
i want here like import statement in java
for example
if i want to read a file using java then i can write like below
import java.io.File;
File f = new File("temp.txt")
if i wont use import statement then i have to write like below
File f = new Java.io.File(temp.txt");
instead of writing every time like java.io.File()
i want import statement as we use in java
the example above which u have given is
var $a=Packages.java.lang.System.out;
again here i wanna use "$a" to print like
$a.println("hai");
i don't want to assign to any variable i wanna use like java import statements
declare once and use where ever i want without using " java.io.File() "
please let me know if there is any option like above
can u plsss reply to the below query ?
is it possible in sahi like below
import java.io.*;
if possible any alternate method without assigning to a variable like below what you have shown
var $a=Packages.java.lang.System.out; You can now call this in another .sah file using _include() API as: _include("C:\Work\Sahi-PRO\userdata\scripts\javaClass.sah"); $a.println("hai");for example
if i wanna capture a screen using java code in sahi then i can write like below
var robot = new java.awt.Robot(); var toolkit = new java.awt.Toolkit.getDefaultToolkit(); var screenSize = toolkit.getScreenSize(); var screenRect = new java.awt.Rectangle(0, 0, screenSize.width, screenSize.height); var image = robot.createScreenCapture(screenRect); var file = new java.io.File($folder_Name +"/"+ $filename+".png"); javax.imageio.ImageIO.write(image, "png", file);instead of above pattern, is it possible to write like java pattern
sahi file
import javax.imageio.*; import java.io.*; import java.awt.*; var robot = new Robot(); var toolkit = new Toolkit.getDefaultToolkit(); var screenSize = toolkit.getScreenSize(); var screenRect = new Rectangle(0, 0, screenSize.width, screenSize.height); var image = robot.createScreenCapture(screenRect); var file = new File($folder_Name +"/"+ $filename+".png"); ImageIO.write(image, "png", file);the example above which u have given is
var $a=Packages.java.lang.System.out;
again here i wanna prefix "$a" to print, like
$a.println("hai");
i don't wanna assign to any variable i wanna use like java import statements
declare once and use where ever i want without prefix java.io for every function
please let me know if there is any option like what im asking
if that is not possible then we can go with the above pattern, which u have mentioned, but pls reply
You can do this:
var System=Packages.java.lang.System;
System.out.println("Hi there");
Similarly
var Robot = java.awt.Robot;
var robot = new Robot();
Regards,
Narayan
if i want to use java.awt component again, i should declare it again as per your example
Despite i have used java.awt for Robot(), if i want to use Toolkit, then again it should declare like below isn't ?
[code]
var Robot = java.awt.Robot;
var robot = new Robot();
var toolkit = new java.awt.Toolkit.getDefaultToolkit();
// here again we have to prefix with java.awt.Toolkit instead of directly call Toolkit.getDefaultToolkit() right ?
[/code]
but im asking like below code
[code]
var Robot = java.awt.Robot;
var robot = new Robot();
var toolkit = new Toolkit.getDefaultToolkit();
// but here i don't wanna prefix again once i have imported package java.awt
// looking like same java code
[/code]
Once i have import/ declared java.awt, the upcoming related functions/ components shouldn't prefix with java.awt
But in your example even i have used java.awt for Robot(), if i want to use Toolkit again i have to prefix with java.awt for Toolkit like below
[code]
var toolkit = new java.awt.Toolkit.getDefaultToolkit();
[/code]
But my requirement is once i have imported any package i dont't wanna prefix for upcoming fun/ components
looking for your reply narayan/ any user, pls let me know is it possible by using sahi ?
thanks in advance
is it possible in Sahi like my before post ?
plsss let me know is possible or not in sahi ?
thanks in advance
Any solutions pls ?
it seems like your request is not ranked pretty high in the priority order. You may improve this by explaining why the heck you can't use the workarounds like above? Just create your default _include file and fill it with every class declaration you need. That's just 2 lines for every additional class you have to write and just a single _include("allTheClassesINeed.sah") for every script you want to use them in.
Alternatively write your test scripts in java.. problem solved.
regards
wormi
thanks for quick reply, sorry im not able to get what exactly are you saying, if possible could u pls provide me 1 example regarding "single _include("allTheClassesINeed.sah")" file.
Why I'm not preferring java, in sahi script we can write multiple scripts at a time like
1. Sahi Script
2. Javascript
3. Java
i don't know whether is it possible in java or not like writing multiple scripting languages, that's why I'm preferred to write test scripts in java
If possible pls provide me some example regarding "_include()"
thanks in advance