Video Recording of Webdriver session with TestContainers - selenium

I am looking for the good example about how to record a webdriver session inside docker via testcontainers. Here is my solution:
lateinit var docker: BrowserWebDriverContainer<Nothing>
fun main() {
//1. Create webdriver and start Docker
val webDriver = startDocker()
//2. Open some page
webDriver.get("https://www.github.com")
//3. Stop the webdriver
webDriver.quit()
//4. Notify docker to save video
docker.afterTest(TestDi(), Optional.of(Throwable("")))
//5. Stop docker container
docker.stop()
}
fun create(): WebDriver {
WebDriverManager.getInstance(DriverManagerType.CHROME).driverVersion("latest").setup()
return ChromeDriver(getCapabilities())
}
private class RecordingFileFactoryImpl: RecordingFileFactory {
override fun recordingFileForTest(vncRecordingDirectory: File?, prefix: String?, succeeded: Boolean) =
Paths.get("video.mp4").toFile()
}
private class TestDi: TestDescription {
override fun getTestId() = ""
override fun getFilesystemFriendlyName() = "/build/"
}
private fun startDocker() : WebDriver {
docker = BrowserWebDriverContainer<Nothing>()
.withCapabilities(getCapabilities())
docker.withRecordingMode(
BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL,
File("build"),
VncRecordingContainer.VncRecordingFormat.MP4
)
docker.withRecordingFileFactory(
RecordingFileFactoryImpl()
)
docker.start()
return docker.webDriver
}
private fun getCapabilities(): DesiredCapabilities {
val caps = DesiredCapabilities.chrome()
val options = ChromeOptions()
options.addArguments(
"--allow-insecure-localhost",
"--safebrowsing-disable-extension-blacklist",
"--safebrowsing-disable-download-protection",
)
caps.setCapability(ChromeOptions.CAPABILITY, options)
caps.setCapability("acceptInsecureCerts", true)
val chromePrefs = HashMap<String, Any>()
chromePrefs["profile.default_content_settings.popups"] = 0
chromePrefs["download.default_directory"] = "build"
chromePrefs["safebrowsing.enabled"] = "true"
options.setExperimentalOption("prefs", chromePrefs)
return caps
}
https://github.com/nachg/DockerDemo
And it works. If you run it you will get an video.mp4 inside project dir. But I am expecting that the video will be completed after the step #3. Actually the video is completing after step #4, and it contains some seconds of the blank screen. I don't want them.

I resolved this issue by myself. Soltion was - to copy the video stream file and convert the copy. Please see the details in my last commit: https://github.com/nachg/DockerDemo/commit/9664eb818fc95979c681b6ce806b92e7e647d413

Related

Why compose ui testing's IdlingResource is blocking the main thread?

I've written a "minimal" AS project to replicate my the problem I'm facing. Here's the gh link.
I'm trying to write an end-to-end ui test in my compose-only project. The test covers a simple sign-in -> sync data -> go to main view use case.
Here's the whole test:
#HiltAndroidTest
class ExampleInstrumentedTest {
#get:Rule(order = 1)
val hiltRule = HiltAndroidRule(this)
#get:Rule(order = 2)
val composeTestRule = createAndroidComposeRule<MainActivity>()
#Inject
lateinit var dao: DummyDao
val isSyncing = mutableStateOf(false)
#Before
fun setup() {
runBlocking {
hiltRule.inject()
dao.deleteAllData()
dao.deleteUser()
}
composeTestRule.activity.isSyncingCallback = {
synchronized(isSyncing) {
isSyncing.value = it
}
}
composeTestRule.registerIdlingResource(
object : IdlingResource {
override val isIdleNow: Boolean
get() {
synchronized(isSyncing) {
return !isSyncing.value
}
}
}
)
}
#Test
fun runsTheStuffAndItWorks() {
composeTestRule
.onNodeWithText("login", ignoreCase = true, useUnmergedTree = true)
.assertIsDisplayed()
.performClick()
composeTestRule
.onNodeWithTag("sync")
.assertExists()
composeTestRule.waitForIdle()
assertFalse(isSyncing.value)
composeTestRule.onRoot().printToLog("not in the list")
composeTestRule
.onNodeWithTag("the list", useUnmergedTree = true)
.assertIsDisplayed()
}
}
The test runs "alright" up to the point where it should be waiting for the sync worker to finish its job and finally navigate to the "main composable".
Unfortunately, the test seems to be blocking the device's ui thread when the idling resource is not idle, finishing the test immediately as the idling resource does become idle.
I've tried using Espresso's IdlingResource directly, which also didn't work, showing similar results. I've tried adding compose's IdlingResource in different points as well, but that also didn't work (adding one between navigation calls also blocks the UI thread and the test fails even sooner).
What am I doing wrong here? Am I forgetting to setup something?

