Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
jami-client-android
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
jami-client-android
Commits
ea379180
Commit
ea379180
authored
8 months ago
by
Pierre Nicolas
Browse files
Options
Downloads
Patches
Plain Diff
test: add screenshot feature
Change-Id: I694037c34df607e3add8d9383fbc6bf6ea4b0070
parent
aac4696f
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
ci/Jenkinsfile
+2
-0
2 additions, 0 deletions
ci/Jenkinsfile
ci/download_screenshots.sh
+23
-0
23 additions, 0 deletions
ci/download_screenshots.sh
jami-android/app/src/androidTest/java/cx/ring/NativeScreenshot.kt
+99
-0
99 additions, 0 deletions
...roid/app/src/androidTest/java/cx/ring/NativeScreenshot.kt
with
124 additions
and
0 deletions
ci/Jenkinsfile
+
2
−
0
View file @
ea379180
...
@@ -121,6 +121,8 @@ pipeline {
...
@@ -121,6 +121,8 @@ pipeline {
errorOccurred
=
true
errorOccurred
=
true
}
}
sh
'cd /jami-client-android/ci && ./download_screenshots.sh'
// Archive tests output save it as Jenkins artifact
// Archive tests output save it as Jenkins artifact
sh
'cd /jami-client-android/ci/spoon-output && zip -r ../ui-test-output.zip *'
sh
'cd /jami-client-android/ci/spoon-output && zip -r ../ui-test-output.zip *'
archiveArtifacts
artifacts:
'ci/ui-test-output.zip'
,
allowEmptyArchive:
false
archiveArtifacts
artifacts:
'ci/ui-test-output.zip'
,
allowEmptyArchive:
false
...
...
This diff is collapsed.
Click to expand it.
ci/download_screenshots.sh
0 → 100755
+
23
−
0
View file @
ea379180
#!/bin/bash
# Define the source directory on the Android device (emulator)
screenshotDir
=
"/sdcard/Download/screenshots"
# Define the destination directory on the local system
localDir
=
"/jami-client-android/ci/spoon-output/screenshots"
# Create the local directory if it doesn't exist
mkdir
-p
"
$localDir
"
# Download the content of the directory from the device to the local directory
adb pull
"
$screenshotDir
"
"
$localDir
"
# Check if the download was successful
if
[
$?
-eq
0
]
;
then
echo
"The content of directory '
$screenshotDir
' has been downloaded to '
$localDir
'."
else
echo
"Error downloading the directory."
fi
# Set ownership of the local directory and its contents to jenkins
chown
-R
jenkins:jenkins
"
$localDir
"
This diff is collapsed.
Click to expand it.
jami-android/app/src/androidTest/java/cx/ring/NativeScreenshot.kt
0 → 100644
+
99
−
0
View file @
ea379180
package
cx.ring
import
android.graphics.Bitmap
import
android.os.Build
import
android.os.Environment
import
androidx.test.runner.screenshot.BasicScreenCaptureProcessor
import
androidx.test.runner.screenshot.Screenshot
import
net.jami.utils.Log
import
java.io.File
import
java.io.IOException
import
java.util.regex.Pattern
object
NativeScreenshot
{
private
var
methodName
:
String
?
=
null
private
var
className
:
String
?
=
null
private
val
SCREENSHOT_NAME_VALIDATION
:
Pattern
=
Pattern
.
compile
(
"[a-zA-Z0-9_-]+"
)
/**
* Captures screenshot using Androidx Screenshot library and stores in the filesystem.
* Special Cases:
* If the screenshotName contains spaces or does not pass validation, the corresponding
* screenshot is not visible on BrowserStack's Dashboard.
* If there is any runtime exception while capturing screenshot, the method throws
* Exception and the test might fail if the exception is not handled properly.
* @param screenshotName a screenshot identifier
* @return path to the screenshot file
*/
fun
capture
(
screenshotName
:
String
):
String
{
Log
.
w
(
"NativeScreenshot"
,
"Capturing screenshot: $screenshotName"
)
val
testClass
=
findTestClassTraceElement
(
Thread
.
currentThread
().
stackTrace
)
className
=
testClass
.
className
.
replace
(
"[^A-Za-z0-9._-]"
.
toRegex
(),
"_"
)
methodName
=
testClass
.
methodName
val
screenCaptureProcessor
=
EspressoScreenCaptureProcessor
()
if
(!
SCREENSHOT_NAME_VALIDATION
.
matcher
(
screenshotName
).
matches
())
{
throw
IllegalArgumentException
(
"ScreenshotName must match ${SCREENSHOT_NAME_VALIDATION.pattern()}."
)
}
else
{
val
capture
=
Screenshot
.
capture
()
capture
.
format
=
Bitmap
.
CompressFormat
.
PNG
capture
.
name
=
screenshotName
try
{
return
screenCaptureProcessor
.
process
(
capture
)
}
catch
(
e
:
IOException
)
{
throw
RuntimeException
(
"Unable to capture screenshot."
,
e
)
}
}
}
/**
* Extracts the currently executing test's trace element based on the test runner
* or any framework being used.
* @param trace stacktrace of the currently running test
* @return StackTrace Element corresponding to the current test being executed.
*/
private
fun
findTestClassTraceElement
(
trace
:
Array
<
StackTraceElement
>):
StackTraceElement
{
for
(
i
in
trace
.
indices
.
reversed
())
{
val
element
=
trace
[
i
]
if
(
"android.test.InstrumentationTestCase"
==
element
.
className
&&
"runMethod"
==
element
.
methodName
)
{
return
extractStackElement
(
trace
,
i
)
}
if
(
"org.junit.runners.model.FrameworkMethod\$1"
==
element
.
className
&&
"runReflectiveCall"
==
element
.
methodName
)
{
return
extractStackElement
(
trace
,
i
)
}
if
(
"cucumber.runtime.model.CucumberFeature"
==
element
.
className
&&
"run"
==
element
.
methodName
)
{
return
extractStackElement
(
trace
,
i
)
}
}
throw
IllegalArgumentException
(
"Could not find test class!"
)
}
/**
* Based on the test runner or framework being used, extracts the exact traceElement.
* @param trace stacktrace of the currently running test
* @param i a reference index
* @return trace element based on the index passed
*/
private
fun
extractStackElement
(
trace
:
Array
<
StackTraceElement
>,
i
:
Int
):
StackTraceElement
{
val
testClassTraceIndex
=
if
(
Build
.
VERSION
.
SDK_INT
>=
23
)
i
-
2
else
i
-
3
return
trace
[
testClassTraceIndex
]
}
private
class
EspressoScreenCaptureProcessor
:
BasicScreenCaptureProcessor
()
{
companion
object
{
private
const
val
SCREENSHOT
=
"screenshots"
}
init
{
val
screenshotDir
=
File
(
Environment
.
getExternalStoragePublicDirectory
(
Environment
.
DIRECTORY_DOWNLOADS
).
toString
(),
SCREENSHOT
)
val
classDir
=
File
(
screenshotDir
,
className
)
Log
.
w
(
"NativeScreenshot"
,
"Screenshot directory: $classDir"
)
mDefaultScreenshotPath
=
File
(
classDir
,
methodName
)
}
/**
* Converts the filename to a standard path to be stored on device.
* Example: "post_addition" converts to "1648038895211_post_addition"
* which is later suffixed by the file extension i.e. png.
* @param filename a screenshot identifier
* @return custom filename format
*/
override
fun
getFilename
(
filename
:
String
):
String
{
return
"${System.currentTimeMillis()}_$filename"
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment