Sikuli click. (ScreenCaptureImage) in java - java

I am new to Sikuli and I wanted to
1. click windows button, and
2. type "Helloworld"
3. press Enter.
I have coded this and working Successfully in Sikuli IDE
click("1391583846712.png")
type("helloWorld")
wait(2)
type(Key.ENTER)
I tried to move this to Java ,
From the sikuli javadocs I have seen the following code, However it is not working in java sikuli-api-1.0.2 and latest version
import org.sikuli.script.*;
public class TestSikuli {
public static void main(String[] args) {
Screen s = new Screen();
try{
s.click("imgs/win-start.png", 0);
s.wait("imgs/spotlight-input.png");
s.type(null, "hello world\n", 0);
}
catch(FindFailed e){
e.printStackTrace();
}
}
}
It tells that Screen is an interface . Please tell me how to make it working in latest java sikuli-api. Please see that I am very new to Sikuli . Any suggestions will be highly appreciated. Also Please point me to the right sikuli java for begineers

new org.sikuli.api.DesktopScreenRegion() creates a ScreenRegion on the base full screen where you can click and seek your images
Your best bet to find how the new API is built is to look at the sources. There aren't a lot of classes to understand, fortunately.

The following Sikuli Java code should work:
import org.sikuli.script.*;
public class HelloWorld {
public static void main(String[] args){
Screen screen = new Screen();
try{
screen.click("D:\\Sikuli\\WinStartButton.png");
//"WinStartButton.png" must exist on the desired location you are using
//OR, instead of above line you can use the following:
screen.type(Key.WIN);
}
catch(FindFailed e){
e.getStackTrace();
}
screen.type("Hello World");
screen.type(Key.ENTER);
}
}

Try to use image locator in your code,
import org.sikuli.script.*;
import org.sikuli.basics.ImageLocator;
public class AuthLogin {
public static void main(String[] args) {
Screen s = new Screen();
ImageLocator.setBundlePath("path to img directory");
try{
s.click("win-start.png", 0);
s.wait("spotlight-input.png");
s.type(null, "hello world\n", 0);
}
catch(FindFailed e){
e.printStackTrace();
}
}

I think you should not use the absolute image path directly in the code.
I would create a class which contains the absolute paths as static constants.
Example :
instead of :
screen.click( "D:\\Sikuli\\WinStartButton.png");
you can do it like this :
public static final String IMAGE = "D:\\Sikuli\\WinStartButton.png";
screen.click(IMAGE);

Use Keydown and Keyup method for Enter key pressed
I have tried to organize the entire code. please let me know if its working.
Screen sikuli = new Screen();
String message = "hello world";
Pattern imgLocator = "";
if(sikuli.exists(imgLocator)!=null) {
sikuli.find(imgLocator);
sikuli.click(imgLocator);
sikuli.wait(2);
sikuli.type(imgLocator, message);
}
sikuli.keyDown(Key.ENTER);
sikuli.keyUp(Key.ENTER);

Related

Why does Playwright's generated Java code have invalid syntax?

When I use the Playwright's codegen feature it traces my clickpath into a Java file. But the created file has the wrong syntax, so I can't compile it.
I start the codegen with:
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"
And the inspector provides this code:
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
page.navigate("https://en.wikipedia.org/wiki/Main_Page");
page.getByPlaceholder("Search Wikipedia").click();
page.getByPlaceholder("Search Wikipedia").fill("stackoverflow");
page.getByRole("button", new Page.GetByRoleOptions().setName("Go")).click();
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Stack_Overflow");
}
}
}
But there is already the first error. The method getByRole requires an AriaRole as its first parameter, not a String. So it's easy to fix, but I think it's not the idea of the product to generate code and let the developer fix it.
In some YouTube tutorials the inspector generates only fill and click functions with powerful selectors inside.
Is there a way to change the generated output to a specifc "code-style"? Or is there another reason why other people get nice working code and I don't?
My dependency:
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.27.0</version>
</dependency>
Sorry if I am wrong. But you should get something like this from an inspector which compiles fine
package org.example;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import java.util.*;
public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(false));
BrowserContext context = browser.newContext();
// Open new page
Page page = context.newPage();
// Go to https://www.wikipedia.org/
page.navigate("https://www.wikipedia.org/");
// Click input[name="search"]
page.locator("input[name=\"search\"]").click();
// Fill input[name="search"]
page.locator("input[name=\"search\"]").fill("stackoverflow");
// Click button:has-text("Search")
page.locator("button:has-text(\"Search\")").click();
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Stack_Overflow");
}
}
}

Detect Language of String with com.cybozu.labs.langdetect package