Why are these log statements not printing?

I'm building an object detection application (in Kotlin, for Android). The application uses CameraX to build a camera preview and Google ML to provide machine learning expertise. Just for reference; I used this CameraX documentation and this this Google ML Kit documentation.
I'm currently attempting to print Log.d("TAG", "onSuccess" + it.size) to my IDE console in order to determine if .addonSuccessListener is actually running. If it does, it should print something along the lines of onSuccess1. However, this isn't the case. Infact, it isn't even printing the Log statement from the .addOnFailureListener either, which really confuses me as I'm not even entirely sure the objectDetector code is even running. What really puzzles me is that I have relatively completed the same project in Java and have not faced this issue.
I did have someone point out that within my YourImageAnalyzer.kt class, if mediaImage is null, then I won't see anything logging. However, upon my own debugging (this is actually my very first time debugging), I was unable to find out if my first sentence of this paragraph is true or not. I suppose this issue may provide a lead into how I'll resolve this issue, and also learn how to properly debug.
Here is my YourImageAnalyzer.kt class, and I will also add the code for my MainActivity.kt class below as well.
YourImageAnalyzer.kt
private class YourImageAnalyzer : ImageAnalysis.Analyzer {
override fun analyze(imageProxy: ImageProxy) {
val mediaImage = imageProxy.image
if (mediaImage != null) {
val image =
InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
val localModel = LocalModel.Builder()
.setAssetFilePath("mobilenet_v1_0.75_192_quantized_1_metadata_1.tflite")
.build()
val customObjectDetectorOptions =
CustomObjectDetectorOptions.Builder(localModel)
.setDetectorMode(CustomObjectDetectorOptions.STREAM_MODE)
.enableClassification()
.setClassificationConfidenceThreshold(0.5f)
.setMaxPerObjectLabelCount(3)
.build()
val objectDetector =
ObjectDetection.getClient(customObjectDetectorOptions)
objectDetector //Here is where the issue stems, with the following listeners
.process(image)
.addOnSuccessListener {
Log.i("TAG", "onSuccess" + it.size)
for (detectedObjects in it)
{
val boundingBox = detectedObjects.boundingBox
val trackingId = detectedObjects.trackingId
for (label in detectedObjects.labels) {
val text = label.text
val index = label.index
val confidence = label.confidence
}
}
}
.addOnFailureListener { e -> Log.e("TAG", e.getLocalizedMessage()) }
.addOnCompleteListener { it -> imageProxy.close() }
}
}
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider>
override fun onCreate(savedInstanceState: Bundle?) {
cameraProviderFuture = ProcessCameraProvider.getInstance(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraProviderFuture.addListener(Runnable {
val cameraProvider = cameraProviderFuture.get()
bindPreview(cameraProvider)
}, ContextCompat.getMainExecutor(this))
}
fun bindPreview(cameraProvider: ProcessCameraProvider) {
val previewView = findViewById<PreviewView>(R.id.previewView)
var preview : Preview = Preview.Builder()
.build()
var cameraSelector : CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
preview.setSurfaceProvider(previewView.surfaceProvider)
var camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)
}
}
You are not binding your ImageAnalysis use case. Something in the line of:
val imageAnalysis = ImageAnalysis.Builder()
.setTargetResolution(Size(1280, 720))
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888)
.build()
and then;
imageAnalysis.setAnalyzer(executor, YourImageAnalyzer())
cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, imageAnalysis, preview)
Also a suggestion as a bonus:
You should get your LocalModel.Builder() out of analyze as this is called each time an image arrives. You do not need to execute this code piece each time as it will make your analysis slower.
So move this code:
val localModel = LocalModel.Builder()
.setAssetFilePath("mobilenet_v1_0.75_192_quantized_1_metadata_1.tflite")
.build()
to just below of the class private class YourImageAnalyzer : ImageAnalysis.Analyzer {.

CUBA Platform push messages from backend to UI

i was wondering if it is possible to send messages from the backend (for example a running task that receives information from an external system) to the UI. In my case it needs to be a specific session (no broadcast) and only on a specific screen
plan B would be polling the backend frequently but i was hoping to get something more "realtime"
I was trying to work something out like this, but i keep getting a NotSerializableException.
#Push
class StorageAccess : Screen(), MessageListener {
#Inject
private lateinit var stationWSService: StationWebSocketService
#Inject
private lateinit var notifications: Notifications
#Subscribe
private fun onInit(event: InitEvent) {
}
#Subscribe("stationPicker")
private fun onStationPickerValueChange(event: HasValue.ValueChangeEvent<StorageUnit>) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
val prevValue = event.prevValue
if (prevValue != null) {
stationWSService.remove(current.userSession.id)
}
val value = event.value ?: return
stationWSService.listen(current.userSession.id, value, this)
}
override fun messageReceived(message: String) {
val current = AppUI.getCurrent()
current.access {
notifications.create().withCaption(message).show()
}
}
#Subscribe
private fun onAfterDetach(event: AfterDetachEvent) {
val current = AppUI.getCurrent()
current.userSession.id ?: return
stationWSService.remove(current.userSession.id)
}
}
-- The callback interface
interface MessageListener : Serializable {
fun messageReceived(message: String);
}
-- The listen method of my backend service
private val listeners: MutableMap<String, MutableMap<UUID, MessageListener>> = HashMap()
override fun listen(id: UUID, storageUnit: StorageUnit, callback: MessageListener) {
val unitStationIP: String = storageUnit.unitStationIP ?: return
if (!listeners.containsKey(unitStationIP))
listeners[unitStationIP] = HashMap()
listeners[unitStationIP]?.set(id, callback)
}
The Exception i get is NotSerializableException: com.haulmont.cuba.web.sys.WebNotifications which happens during adding the listener to the backend: stationWSService.listen(current.userSession.id, value, this)
as far as i understand this is the place where the UI sends the information to the backend - and with it the entire status of the class StorageAccess, including all its members.
is there an elegant solution to this?
regards
There is an add-on that solves exactly this problem: https://github.com/cuba-platform/global-events-addon

