【问题标题】:Cannot run program "osascript": error=2, No such file or directory无法运行程序“osascript”:错误=2,没有这样的文件或目录
【发布时间】:2016-02-02 19:59:32
【问题描述】:

我正在尝试在远程网格上托管的 selenium 测试中运行以下 AppleScript。

protected void enableTouchIDLogin(){
   Runtime runtime = Runtime.getRuntime();
   String appleScriptCommand =   "tell application \"System Events\" to tell process \"Simulator\"\n" +
                   "click menu item \"Touch ID Enrolled\" of menu 1 of menu bar item \"Hardware\" of menu bar 1\n"+
                   "end tell";


   String[] args = { "osascript", "-e", appleScriptCommand};
   try
   {
     Process process = runtime.exec(args);
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
}

当我在本地运行测试时,它工作正常。但是在远程网格上我得到了

java.io.IOException: Cannot run program "osascript": error=2, No such file or directory at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source)

我不确定为什么会这样。在远程网格上,“which osascript”返回“/usr/bin/osascript”。这与我的 osascript 在本地运行时的位置相同。

鉴于本地和远程网格上的路径是相同的,我不确定为什么 -e 标志不起作用。我不确定我的 appleScriptCommand 应该是什么样子...

编辑

根据这里的一个回复,我尝试了以下操作,它不会引发错误,但也不会在本地或远程执行功能。

  protected void enableTouchIDLogin(){



   try
   {
       Runtime runtime = Runtime.getRuntime();
       String appleScriptCommand =   "tell application \"System Events\" to tell process \"Simulator\"\n" +
                       "click menu item \"Touch ID Enrolled\" of menu 1 of menu bar item \"Hardware\" of menu bar 1\n"+
                       "end tell";

       File executor = File.createTempFile("exec", ".sh");
       PrintWriter writer = new PrintWriter(executor, "UTF-8");
       writer.println("#!/bin/bash");
       writer.println();
       writer.println(String.format("osascript -e \"do shell script \\\"%s\\\" with administrator privileges\"",
                       appleScriptCommand));
       writer.close();
       executor.setExecutable(true);

       Process process = runtime.exec(String.format("%s",
                       executor.getPath()));
   }
   catch (Exception e)
   {
       e.printStackTrace();
   }

  }   

【问题讨论】:

  • 我改变了我的代码,现在试试新的;)
  • 新更新:) 往下看

标签: java selenium applescript appium osascript


【解决方案1】:

这对我很有用
更新5:

String script = "tell application \\\"System Events\\\" to tell process \\\"Simulator\\\"\n" +
                   "click menu item \\\"Touch ID Enrolled\\\" of menu 1 of menu bar item \\\"Hardware\\\" of menu bar 1\n"+
                   "end tell";
File executor = File.createTempFile("exec", ".sh");
PrintWriter writer = new PrintWriter(executor, "UTF-8");
writer.println("#!/bin/bash");
writer.println();
writer.println(String.format("osascript -e \"%s\" with administrator privileges",
        script);
writer.close();
executor.setExecutable(true);

Runtime.getRuntime().exec(String.format("%s", 
        executor.getPath()));

【讨论】:

  • 我已经尝试过了,但它对我不起作用......我已经更新了我的问题以反映这段代码。这不会引发错误,但它也对我没有任何作用
  • 我也以这种方式更新了我的代码。现在在本地,它要求我输入密码以允许进行更改....但是在我正在运行的远程网格 Saucelabs 上,此提示永远不会出现命令永远不会运行。
  • 我不明白为什么它不起作用..也许可以尝试一下:
  • 我不明白为什么它不起作用..你的脚本是 osascript ?然后尝试我的新更新...
  • 让我试试这个然后回复你。它是一个 osascript,它从一开始就在本地运行良好。只是远程是问题。
猜你喜欢
  • 2018-01-30
  • 2016-08-22
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
相关资源
最近更新 更多