I am searching for a small example code to detect the language of a string in JAVA. For that i downloaded and imported the following GitHub Project: https://github.com/shuyo/language-detection
Unfortunately I am struggling reading the API and I don't know how to get my code to work. Help is very appreciated. Heres what i have so far. I get a NullPointerException because i dont know how to initialize the Detector properly. ny help is kindly appreciated.
import com.cybozu.labs.langdetect.*;
public class DetectLanguage {
public static void main(String[] args) throws LangDetectException {
String sample = "Comment vous appelez-vous?"; // french demo text
Detector d = new Detector(null); // initialize detector
d.append(sample);
System.out.println(d.detect());
}
}
The Detector constructor signature is:
public Detector(DetectorFactory factory)
So take a look to the DetectorFactory, is a singleton without getInstance() method:
You should create your Detector like this:
Detector d = DetectorFactory.create();
But if you just doing that, is not enough...
com.cybozu.labs.langdetect.LangDetectException: need to load profiles
So the minimal and complete work example is:
try {
String sample = "Comment vous appelez-vous?";
// Prepare the profile before
DetectorFactory.loadProfile("/language-detection/profiles");
// Create the Detector
Detector d = DetectorFactory.create();
d.append(sample);
System.out.println(d.detect()); // Ouput: "fr"
} catch (LangDetectException e) {
e.printStackTrace();
}
And when you test these strings:
String sample = "Comment vous appelez-vous ?"; // "fr"
String sample = "Buongiorno come stai ?"; // "it"
String sample = "Hello how are you ?"; // "en"

"Image not valid, but TextSearch is switched off!" error - Sikuli on Mac

I am trying to automate a desktop application on Mac using Sikuli and Eclipse.
Source code:
import org.sikuli.script.FindFailed;
import org.sikuli.script.ImagePath;
import org.sikuli.script.Screen;
public class TextEditorExample {
public static void main(String[] args) throws FindFailed {
// TODO Auto-generated method stub
Screen s=new Screen();
System.out.println(ImagePath.getBundlePath());
s.click("spotlight_icon.png");
s.find("spotlight.png");
s.type("spotlight.png","finder");
s.click("applications.png");
s.click("texteditor_icon.png");
s.find("texteditor.png");
s.type("texteditor.png","Sikuli Example");
}
}
But I'm getting the following error :
[error] Image: Image not valid, but TextSearch is switched off!
[error] RunTimeAPI: Wait: Abort: unknown
[error] RunTimeAPI: ImageMissing: spotlight_icon.png
Path of sikuli script:
/Users/adamin/Desktop/Automation/SikuliExample/src/TextEditorExample.java
Path of Images:
/Users/adamin/Desktop/Automation/SikuliExample/src/spotlight_icon.png
/Users/adamin/Desktop/Automation/SikuliExample/src/spotlight.png
/Users/adamin/Desktop/Automation/SikuliExample/src/applications.png
/Users/adamin/Desktop/Automation/SikuliExample/src/texteditor_icon.png
/Users/adamin/Desktop/Automation/SikuliExample/src/texteditor.png
Can anybody help me in solving this issue?
The imagepath is set by default to your project root folder and will only look for patterns there. Just set the bundle path manually to wherever your files are:
ImagePath.setBundlePath("fullpath");
Alternatively, place your files to whatever folder that is returned by:
System.out.println(ImagePath.getBundlePath());
Use Pattern.
Pattern pattern = new Pattern(path+"spotlight_icon.png");
Screen s=new Screen();
try {
s.click(pattern);
} catch (FindFailed e) {
e.printStackTrace();
}
This Error most probably comes when image is not loadable, Meanwhile, use this approach
try{
String path = "path of your image";
Pattern target = new Pattern(path);
Screen scr = new Screen();
scr.click(target);
}
catch(Exception e)
{
e.printStackTrace();
}
Y

How do I get a class from an external .jar from a different computer?