Why Kotlin's loop (for) not work correct with org.slf4j.Logger?

In my Kotlin project in folder src/resources/ I has file pairs_ids.txt.
Here code:
This is a property file:
key=value
The count of all lines are 1389.
Here code that read content of this file line by line.
import org.slf4j.LoggerFactory
private val logger = LoggerFactory.getLogger("Exchange")
private var allSymbolsIDsMap: Map<String, String> = mapOf()
val pairsIDs = getResourceAsText("/pairs_ids.txt")
allSymbolsIDsMap = pairsIDs.split(",").associate {
val (left, right) = it.split("=")
left to right.toString()
}
logger.info("allSymbolsIDsMap_size = " + allSymbolsIDsMap.size)
var countAllSymbolHandler = 0
for ((key, value) in allSymbolsIDsMap) {
countAllSymbolHandler++
logger.info("countAllSymbolHandler = $countAllSymbolHandler")
}
private fun getResourceAsText(path: String): String {
return object {}.javaClass.getResourceAsStream(path).bufferedReader().use { it.readText() }
}
Result:
Start project:
allSymbolsIDsMap_size = 1389
Start project - "countAllSymbolHandler =" print 1113 times
Again start project:
allSymbolsIDsMap_size = 1389
"countAllSymbolHandler =" print 242 times
If replace logger.info by simple println then success work. The count is always 1389.
Why loop (for) not work correct with logger?
Try:
override fun run(configuration: AppConfig?, environment: Environment?) {
val logger = LoggerFactory.getLogger(this::class.java)
javaClass.getResourceAsStream("/pairs_ids.txt").bufferedReader().use { reader -> reader.readLines() }.forEach { line -> logger.info(line) }
}
When playing with input/output streams, use Kotlin's use extension methods, and do all of your processing inside of the use block.
This will handle all opening and closing of the streams so that there are no leaks, or forgetting to close/flush etc.

