0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Sikulixの学習

0
Last updated at Posted at 2025-10-04

概要

Sikulixに触れまして、その備忘録を残します。。。
簡単なサンプルコードでの動作確認にとどめていますので
ご了承ください。

準備

1)Javaをインストール
2)Sikulix IDE(sikulixide-2.0.5.jar) をダウンロード
3)JRuby(jruby-complete-10.0.2.0.jar)をSikulixのExtensionsフォルダに配置
 C:\Users\ユーザー名\AppData\Roaming\Sikulix\Extensions
4)3でのフォルダでJRubyにgemをインストール

java -jar jruby-complete-10.0.2.0.jar -S gem install sikuli

5)Sikulix IDEとJRubyを同じディレクトリに置く。
JRubyは3でのものと同一のものを置く。

実行 メモ帳の起動

今回はメモ帳を起動して任意の文字列を入力できるようにしました。
ディレクトリ構成は次のようになっています。

C:\SikukiX
├─ sikulixide-2.0.5.jar
├─ jruby-complete-10.0.2.0.jar
└─ sample.rb

sample.rb
require 'java'
include Java

# SikuliXのライブラリを読み込む
require_relative 'sikulixide-2.0.5.jar'

java_import 'org.sikuli.script.Screen'

screen = Screen.new

# メモ帳を起動(Windows環境)
system("notepad")

# 少し待機
sleep(2)

# "Hello World" を入力
screen.type("Hello World")

実行コマンド

java -cp "jruby-complete-10.0.2.0.jar;sikulixide-2.0.5.jar" org.jruby.Main sample.rb

python活用(2026/2/8記載)

目的:デスクトップ画面のアイコンをPython → JRuby → SikuliX の順で呼び出し押下させる。
ディレクトリ構成は次のようになっています。
C:\SikukiX
├─ sikulixide-2.0.5.jar
├─ jruby-complete-10.0.2.0.jar
└─ sample.rb
└─ test_runner.py
└─ images/
  └─ button.png

test_runner.py
import subprocess
import json
import time
from pathlib import Path

JRUBY_CMD = [
    "java",
    "-cp", "jruby-complete-10.0.2.0.jar;sikulixide-2.0.5.jar",
    "org.jruby.Main",
    "sample.rb"
]

def run_sikuli(action, target):
    cmd = JRUBY_CMD + [action, target]
    result = subprocess.run(cmd, capture_output=True, text=True)
    return result.returncode == 0, result.stdout, result.stderr


def run_test_case(test_case):
    print(f"Running: {test_case}")
    ok, out, err = run_sikuli(test_case["action"], test_case["target"])
    time.sleep(1)
    return {
        "case": test_case,
        "success": ok,
        "stdout": out,
        "stderr": err
    }


def generate_html_report(results, output="report.html"):
    html = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>GUI Test Report</title>
<style>
body { font-family: Arial; margin: 20px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ccc; padding: 8px; }
th { background: #eee; }
.success { background: #d4ffd4; }
.fail { background: #ffd4d4; }
pre { white-space: pre-wrap; }
</style>
</head>
<body>
<h1>GUI Test Report</h1>
<table>
<tr>
  <th>Result</th>
  <th>Action</th>
  <th>Target</th>
  <th>Stdout</th>
  <th>Stderr</th>
</tr>
"""

    for r in results:
        cls = "success" if r["success"] else "fail"
        html += f"""
<tr class="{cls}">
  <td>{'OK' if r["success"] else 'NG'}</td>
  <td>{r["case"]["action"]}</td>
  <td>{r["case"]["target"]}</td>
  <td><pre>{r["stdout"]}</pre></td>
  <td><pre>{r["stderr"]}</pre></td>
</tr>
"""

    html += """
</table>
</body>
</html>
"""

    Path(output).write_text(html, encoding="utf-8")
    print(f"HTML report generated: {output}")


def main():
    test_scenario = [
        {"action": "click", "target": "images/button.png"},
    ]

    results = []
    for case in test_scenario:
        results.append(run_test_case(case))

    # JSON ログ
    Path("test_log.json").write_text(
        json.dumps(results, indent=2, ensure_ascii=False),
        encoding="utf-8"
    )

    # HTML レポート生成
    generate_html_report(results)


if __name__ == "__main__":
    main()

sample.rb
require 'java'
include Java

require_relative 'sikulixide-2.0.5.jar'
java_import 'org.sikuli.script.Screen'
java_import 'org.sikuli.script.Pattern'

screen = Screen.new

# Python から渡された引数
action = ARGV[0]
target = ARGV[1]

# 画像パスを絶対パスに変換
require 'pathname'
target_path = Pathname.new(target).realpath.to_s

begin
  case action
  when "click"
    screen.click(Pattern.new(target_path))
    puts "Clicked: #{target_path}"

  when "wait"
    screen.wait(Pattern.new(target_path), 5)
    puts "Waited for: #{target_path}"

  else
    puts "Unknown action: #{action}"
    exit(1)
  end

rescue => e
  puts "Error: #{e}"
  exit(1)
end

exit(0)

実行コマンド

py test_runner.py

実行結果

report.html
が下記のように結果を表示する。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?