How to write java program in Sikuli? - java

I have recorded the GUI desktop application using SIKULI as below:
App.open ("C:\\Program Files\\acd\\bin\\VPNClient.exe")
sleep(1)
type ("mganda1")
sleep(1)
click( ) //click OK
I want to convert this script into Java. So I am trying as below:
package com.arcot.test.vpn;
import org.sikuli.script.*;
public class AuthLogin {
public static void main(String[] args) {
Screen s = new Screen();
App myApp = new App("application-identifier") ;
myApp.open ("C:\\Program Files\\acd\\bin\\VPNClient.exe");
//How to simulate the type, sleep and click functions here?
I am searching for java examples to understand the objects relation and how to use it to simulate the recorded scripts. Please provide if any of you know the links that help me.
Best regards,
Madhu

After your program, proceed in following way:
package com.arcot.test.vpn;
import org.sikuli.script.*;
public class AuthLogin {
public static void main(String[] args) {
Screen s = new Screen();
App myApp = new App("application-identifier") ;
myApp.open ("C:\\Program Files\\acd\\bin\\VPNClient.exe");
Kindly proceed in this way,
-Create one image folder inside your package "img"
-Copy all the respective images in the img folder
-Assign the image names in a folder to a different variables
For doing operations, use follwing command:
s.type("mganda1");
s.sleep(time);
s.click("ok.png");
Regards,
Npesik

Madhu,
I'm not sure why you recorded the script to lunch that app with sikuli. All of the commands yu use don't invoke any images and can all be written without the sikuli ide.
I would make the following changes to your original sikuli/jython script
App.open ("C:\\Program Files\\acd\\bin\\VPNClient.exe")
sleep(1)
//change to
wait(path to image, FOREVER)
//By changing to a wait there is an implicit find as defined by the path to the image
type ("mganda1")
//if there are issues verifying focus invoke type with the img option
sleep(1)
//use wait instead of sleep
click( ) //click OK
//What are you clicking on?
Regarding Java, here's the link to Sikuli javadocs

Related

Sikuli on Java does not recognize images

I have been trying to automatize some tasks on my computer and did choose Sikuli from Java to do so (I work with Java everyday and didn't know any automation tool using java, sikuli was the first I found). I use java with maven and eclipse as IDE. I have added Sikuli as a Maven dependency.
<dependency>
<groupId>com.sikulix</groupId>
<artifactId>sikulixapi</artifactId>
<version>2.0.5</version>
</dependency>
I tried to do some simple stuff. I did a few screenshots of parts of my screen using windows' screenshot tool, and wanted sikuli to hover it. It works quite fine for one image, but not at all for the others. It seems that the bigger the image the better it works as I did not have success for any small images. The one working is a screen of a whole window (reduced to ~1/4 of my screen).
I also tried to find a button inside this window, to find the windows logo on bottom left, to find a screen of my package explorer, but none work correctly.
I played with similar() using various values, but it didn't improve the results. In some cases (button inside the window) it did find a result for some low similar value, but it was another button. The weird part is : its finding this other button which is bright blue, while the one i'm looking for is purple.
My pc background never changes, I did some screen.highlight() and its looking at the correct screen (dual screen). It's not an issue with the path to images (already solved this one).
Do you have any idea of what I could try ? I have read about people having different success rate depending on whether they were using Sikuli IDE or another IDE. So maybe I could give sikuli IDE a try.
I can give code samples as soon as I am back home.
The code I'm using to test :
public class CleanTest {
static Screen screen = new Screen();
public static void main(String[] args) throws FindFailed, AWTException, IOException, InterruptedException {
String pathYourSystem = System.getProperty("user.dir") + "\\";
System.out.println(pathYourSystem);
Pattern pLauncher = new Pattern(pathYourSystem+"img\\full_launcher.PNG").similar(0.9d);
Desktop.getDesktop().open(new File("path_to_an_exe_opening_a_launcher"));
screen.wait(pLauncher, 300);
screen.mouseMove();
System.out.println("launcher found");
}
}
It works with the "full launcher" image, but it doesn't find a sub-part of the launcher (a button). I tried to make some code to test if there was some threshold for the similar parameter :
double similarValue = 1d;
Pattern pLauncher = new Pattern(pathYourSystem+"img\\the_button.PNG").similar(similarValue);
Desktop.getDesktop().open(new File("path_to_an_exe_opening_a_launcher"));
while(!screen.has(pLauncher)) {
similarValue-=0.1;
pLauncher = new Pattern(pathYourSystem+"img\\login.PNG").similar(similarValue);
}
System.out.println(similarValue);
screen.mouseMove();
it finds something at around 0.5, but it's a totally different button.
Thank you !
EDIT: if someone has the same issue, try to use sikulix IDE to take the screenshots. It works with the screenshots taken by the IDE.
This is a simple test, that completely stays within the SikuliX features.
import org.sikuli.basics.Debug;
import org.sikuli.script.*;
public class SikulixTest {
public static void main(String[] args) {
System.out.println("SikulixTest");
Screen scr = new Screen();
// craete an image to be searched on the screen
Image img = new Image(scr.userCapture());
// try to find it
Match mImg = scr.exists(img);
if (mImg != null) {
// show sthg. when found
Debug.info("%s", mImg);
mImg.highlight(2);
}
}
}
This is RaiMan from SikuliX

Is there any way to record the screenshots of minimized browser windows

I am using Java and selenium to write some tests. I need to have my screen records while the tests are running it makes much easier for me to track if any bugs occurs. The problem is that I need to run more than one tests at the same time and as I have only one monitor I cannot record all of their screen records at the same time so I have to run the test one after each other. I was wondering if there is any way that I can run all my tests and actually minimize their browsers windows but still record what is going on at each minimized chrome window. My question may sound a bit very strange but that makes my testing very faster.
Yes, definitely we can take multiple screenshots. There is no affect whether the browser is in minimize or Maximize condition. Just you have to switch the new opened window & add "Take screenshot" method after each method where you have to take screenshot.
Take screenshot method can work in both mode while browser is either in Minimize or Maximize condition.
For screenshot you can may use the below code:
public void getscreenshot() throws Exception
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in d drive with name "screenshot.png"
FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
}
or you can opt for the multi-screen capture and the code for that is below :
public void GoogleAbout() throws Exception {
driver.get(baseUrl); // Enter the URL as per your choice
driver.findElement(By.linkText(Prop.getProperty("AboutLinkText"))).click(); //find the web element
MultiScreenShot multiScreens = new MultiScreenShot("C:\\New\\","GoogleAbout"); // Here we need to create the object which will take two arguement one is the path to save the file and second one is class name
multiScreens.multiScreenShot(driver);
driver.findElement(By.linkText("More about our philosophy")).click();
multiScreens.multiScreenShot(driver);
}
To enable the multi-screenshot you have to download the JAR file and then attached it to your project and then :
import multiScreenShot.MultiScreenShot;

TestStack White error JAVA app

I try TestStack.White to run an automation for JAVA app, here in my example is Calculator coded in JAVA, under VS2015 in C#. I installed TestStack.White using NuGet and tring with Winform app and it works, but here with java app, I can open the application, But not all component I can click on it.
So I try to know the component name with UIA Verify interface, but it selects the whole window, after that I look for something similar to UI Verify, and I found Ranorex STudio with Track Features and it works I can see Java Component name, but after I know the name, and I searsh for it in the app, the code not working. This is my code:
class Program
{
static void Main(string[] args)
{
Application aa = Application.Launch("..\\..\\CalculatorOrange.jar");
Window calcWindos = aa.GetWindow("Calculator Lite - 1.5.3", InitializeOption.NoCache);
Button twoBtn = calcWindos.Get<Button>(SearchCriteria.ByText("8"));
twoBtn.Click();
//var twoBtn = calcWindos.Get<MenuBar>("mnuFile");
//twoBtn.Click();
}
}
and it shows this error:
An unhandled exception of type 'TestStack.White.AutomationException'
occurred in TestStack.White.dll
Additional information: Failed to get (ControlType=button or
ControlType=check box),Name=8
This image shows the component type and name
Update 1 : On the left, I use Ranorex Spy, and it shows all the Component, and on the right the Inspect for Microsoft

Java wait for method to return object

I am making a library where an application can use it to capture a selection of a screen and convert it to an image, like Gyazo.
This library is not the application itself, but only the tool that returns the File or BufferedImage object to the application.
I want the application to be simple as this:
Bootstrap b = new Boostrap(new GifCapturer());
b.beginCapture(); // user selects an area
File file = b.getFile();
But how can I make the application wait till the library returns the object? as you see the beginCapture method should activate the JFrame where the user will select an area to capture.
Do I need to sleep the thread? or use listeners design?
The beginCapture method starts a jframe window, where the user is able to select an area of the screen. Once selected, the library will convert the selected area to an object and set it as a local variable. So when you will use getFile it ill return the captured image. But the thing is, i need to make sure that the image was selected before getFile call gets executed, and wait instead but im not sure how.
Sorry if the question is not detailed, im on phone.
Please let me know if you need more information.
Implement a listener, that is invoked as soon the selection is ready. Put your File file = b.getFile(); code into the listener.
The code of your JFrame would be necessary to give a more detailed answer.
I have decided to use a Listener with a own built listener class, and interface.
Create an interface which you will use to get the data, or that will get know when the listener gets called, like this in my case:
public static void main(String[] args) throws AWTException {
Bootstrap b = new Bootstrap(new GifCapturer());
b.beginCapture(new ScreenCaptureCallback() {
#Override
public void captureEnded(File file) {
System.out.println("done!");
}
});
}

Is there a way to expand a JFileChooser directory without a mouse

Using a JFileChooser, I can select a directory by double clicking the directory (going down a level) with my mouse. Is there a way to select a directory without the mouse? For example, is there a key binding to go down a directory level or do I have to somehow add a key listener to the JFileChooser?
You should be able to use tab to move between the different parts of the chooser, and then use the arrow keys to change which directory is highlighted, and then press Enter to change the directory to the highlighted one.
I have tested the following example code on my machine (Vista/JDK 1.6) and it works as I would expect:
import javax.swing.*;
public class test {
public static void main(String[] args) {
(new JFileChooser("")).showOpenDialog(new JFrame());
System.out.println("OK!");
}
}
If your project is not responding similiarly in your JFileChooser, I would debug as follows:
Create test.java with only the code necessary to pop up a chooser.
If the test app differently than within your app, its something in your code causing it to fail, such as UI skinning code, keyboard listeners, etc. Modify the example, one change at a time to closer replicate your settings for your chooser in your app and see if you can pinpoint where it breaks.
If even a basic test app doesn't work right, it is probably something about your setup, such as a bug in your JDK version, your OS, etc. Troubleshoot your setup.
Have you tried the space-bar or enter key?
Try using ctrl+enter key to select directory.
This behavior is happened when you set to JFileChooser file selection mode to “files and direcories”:
JFileChooser fileBrowser = new JFileChooser();
fileBrowser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

Categories

Resources