{"action":"created","comment":{"url":"https://api.github.com/repos/appium/appium-android-driver/pulls/comments/123244936","pull_request_review_id":45428290,"id":123244936,"diff_hunk":"@@ -185,64 +183,61 @@ commands.sendSMS = async function (phoneNumber, message) {\n   await this.adb.sendSMS(phoneNumber, message);\n };\n \n-async function storeScreenshotDataWithAdbShell (adb, opts, localFile) {\n-  let pngDir = opts.androidScreenshotPath || '/data/local/tmp/';\n-  const png = path.posix.resolve(pngDir, 'screenshot.png');\n-  let cmd = ['/system/bin/rm', `${png};`, '/system/bin/screencap', '-p', png];\n-  await adb.shell(cmd);\n-  await adb.pull(png, localFile);\n+async function getScreenshotDataWithAdbShell (adb, opts) {\n+  const localFile = temp.path({prefix: 'appium', suffix: '.png'});\n+  if (await fs.exists(localFile)) {\n+    await fs.unlink(localFile);\n+  }\n+  try {\n+    const pngDir = opts.androidScreenshotPath || '/data/local/tmp/';\n+    const png = path.posix.resolve(pngDir, 'screenshot.png');\n+    const cmd = ['/system/bin/rm', `${png};`, '/system/bin/screencap', '-p', png];\n+    await adb.shell(cmd);\n+    await adb.pull(png, localFile);\n+    return await jimp.read(localFile);\n+  } finally {\n+    if (await fs.exists(localFile)) {\n+      await fs.unlink(localFile);\n+    }\n+  }\n }\n \n-async function storeScreenshotDataWithAdbExecOut (adb, localFile) {\n+async function getScreenshotDataWithAdbExecOut (adb) {\n   let {stdout} = await exec(adb.executable.path, ['exec-out', '/system/bin/screencap -p'],\n-                            {encoding: 'binary'});\n-  const signature = new Buffer(stdout, 'binary').toString('hex', 0, PNG_MAGIC_LENGTH);\n-  if (signature === PNG_MAGIC) {\n-    await fs.writeFile(localFile, stdout, 'binary');\n-  } else {\n-    throw new Error(`Command output is not a valid PNG file (${signature} !== ${PNG_MAGIC})`);\n-  }\n+                            {encoding: 'binary', isBuffer: true});\n+  return await jimp.read(stdout);\n }\n \n commands.getScreenshot = async function () {\n-  let localFile = temp.path({prefix: 'appium', suffix: '.png'});\n-  if (await fs.exists(localFile)) {\n-    await fs.unlink(localFile);\n-  }\n   const apiLevel = await this.adb.getApiLevel();\n+  let image = null;\n   if (apiLevel > 20) {\n     try {\n       // This screenshoting approach is way faster, since it requires less external commands\n       // to be executed. Unfortunately, exec-out option is only supported by newer Android/SDK versions (5.0 and later)\n-      await storeScreenshotDataWithAdbExecOut(this.adb, localFile);\n+      image = await getScreenshotDataWithAdbExecOut(this.adb);\n     } catch (e) {\n       log.info(`Cannot get screenshot data with 'adb exec-out' because of '${e.message}'. ` +\n                `Defaulting to 'adb shell' call`);\n     }\n   }\n-  if (!await fs.exists(localFile)) {\n-    await storeScreenshotDataWithAdbShell(this.adb, this.opts, localFile);\n-  }\n-  if (!await fs.exists(localFile)) {\n-    log.errorAndThrow(`The screenshot is expected to be present at ${localFile}`);\n+  if (!image) {\n+    try {\n+      image = await getScreenshotDataWithAdbShell(this.adb, this.opts);\n+    } catch (e) {\n+      log.errorAndThrow(`Cannot get screenshot data because of '${e.message}'`);\n+    }\n   }\n-  try {\n-    let image = await jimp.read(localFile);\n-    if (apiLevel < 23) {\n-      // Android bug 8433742 - rotate screenshot if screen is rotated\n-      let screenOrientation = await this.adb.getScreenOrientation();\n-      try {\n-        image = await image.rotate(-90 * screenOrientation);\n-      } catch (err) {\n-        log.warn(`Could not rotate screenshot due to error: ${err}`);\n-      }\n+  if (apiLevel < 23) {\n+    // Android bug 8433742 - rotate screenshot if screen is rotated\n+    let screenOrientation = await this.adb.getScreenOrientation();\n+    try {\n+      image = await image.rotate(-90 * screenOrientation);\n+    } catch (err) {\n+      log.warn(`Could not rotate screenshot due to error: ${err}`);\n     }\n-    let b64data = (await B.promisify(image.getBuffer).call(image, jimp.MIME_PNG))\n-                  .toString('base64');\n-    return b64data;\n-  } finally {\n-    await fs.unlink(localFile);\n   }\n+  return (await B.promisify(image.getBuffer).call(image, jimp.MIME_PNG)).toString('base64');","path":"lib/commands/actions.js","position":106,"original_position":106,"commit_id":"d36dff6616069497425e1a7be9323d29106a97f4","original_commit_id":"d36dff6616069497425e1a7be9323d29106a97f4","user":{"login":"jlipps","id":605053,"avatar_url":"https://avatars0.githubusercontent.com/u/605053?v=3","gravatar_id":"","url":"https://api.github.com/users/jlipps","html_url":"https://github.com/jlipps","followers_url":"https://api.github.com/users/jlipps/followers","following_url":"https://api.github.com/users/jlipps/following{/other_user}","gists_url":"https://api.github.com/users/jlipps/gists{/gist_id}","starred_url":"https://api.github.com/users/jlipps/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlipps/subscriptions","organizations_url":"https://api.github.com/users/jlipps/orgs","repos_url":"https://api.github.com/users/jlipps/repos","events_url":"https://api.github.com/users/jlipps/events{/privacy}","received_events_url":"https://api.github.com/users/jlipps/received_events","type":"User","site_admin":false},"body":"while nice and compact, this is a bit less readable than:\r\n\r\n```js\r\nconst getBuffer = B.promisify(image.getBuffer, {context: image});\r\nconst imgBuffer = await getBuffer(jimp.MIME_PNG);\r\nreturn imgBuffer.toString('base64');\r\n```","created_at":"2017-06-21T13:18:17Z","updated_at":"2017-06-21T13:18:32Z","html_url":"https://github.com/appium/appium-android-driver/pull/247#discussion_r123244936","pull_request_url":"https://api.github.com/repos/appium/appium-android-driver/pulls/247","_links":{"self":{"href":"https://api.github.com/repos/appium/appium-android-driver/pulls/comments/123244936"},"html":{"href":"https://github.com/appium/appium-android-driver/pull/247#discussion_r123244936"},"pull_request":{"href":"https://api.github.com/repos/appium/appium-android-driver/pulls/247"}}},"pull_request":{"url":"https://api.github.com/repos/appium/appium-android-driver/pulls/247","id":126357436,"html_url":"https://github.com/appium/appium-android-driver/pull/247","diff_url":"https://github.com/appium/appium-android-driver/pull/247.diff","patch_url":"https://github.com/appium/appium-android-driver/pull/247.patch","issue_url":"https://api.github.com/repos/appium/appium-android-driver/issues/247","number":247,"state":"open","locked":false,"title":"Fix screenshoting with exec-out","user":{"login":"mykola-mokhnach","id":7767781,"avatar_url":"https://avatars2.githubusercontent.com/u/7767781?v=3","gravatar_id":"","url":"https://api.github.com/users/mykola-mokhnach","html_url":"https://github.com/mykola-mokhnach","followers_url":"https://api.github.com/users/mykola-mokhnach/followers","following_url":"https://api.github.com/users/mykola-mokhnach/following{/other_user}","gists_url":"https://api.github.com/users/mykola-mokhnach/gists{/gist_id}","starred_url":"https://api.github.com/users/mykola-mokhnach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mykola-mokhnach/subscriptions","organizations_url":"https://api.github.com/users/mykola-mokhnach/orgs","repos_url":"https://api.github.com/users/mykola-mokhnach/repos","events_url":"https://api.github.com/users/mykola-mokhnach/events{/privacy}","received_events_url":"https://api.github.com/users/mykola-mokhnach/received_events","type":"User","site_admin":false},"body":"Trying to address the issue visible in https://gist.github.com/ajaj91/a457b8b378481f9ddee39434f3343785:\r\n\r\n> [AndroidDriver] Cannot get screenshot data with 'adb exec-out' because of 'Command output is not a valid PNG file (fd504e47 !== 89504e47)'. Defaulting to 'adb shell' call\r\n","created_at":"2017-06-19T17:35:26Z","updated_at":"2017-06-21T13:18:32Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ae1bb3fcfbe8b773f339384c9a76843777e93bbf","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/appium/appium-android-driver/pulls/247/commits","review_comments_url":"https://api.github.com/repos/appium/appium-android-driver/pulls/247/comments","review_comment_url":"https://api.github.com/repos/appium/appium-android-driver/pulls/comments{/number}","comments_url":"https://api.github.com/repos/appium/appium-android-driver/issues/247/comments","statuses_url":"https://api.github.com/repos/appium/appium-android-driver/statuses/d36dff6616069497425e1a7be9323d29106a97f4","head":{"label":"mykola-mokhnach:fix_adb_exec_screen","ref":"fix_adb_exec_screen","sha":"d36dff6616069497425e1a7be9323d29106a97f4","user":{"login":"mykola-mokhnach","id":7767781,"avatar_url":"https://avatars2.githubusercontent.com/u/7767781?v=3","gravatar_id":"","url":"https://api.github.com/users/mykola-mokhnach","html_url":"https://github.com/mykola-mokhnach","followers_url":"https://api.github.com/users/mykola-mokhnach/followers","following_url":"https://api.github.com/users/mykola-mokhnach/following{/other_user}","gists_url":"https://api.github.com/users/mykola-mokhnach/gists{/gist_id}","starred_url":"https://api.github.com/users/mykola-mokhnach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mykola-mokhnach/subscriptions","organizations_url":"https://api.github.com/users/mykola-mokhnach/orgs","repos_url":"https://api.github.com/users/mykola-mokhnach/repos","events_url":"https://api.github.com/users/mykola-mokhnach/events{/privacy}","received_events_url":"https://api.github.com/users/mykola-mokhnach/received_events","type":"User","site_admin":false},"repo":{"id":84955763,"name":"appium-android-driver","full_name":"mykola-mokhnach/appium-android-driver","owner":{"login":"mykola-mokhnach","id":7767781,"avatar_url":"https://avatars2.githubusercontent.com/u/7767781?v=3","gravatar_id":"","url":"https://api.github.com/users/mykola-mokhnach","html_url":"https://github.com/mykola-mokhnach","followers_url":"https://api.github.com/users/mykola-mokhnach/followers","following_url":"https://api.github.com/users/mykola-mokhnach/following{/other_user}","gists_url":"https://api.github.com/users/mykola-mokhnach/gists{/gist_id}","starred_url":"https://api.github.com/users/mykola-mokhnach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mykola-mokhnach/subscriptions","organizations_url":"https://api.github.com/users/mykola-mokhnach/orgs","repos_url":"https://api.github.com/users/mykola-mokhnach/repos","events_url":"https://api.github.com/users/mykola-mokhnach/events{/privacy}","received_events_url":"https://api.github.com/users/mykola-mokhnach/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mykola-mokhnach/appium-android-driver","description":"Android Driver for Appium","fork":true,"url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver","forks_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/forks","keys_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/teams","hooks_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/hooks","issue_events_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/issues/events{/number}","events_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/events","assignees_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/assignees{/user}","branches_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/branches{/branch}","tags_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/tags","blobs_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/languages","stargazers_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/stargazers","contributors_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/contributors","subscribers_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/subscribers","subscription_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/subscription","commits_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/contents/{+path}","compare_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/merges","archive_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/downloads","issues_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/issues{/number}","pulls_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/labels{/name}","releases_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/releases{/id}","deployments_url":"https://api.github.com/repos/mykola-mokhnach/appium-android-driver/deployments","created_at":"2017-03-14T14:02:17Z","updated_at":"2017-03-14T14:02:20Z","pushed_at":"2017-06-21T05:58:44Z","git_url":"git://github.com/mykola-mokhnach/appium-android-driver.git","ssh_url":"git@github.com:mykola-mokhnach/appium-android-driver.git","clone_url":"https://github.com/mykola-mokhnach/appium-android-driver.git","svn_url":"https://github.com/mykola-mokhnach/appium-android-driver","homepage":null,"size":14072,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"appium:master","ref":"master","sha":"d11a0c4d59cc65dffc70cb6fe8e33a554085f453","user":{"login":"appium","id":3221291,"avatar_url":"https://avatars1.githubusercontent.com/u/3221291?v=3","gravatar_id":"","url":"https://api.github.com/users/appium","html_url":"https://github.com/appium","followers_url":"https://api.github.com/users/appium/followers","following_url":"https://api.github.com/users/appium/following{/other_user}","gists_url":"https://api.github.com/users/appium/gists{/gist_id}","starred_url":"https://api.github.com/users/appium/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/appium/subscriptions","organizations_url":"https://api.github.com/users/appium/orgs","repos_url":"https://api.github.com/users/appium/repos","events_url":"https://api.github.com/users/appium/events{/privacy}","received_events_url":"https://api.github.com/users/appium/received_events","type":"Organization","site_admin":false},"repo":{"id":39916403,"name":"appium-android-driver","full_name":"appium/appium-android-driver","owner":{"login":"appium","id":3221291,"avatar_url":"https://avatars1.githubusercontent.com/u/3221291?v=3","gravatar_id":"","url":"https://api.github.com/users/appium","html_url":"https://github.com/appium","followers_url":"https://api.github.com/users/appium/followers","following_url":"https://api.github.com/users/appium/following{/other_user}","gists_url":"https://api.github.com/users/appium/gists{/gist_id}","starred_url":"https://api.github.com/users/appium/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/appium/subscriptions","organizations_url":"https://api.github.com/users/appium/orgs","repos_url":"https://api.github.com/users/appium/repos","events_url":"https://api.github.com/users/appium/events{/privacy}","received_events_url":"https://api.github.com/users/appium/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/appium/appium-android-driver","description":"Android Driver for Appium","fork":false,"url":"https://api.github.com/repos/appium/appium-android-driver","forks_url":"https://api.github.com/repos/appium/appium-android-driver/forks","keys_url":"https://api.github.com/repos/appium/appium-android-driver/keys{/key_id}","collaborators_url":"https://api.github.com/repos/appium/appium-android-driver/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/appium/appium-android-driver/teams","hooks_url":"https://api.github.com/repos/appium/appium-android-driver/hooks","issue_events_url":"https://api.github.com/repos/appium/appium-android-driver/issues/events{/number}","events_url":"https://api.github.com/repos/appium/appium-android-driver/events","assignees_url":"https://api.github.com/repos/appium/appium-android-driver/assignees{/user}","branches_url":"https://api.github.com/repos/appium/appium-android-driver/branches{/branch}","tags_url":"https://api.github.com/repos/appium/appium-android-driver/tags","blobs_url":"https://api.github.com/repos/appium/appium-android-driver/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/appium/appium-android-driver/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/appium/appium-android-driver/git/refs{/sha}","trees_url":"https://api.github.com/repos/appium/appium-android-driver/git/trees{/sha}","statuses_url":"https://api.github.com/repos/appium/appium-android-driver/statuses/{sha}","languages_url":"https://api.github.com/repos/appium/appium-android-driver/languages","stargazers_url":"https://api.github.com/repos/appium/appium-android-driver/stargazers","contributors_url":"https://api.github.com/repos/appium/appium-android-driver/contributors","subscribers_url":"https://api.github.com/repos/appium/appium-android-driver/subscribers","subscription_url":"https://api.github.com/repos/appium/appium-android-driver/subscription","commits_url":"https://api.github.com/repos/appium/appium-android-driver/commits{/sha}","git_commits_url":"https://api.github.com/repos/appium/appium-android-driver/git/commits{/sha}","comments_url":"https://api.github.com/repos/appium/appium-android-driver/comments{/number}","issue_comment_url":"https://api.github.com/repos/appium/appium-android-driver/issues/comments{/number}","contents_url":"https://api.github.com/repos/appium/appium-android-driver/contents/{+path}","compare_url":"https://api.github.com/repos/appium/appium-android-driver/compare/{base}...{head}","merges_url":"https://api.github.com/repos/appium/appium-android-driver/merges","archive_url":"https://api.github.com/repos/appium/appium-android-driver/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/appium/appium-android-driver/downloads","issues_url":"https://api.github.com/repos/appium/appium-android-driver/issues{/number}","pulls_url":"https://api.github.com/repos/appium/appium-android-driver/pulls{/number}","milestones_url":"https://api.github.com/repos/appium/appium-android-driver/milestones{/number}","notifications_url":"https://api.github.com/repos/appium/appium-android-driver/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/appium/appium-android-driver/labels{/name}","releases_url":"https://api.github.com/repos/appium/appium-android-driver/releases{/id}","deployments_url":"https://api.github.com/repos/appium/appium-android-driver/deployments","created_at":"2015-07-29T21:07:42Z","updated_at":"2017-06-20T09:42:33Z","pushed_at":"2017-06-21T10:17:20Z","git_url":"git://github.com/appium/appium-android-driver.git","ssh_url":"git@github.com:appium/appium-android-driver.git","clone_url":"https://github.com/appium/appium-android-driver.git","svn_url":"https://github.com/appium/appium-android-driver","homepage":null,"size":14058,"stargazers_count":42,"watchers_count":42,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":71,"mirror_url":null,"open_issues_count":4,"forks":71,"open_issues":4,"watchers":42,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/appium/appium-android-driver/pulls/247"},"html":{"href":"https://github.com/appium/appium-android-driver/pull/247"},"issue":{"href":"https://api.github.com/repos/appium/appium-android-driver/issues/247"},"comments":{"href":"https://api.github.com/repos/appium/appium-android-driver/issues/247/comments"},"review_comments":{"href":"https://api.github.com/repos/appium/appium-android-driver/pulls/247/comments"},"review_comment":{"href":"https://api.github.com/repos/appium/appium-android-driver/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/appium/appium-android-driver/pulls/247/commits"},"statuses":{"href":"https://api.github.com/repos/appium/appium-android-driver/statuses/d36dff6616069497425e1a7be9323d29106a97f4"}}}}
{"actor":{"display_login":"jlipps"}}