I'm building a video game and I've built a launcher for my video game as well. The launcher downloads .jar files and stores them in the %appdata% folder for each person who buys the game and downloads the launcher and then runs it.
I need to be able to write a few lines of code to tell the launcher to get the .jar file from the user's computer and run a file from there. The .jar is already compiled and everything is okay and whatnot, but I'm not quite sure how to get the .class file to work with.
Something like this might help:
import System.getPropery("user.home") + "/AppData/Roaming/GameNameHere/bin/game.jar" + ".runGame.class"
And then I could possible do something like this:
if (credentials == true) {
runGame game = new runGame();
game.start();
}
How would I do something like this? Thanks in advance.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So, I looked the ClassLoader.java class and messed around with it for a bit, but nothing really worked well. What am I doing wrong?
private String location = System.getProperty("user.home") + "\\Desktop\\myJar.jar";
URL url = new URL(location);
public Load() throws Exception {
ClassLoader loader = URLClassLoader.newInstance(new URL[]{url}, getClass().getClassLoader());
Class<?> clazz = Class.forName("gumptastic.MyClass", true, loader);
Method method = clazz.getMethod("output");
method.invoke(this);
}
public static void main(String[] args) {
try {
new Load();
} catch (Exception exc) {
exc.printStackTrace();
}
}
Not sure if you're familiar with this but
I think you should look at class loaders.
http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html
I guess you would need to write a simple one for your particular needs.
Alternatively, it would be even easier if you just use URLClassLoader.
Below is a simple example. This program has no idea of the Gson class
at compile time. But it can successfully load it, create an instance of it,
and use it at runtime. It was tested on Windows 7.
You can download Google Gson from here.
http://code.google.com/p/google-gson/downloads/list
Then place the gson-2.2.4.jar file anywhere you like
on your computer, then point this program to it by
setting arr[0] in the proper way.
Then observe the magic that is taking place :)
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
public class Test007 {
public static void main(String[] args) throws Exception {
URL[] arr = new URL[1];
arr[0] = new URL("file:///dir1/dir2/dir3/gson-2.2.4.jar");
URLClassLoader loader = new URLClassLoader(arr);
Class cls = loader.loadClass("com.google.gson.Gson");
System.out.println(cls);
Constructor constructor = cls.getConstructor(new Class[0]);
Object obj = constructor.newInstance(new Object[0]);
System.out.println(obj);
if (obj!=null){
System.out.println("OK, so now we have an instance of:");
System.out.println(obj.getClass().getName());
}
}
}

How do I change the default application icon in Java?

I'm using NetBeans, trying to change the familiar Java coffee cup icon to a png file that I have saved in a resources directory in the jar file. I've found many different web pages that claim they have a solution, but so far none of them work.
Here's what I have at the moment (leaving out the try-catch block):
URL url = new URL("com/xyz/resources/camera.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
getFrame().setIconImage(img);
The class that contains this code is in the com.xyz package, if that makes any difference. That class also extends JFrame. This code is throwing a MalformedUrlException on the first line.
Anyone have a solution that works?
java.net.URL url = ClassLoader.getSystemResource("com/xyz/resources/camera.png");
May or may not require a '/' at the front of the path.
You can simply go Netbeans, in the design view, go to JFrame property, choose icon image property, Choose Set Form's iconImage property using: "Custom code" and then in the Form.SetIconImage() function put the following code:
Toolkit.getDefaultToolkit().getImage(name_of_your_JFrame.class.getResource("image.png"))
Do not forget to import:
import java.awt.Toolkit;
in the source code!
Or place the image in a location relative to a class and you don't need all that package/path info in the string itself.
com.xyz.SomeClassInThisPackage.class.getResource( "resources/camera.png" );
That way if you move the class to a different package, you dont have to find all the strings, you just move the class and its resources directory.
Try This write after
initcomponents();
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("Your image address")));
/** Creates new form Java Program1*/
public Java Program1()
Image im = null;
try {
im = ImageIO.read(getClass().getResource("/image location"));
} catch (IOException ex) {
Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex);
}
setIconImage(im);
This is what I used in the GUI in netbeans and it worked perfectly
In a class that extends a javax.swing.JFrame use method setIconImage.
this.setIconImage(new ImageIcon(getClass().getResource("/resource/icon.png")).getImage());
You should define icons of various size, Windows and Linux distros like Ubuntu use different icons in Taskbar and Alt-Tab.
public static final URL ICON16 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug16.png");
public static final URL ICON32 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug32.png");
public static final URL ICON96 = HelperUi.class.getResource("/com/jsql/view/swing/resources/images/software/bug96.png");
List<Image> images = new ArrayList<>();
try {
images.add(ImageIO.read(HelperUi.ICON96));
images.add(ImageIO.read(HelperUi.ICON32));
images.add(ImageIO.read(HelperUi.ICON16));
} catch (IOException e) {
LOGGER.error(e, e);
}
// Define a small and large app icon
this.setIconImages(images);
You can try this one, it works just fine :
` ImageIcon icon = new ImageIcon(".//Ressources//User_50.png");
this.setIconImage(icon.getImage());`
inside frame constructor
try{
setIconImage(ImageIO.read(new File("./images/icon.png")));
}
catch (Exception ex){
//do something
}
Example:
URL imageURL = this.getClass().getClassLoader().getResource("Gui/icon/report-go-icon.png");
ImageIcon iChing = new ImageIcon("C:\\Users\\RrezartP\\Documents\\NetBeansProjects\\Inventari\\src\\Gui\\icon\\report-go-icon.png");
btnReport.setIcon(iChing);
System.out.println(imageURL);

Categories

Resources