iOS 使用fastlane screenshots 自动化截图、为图片添加手机框

**# fastlane screenshots **

图片失效请去 博客 查看

文中使用到的文件都在这里

## 截屏使用

官方使用文档

### 准备条件

  1. 项目中使用 UI Tests ,新项目可以直接勾选,老项目点击addTarget 找到 UITests 添加之后 会生成两个文件

  2. 在项目名UITests.swift 中添加测试代码 想要截图就要选择截取的位置


func testLoginToLogOut() {

  // Use recording to get started writing UI tests.

  // Use XCTAssert and related functions to verify your tests produce the correct results.

 let ex = expectation(description: "等待启动完成")

 ex.fulfill()

 waitForExpectations(timeout: 2) { (error) in

 snapshot("0SignIn")

 }

 let textField = app.textFields["请输入手机号码"]

 textField.tap()

 textField.typeText("15311569523")

 let secureTextField = app.secureTextFields["输入密码"]

 secureTextField.tap()

 secureTextField.typeText("wwwwww")

 app.buttons["登  录"].tap()

 let ex1 = expectation(description: "首页显示")

 ex1.fulfill()

 waitForExpectations(timeout: 2) { (error) in

 snapshot("1SignIn")

 }

 tabBarsQuery.children(matching: .other).element(boundBy: 1).tap()

 let ex2 = expectation(description: "通讯录")

 ex2.fulfill()

 waitForExpectations(timeout: 3) { (error) in

 snapshot("2SignIn")

 }

 app.buttons["xy address add"].tap()

 let xyCommonBlackBackArrowButton = app.buttons["xy common black back arrow"]

 xyCommonBlackBackArrowButton.tap()

  // 查询当前tab的所有cell 选择第一个cell 点击

 app.tables.element.cells.allElementsBoundByIndex[0].tap()

 xyCommonBlackBackArrowButton.tap()

 app.tabBars.children(matching: .other).element(boundBy: 1).tap()

 app.tables.cells.containing(.staticText, identifier:"我的暴风设备").buttons["xy address call"].tap()

 app.buttons["xy videocall hangup"].tap()

 let ex3 = expectation(description: "拨打视频通话")

 ex3.fulfill()

 waitForExpectations(timeout: 2) { (error) in

 snapshot("3SignIn")

 }

 app.tabBars.children(matching: .other).element(boundBy: 2).tap()

 app.buttons["编辑"].tap()

 xyCommonBlackBackArrowButton.tap()

 let ex4 = expectation(description: "个人")

 ex4.fulfill()

 waitForExpectations(timeout: 2) { (error) in

 snapshot("4SignIn")

 }

 let tablesQuery2 = app.tables

 let tablesQuery = tablesQuery2

 tablesQuery/*@START_MENU_TOKEN@*/.staticTexts["修改密码"]/*[[".cells.staticTexts[\"修改密码\"]",".staticTexts[\"修改密码\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

 xyCommonBlackBackArrowButton.tap()

 tablesQuery/*@START_MENU_TOKEN@*/.staticTexts["使用帮助"]/*[[".cells.staticTexts[\"使用帮助\"]",".staticTexts[\"使用帮助\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

 xyCommonBlackBackArrowButton.tap()

 tablesQuery/*@START_MENU_TOKEN@*/.staticTexts["关于"]/*[[".cells.staticTexts[\"关于\"]",".staticTexts[\"关于\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

 xyCommonBlackBackArrowButton.tap()

 tablesQuery/*@START_MENU_TOKEN@*/.staticTexts["联系我们"]/*[[".cells.staticTexts[\"联系我们\"]",".staticTexts[\"联系我们\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()

 xyCommonBlackBackArrowButton.tap()

 tablesQuery2.buttons["退出登录"].tap()

 }

snapshot("0SignIn") 这个方法你可能吊用不了 是因为你还没有安装 snapshot

### 配置 snapshot


fastlane snapshot init

执行完成会在Fastlane 文件下生成 Snapfile 文件打开如下 还会有一个 SnapshotHelper.swift 文件

SnapshotHelper.swift 这个文件需要添加到项目中以便下面使用

屏幕快照 2018-03-06 下午6.57.49

# Uncomment the lines below you want to change by removing the # in the beginning

# A list of devices you want to take the screenshots from

devices([

  "iPhone 8 Plus",

  "iPhone X"

])

languages([

  "en-US"

  # "it-IT",

  # ["pt", "pt_BR"] # Portuguese with Brazilian locale

])

# The name of the scheme which contains the UI Tests

# scheme("SchemeName")

# Where should the resulting screenshots be stored?

# output_directory("./screenshots")

# remove the '#' to clear all previously generated screenshots before creating new ones

# clear_previous_screenshots(true)

# Arguments to pass to the app on launch. See https://docs.fastlane.tools/actions/snapshot/#launch-arguments

# launch_arguments(["-favColor red"])

# For more information about all available options run

# fastlane action snapshot

成功之后需要在你项目中 xxxUITests.swift 文件方法 setUp 中添加


let app = XCUIApplication()

setupSnapshot(app)

现在你可以在需要截屏的位置使用 snapshot("截屏图片的名字")

现在打开你的终端在项目的根目录下执行

### 执行截屏操作得到结果


fastlane snapshot

完成后会在Fastlane 目录下生成一个文件名为 "screenshots"里面就是你截图的文件 如图 html文件 会自动打开
屏幕快照 2018-03-06 下午6.55.07
屏幕快照 2018-03-06 下午6.55.24

使用 fastlane action snapshot 查看更详细的操作功能

## 为截屏的图片加上手机框****使用 frameit

使用 frameit 需要安装依赖库 ImageMagick


brew install libpng jpeg imagemagick

如果你已经安装了imagemagick,但看到如下错误消息:


mogrify: no decode delegate for this image format `PNG'

您可能需要重新安装并从源代码构建。运行:


brew uninstall imagemagick; brew install libpng jpeg; brew install imagemagick --build-from-source

更新frameit fastlane frameit setup

屏幕快照 2018-03-07 上午10.15.27

### 使用

在截屏根目录使用执行 frameit 会得到一组套黑色手机边框的图片,可以使用 frameit silver 得到白色边框的图片


frameit

支持的手机框颜色


white

silver

rose_gold

gold

// 查询更多用法

fastlane action frameit 

## 为截图自定义标题和背景

官方文档

屏幕快照 2018-03-07 下午3.32.37

### 第一步****创建 Framefile.json 文件

在 screenshots文件目录下 创建 Framefile.json,cd 到终端 touch Framefile.json


{

  "default": {

  "keyword": {

 "fonts": [

 {

  "font": "./fonts/Sauce Code Powerline Light.otf",

  "supported": ["en-US"]

 },

 {

  "font": "./fonts/Sauce Code Powerline Light.otf",

  "supported": ["zh-Hans"]

 }

 ]

 },

 "title": {

 "fonts": [

 {

  "font": "./fonts/Sauce Code Powerline Light.otf",

  "supported": ["en-US"]

 },

 {

  "font": "./fonts/Sauce Code Powerline Light.otf",

  "supported": ["zcmn-Hans"]

 },

 {

  "font": "./fonts/Sauce Code Powerline Light.otf",

  "supported": ["ja-JP"]

 }

 ],

  "color": "#545454"

 },

  # 自定义背景图片

  "background": "./background.jpeg",

  # 是否完全显示手机框、false 为底部会被隐藏部分视图

  "show_complete_frame": false,

  # 关键字是否在标题上面

  "stack_title" : true,

  "padding": 50,

  # 标题在屏幕下方还是上方 false 为上方

  "title_below_image": false,

 },

  "data": [

 {

  "filter": "iPhone 8 Plus-0Snapshot",

 "keyword": {

  "color": "#d21559"

 }

 },

 {

  "filter": "iPhone 8 Plus-1Snapshot",

 "keyword": {

  "color": "#feb909"

 }

 },

 {

  "filter": "iPhone 8-0Snapshot",

 "keyword": {

  "color": "#feb909"

 }

 },

 {

  "filter": "iPhone 8-1Snapshot",

 "keyword": {

  "color": "#feb909"

 }

 },

 {

  "filter": "iPhone X-0Snapshot",

 "keyword": {

  "color": "#feb909"

 }

 },

 {

  "filter": "iPhone X-1Snapshot",

 "keyword": {

  "color": "#feb909"

 }

 }

 ]

}

### 创建 keyword.strings title.strings 文件

keyword 关键字 title 标题文件

要使用自定义标题和关键字的前提是需要 将两个 .strings文件放入 语言文件夹中 示例项目

报错提示这两个文件有问题去 这里查看

文件编码必须为 utf-16 编码

屏幕快照 2018-03-07 下午4.14.42
屏幕快照 2018-03-07 下午4.14.51

配置工作已经完成

### 运行 fastlane frameit 这里是在****en-US 目录下执行的****在根目录执行还睡将生成的图片添加到项目的****Assets

可以指定颜色的手机框,执行结果如图

屏幕快照 2018-03-07 下午4.20.17

完整结构目录:

屏幕快照 2018-03-07 下午4.22.12

到这里已经完成从 自动截图 -- 自动添加手机边框 -- 自定义背景 --- 自定义标题、关键字

参考文档

fastlane 官方文档

Screenshots 文档

frameit 文档

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容