mac上用Automator编写自动脚本

最近努力学习新知识,在公司内网上阅读一些优秀文章,但是发现这些文章:是通过通过flash控件展示,并且不提供无法下载地址(类似《百度文库》)。一次忍了,两次再忍了,三次就无法忍受:都在我电脑上展示了,这么做就是恶心人了。。。。
于是花了大半天时间,使用mac上自带软件Automator,从零开始完成了自己的工具:《连续抓图导出PDF》。

实现原理:自动截屏、翻页=》merge生成PDF文档。(感兴趣的人,可以下载附件看看源码)

在写这个脚本过程中,本以为会很快,但是还是碰到了不少问题;于是就记录并分享出来。

首先:最基本用法,可以直接参考Automator帮助,本文章只写自认为容易忽略或者难点的几个地方。


变量使用

变量新建就不说了;这里主要说变量再控件上使用,几种方法(有的方法只对部分控件有用):

从下拉列表直接选择“变量”

这里写图片描述

将“变量”拖进区域

这里写图片描述
当然你也可以,抛弃使用已存在的变量,直接通过下拉框选择一个“新建变量”。

“输入变量名”弹出提示,“回车”选择

这里写图片描述
这里写图片描述

最后,如果上面三种方法你尝试都不行的话,基本上可以宣告,那个是不支持变量的。
比如,LOOP的“次数”,是不支持变量的:
这里写图片描述


脚本

脚本使用

Automator脚本支持:shell脚本(bash、perl、python、ruby等)、appleScript、javascript几种,你可以选择不同的脚本来运行。
这里写图片描述

脚本参数传递

脚本和变量之间,是无法直接获取或设置的

“变量”值传递到脚本

– shell脚本

        获取到的输入:只能是通过输入的一行一行的字符串
        也即如果,想获取变量值的话,可以通过,“获得变量的值”控件来输出值,然后传递到脚本然后再读取。
        如:下面是获取“页数”、“URL”、“临时文件夹”三个变量值到输出,然后传递给perl脚本。

这里写图片描述

– AppleScript脚本

AppleScript的输入不是一行一行的字符串,貌似是键值队(还不熟悉这块),结果没搞懂如何传递的变量——搞懂后,再补充。
于是我考虑的解决方案是:通过分隔符“|”将输入的多行转换成一行,然后在AppleScript进行反转。的确是有些trick -_-!——不过后来查资料发现,也有人这么整。

这里写图片描述
perl代码:

@input = ();
while (<>) {
        $_=~s/(^\s*)|(\s*$)//g;
        push(@input,$_);
}
print join "|" ,@input;

AppleScript代码:

on run input

    set myArray to my theSplit((item 1 of input) as string, "|")
    set outputPath to item 1 of myArray
    set pagenum to item 2 of myArray as number
    set myurl to item 3 of myArray
    set tempDir to item 4 of myArray

    return myArray
end run

on theSplit(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters

    -- set delimiters to delimiter to be used
    set AppleScript's text item delimiters to theDelimiter

    -- create the array
    --set theArray to every text item of theString
    set theArray to text items of theString

    -- restore the old setting
    set AppleScript's text item delimiters to oldDelimiters

    -- return the result
    return theArray
end theSplit

脚本结果,设置到“变量”中

脚本中的变量,是无法设置到脚本中,怎么办呢?其也只能通过脚本输出结果,然后“设定变量的值”控件,对变量进行设置。
- - 设置一个变量

这里写图片描述

注意:这里有一个问题,如果脚本输出的是多行,其实只把第一行字符串,赋值到“变量”中

    • 设置多个变量

但是如果要设置多个变量值,咋办?
于是又是一个trick方法来了,利用上面“只会把第一行赋值给变量”的机制,通过“漏斗”方式逐个对变量进行赋值:
这里写图片描述
如上图,依次对“导出路径”、“页数”等多个变量进行逐个复制。
其中perl代码作用是:过滤第一行字符串:

$count=0;
while(<>){
    print $_ if $count >0 ; 
    $count = $count + 1 ;
}

键盘录制&鼠标录制

Automator提供“录制”功能,位于右上角:
这里写图片描述
注意如果你使用录制功能,需要mac再偏好设置里,设置一下“允许Automator控制你的电脑”,这样在运行时才有用:
这里写图片描述

但是实际操作起来,我碰到了下面两个问题:

1、鼠标录制,总是不准
2、键盘录制,则永远也没有录制上去

第一个问题,我没有找到很好办法;最后没有使用;
第二个问题,不是录入,而是写代码,通过appleScript来实现:

键盘操作

这里写图片描述
脚本代码是:

on run {input, parameters}
    -- 
    
  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UiAutomator是Android平台自带的测试框架,可以用于编写Android应用程序的UI自动化测试用例。以下是编写app自动化的一般步骤: 1. 首先,需要在Android Studio中创建一个新的项目并选择“空活动”。 2. 接下来,需要在项目中添加所需的依赖项。可以在build.gradle文件中添加以下依赖项: ```groovy dependencies { androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3' } ``` 3. 创建一个测试类,并导入必要的类: ```java import androidx.test.core.app.ActivityScenario; import androidx.test.espresso.matcher.ViewMatchers; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.uiautomator.By; import androidx.test.uiautomator.UiDevice; import androidx.test.uiautomator.UiObject2; import androidx.test.uiautomator.UiSelector; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; ``` 4. 编写测试用例,并使用UiDevice类与应用程序进行交互。例如,下面的示例代码将启动应用程序并单击按钮: ```java @RunWith(AndroidJUnit4.class) public class MainActivityTest { private UiDevice mDevice; @Before public void setUp() throws Exception { mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); mDevice.pressHome(); // Wait for launcher final String launcherPackageName = mDevice.getLauncherPackageName(); assertThat(launcherPackageName, notNullValue()); mDevice.wait(Until.hasObject(By.pkg(launcherPackageName).depth(0)), 5000); // Launch the app Context context = InstrumentationRegistry.getInstrumentation().getContext(); final Intent intent = context.getPackageManager().getLaunchIntentForPackage("com.example.myapp"); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); // Wait for the app to appear mDevice.wait(Until.hasObject(By.pkg("com.example.myapp").depth(0)), 5000); } @Test public void testButton() { UiObject2 button = mDevice.wait(Until.findObject(By.res("com.example.myapp:id/my_button")), 5000); assertThat(button, notNullValue()); button.click(); } } ``` 5. 运行测试用例。可以通过在终端中运行以下命令来运行测试: ``` ./gradlew connectedAndroidTest ``` 这将启动模拟器/设备并运行测试用例。 总之,使用UiAutomator编写app自动化测试用例需要一定的编程经验,并且需要对Android应用程序的UI结构有一定的了解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值