Selenium multiple tabs at once

I'm working with Selenium, and am wondering if it's possible to use multiple TABS at once? I do not want to use multiple browser instances (i.e., 2 copies of IE pun). IF IT IS NOT possible, how would one go about switching between individual tabs, that are running sequentially?
Thanks!
If there is a link that opens up a new window/tab, then you can use driver.switchTo().window();
However, if you want to run something on multiple windows, then I recommend having multiple instances of webdriver. It is much easier to manage, and is supported (There are workarounds on opening a new tab/window, such as pressing a hotkey that opens a new window, but they aren't supported).
If you are wanting to have multiple threads all act on the same driver instance, but different tabs, that is NOT possible.
It is possible to switch between individual tabs without having multiple browser instances.
There is a difference how web driver handles different windows and how it handles different tabs.
Case 1:
In case there are multiple windows, then the following code can help:
//Get the current window handle
String windowHandle = driver.getWindowHandle();
//Get the list of window handles
ArrayList tabs = new ArrayList (driver.getWindowHandles());
System.out.println(tabs.size());
//Use the list of window handles to switch between windows
driver.switchTo().window(tabs.get(0));
//Switch back to original window
driver.switchTo().window(mainWindowHandle);
Case 2:
In case there are multiple tabs in the same window, then there is only one window handle. Hence switching between window handles keeps the control in the same tab. In this case using Ctrl + \t (Ctrl + Tab) to switch between tabs is more useful.
//Open a new tab using Ctrl + t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Switch between tabs using Ctrl + \t
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
Detailed sample code can be found here:
http://design-interviews.blogspot.com/2014/11/switching-between-tabs-in-same-browser-window.html
To open multiple tabs:
driver = new ChromeDriver();
IJavaScriptExecutor jscript = driver as IJavaScriptExecutor;
for (int i = 0; i < 10; i++)
{
driver.Navigate().GoToUrl(this.baseURL);
jscript.ExecuteScript("window.open('{0}', '_blank');", this.baseURL);
}
Swich between them:
for (int i = 0; i < driver.WindowHandles.Count; i++)
{
driver.SwitchTo().Window(driver.WindowHandles[i])]);
}
Try with below code.
String oldTab = driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
I recently implemented a simple multi-thread utility that allows running tests on separate tabs on separate threads WITH JUST ONE WEBDRIVER INSTANCE. The problem with WebDriver is that it can focus only one tab (window) at a time. So, to do tests in multiple tabs, WebDriver must be focused separately on each of them. I'm sure my implementation is not perfect, but here it is (implementation in Kotlin):
Usage:
fun test() {
val results = ParallelNavigator(webDriver,
listOf(
::test1,
::test2,
::test3
)
).start()
println(results)
// Output: [Success, Failure: java.lang.RuntimeException: Some error, Success]
}
fun test1(pn: ParallelNavigator) {
/* ... open url, find elements etc. so stuff */
pn.resumeNext() // transfer flow to another unfinished thread (test2 if not finished)
/* ... do more stuff */
pn.resumeNext() // again transfer flow to another thread
}
fun test2(pn: ParallelNavigator) { /* ... */ }
fun test3(pn: ParallelNavigator) { /* ... */ }
Implementation:
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.support.ui.WebDriverWait
import java.util.concurrent.locks.Condition
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.thread
import kotlin.concurrent.withLock
class ParallelNavigator(private val webDriver: WebDriver, executions: List<(ParallelNavigator) -> Unit>) {
private val _executions: List<TabExecution> = executions.map { TabExecution(it) }
private var currentExecutionIndex: Int = -1
fun start(): List<Result> {
createTabs()
return runInternal()
}
fun resumeNext() {
if (_executions.isEmpty()) {
throw RuntimeException("No executions provided.")
}
val currentExecution: TabExecution? = if (currentExecutionIndex != -1) {
_executions[currentExecutionIndex]
} else null
val unfinished = _executions.filter { !it.finished }
if(unfinished.isEmpty()) {
return
}
val nextExecutionIndex = if (currentExecutionIndex >= unfinished.lastIndex || currentExecutionIndex <= -1) {
0
} else {
currentExecutionIndex + 1
}
val nextExecution = unfinished[nextExecutionIndex]
currentExecutionIndex = nextExecutionIndex
webDriver.switchTo().window(nextExecution.windowHandle)
nextExecution.lock.withLock {
nextExecution.condition.signal()
}
currentExecution?.lock?.withLock {
if (!currentExecution.finished) {
currentExecution.condition.await()
}
}
}
sealed class Result {
class Success : Result() {
override fun toString(): String {
return "Success"
}
}
class Failure(val ex: Throwable) : Result() {
override fun toString(): String {
return "Failure: ${ex.javaClass.name}: ${ex.message}"
}
}
class Unfinished : Result() {
override fun toString(): String {
return "Unfinished"
}
}
}
data class TabExecution(
val test: (ParallelNavigator) -> Unit,
val lock: ReentrantLock = ReentrantLock(),
var finished: Boolean = false
) {
lateinit var windowHandle: String
lateinit var condition: Condition
lateinit var thread: Thread
}
private fun createTabs() = with(webDriver) {
navigate().to("about:blank")
val homeWindowHandle = windowHandle
for (execution in _executions) {
execution.windowHandle = openNewTab()
}
webDriver.switchTo().window(homeWindowHandle)
}
private fun runInternal(): List<Result> {
val results = _executions.map { Result.Unfinished() as Result }.toMutableList()
for (index in _executions.indices) {
val execution = _executions[index]
val condition = execution.lock.newCondition()
execution.condition = condition
execution.thread = thread(start = false) {
execution.lock.withLock {
condition.await()
try {
execution.test(this)
results[index] = Result.Success()
} catch (ex: Throwable) {
ex.printStackTrace()
results[index] = Result.Failure(ex)
}
execution.finished = true
currentExecutionIndex--
resumeNext()
}
}
execution.thread.start()
}
resumeNext() // run first execution
for (execution in _executions) {
execution.thread.join()
}
return results
}
fun waitForNewTabToOpen(oldWindowHandles: Set<String>) = with(webDriver) {
waitForNewTabToOpen(oldWindowHandles, 10)
}
fun waitForNewTabToOpen(oldWindowHandles: Set<String>, seconds: Int) = with(webDriver) {
WebDriverWait(webDriver, seconds.toLong()).until<Boolean> { WebDriver -> availableWindowHandles().size > oldWindowHandles.size }
}
fun availableWindowHandles(): Set<String> = with(webDriver) {
return webDriver.getWindowHandles()
}
private fun getNewTabHandle(oldWindowHandles: Set<String>): String = with(webDriver) {
waitForNewTabToOpen(oldWindowHandles)
val newWindowHandles = availableWindowHandles().toMutableSet()
newWindowHandles.removeAll(oldWindowHandles)
return newWindowHandles.iterator().next()
}
fun openNewTab(): String = with(webDriver) {
val oldHandles = availableWindowHandles()
(webDriver as JavascriptExecutor).executeScript("Object.assign(document.createElement('a'), { target: '_blank', href: 'about:blank'}).click();")
waitForNewTabToOpen(oldHandles)
return getNewTabHandle(oldHandles)
}
}
This will solve your problem in case ur working with selenium and nodejs.
driver.get('https://www.google.com/')
.then(_ =>
driver.findElement(webdriver.By.tagName('body'))
)
.then(bodyElement => {
bodyElement.sendKeys(webdriver.Key.chord(webdriver.Key.CONTROL, 't'))
})
.catch(err => {
console.log(err);
})
if you are wishing to run the multıple wındows at the same time, use threading with multiple instances of IWebDriver
EX:
public void Work()
{
IWebDriver driver = new ChromeDriver("D:\\Drivers");
driver.Navigate().GoToUrl(URL);
\\Do the rest
}
public void Work2()
{
IWebDriver driver = new ChromeDriver("D:\\Drivers");
driver.Navigate().GoToUrl(URL2);
\\Do the rest
}
and call the function like this:
Thread thread1 = new Thread(new ThreadStart(Work));
thread1.Start();
Thread thread2 = new Thread(new ThreadStart(Work2));
thread2.Start();