How to change the UI of an activity according to the commands received from a service? - android-service

I created a service which sends commands to an external activity through AIDL: after this activity bind to the service, it sends a callback to the service, and so a two-way communication is established and works correctly.
Instead, the problem arises when I want to change the UI of the application according to commands received from the service. I performed a test with a simple Toast notification, but some errors occur...
The onExecute method of the external activity is called when a new command is received through AIDL. If I remove the Toast notification, everything is working properly, but I need to change the UI according to commands received from the service.
protected void onExecute(Bundle params) {
Log.i(LOG_TAG, "onExecute(): " + params);
if (params.containsKey("command01")) {
Log.i(LOG_TAG, "onExecute(): " + params.getString("command01"));
Toast
.makeText(this, "Command 1...", Toast.LENGTH_SHORT)
.show();
}
else if (params.containsKey("command02")) {
Log.i(LOG_TAG, "onExecute(): " + params.getString("command02"));
Toast
.makeText(this, "Command 2...", Toast.LENGTH_SHORT)
.show();
}
else {
Log.w(LOG_TAG, "onExecute() - unknown command: " + params);
}
}
Below is shown the log recorded by LogCat during the error:
05-07 12:45:30.676: W/dalvikvm(363): JNI WARNING: JNI method called with exception raised
05-07 12:45:30.676: W/dalvikvm(363): in Ldalvik/system/NativeStart;.run ()V (CallStaticVoidMethodV)
05-07 12:45:30.676: W/dalvikvm(363): Pending exception is:
05-07 12:45:30.676: I/dalvikvm(363): Ljava/lang/RuntimeException;: Can't create handler inside thread that has not called Looper.prepare()
05-07 12:45:30.706: I/dalvikvm(363): at android.os.Handler.<init>(Handler.java:121)
05-07 12:45:30.706: I/dalvikvm(363): at android.widget.Toast.<init>(Toast.java:68)
05-07 12:45:30.706: I/dalvikvm(363): at android.widget.Toast.makeText(Toast.java:231)
05-07 12:45:30.706: I/dalvikvm(363): at org.example.android.applicationdemo.ApplicationDemoActivity.onExecute(ApplicationDemoActivity.java:130)
05-07 12:45:30.706: I/dalvikvm(363): at org.example.android.applicationdemo.ApplicationDemoActivity$2.execute(ApplicationDemoActivity.java:100)
05-07 12:45:30.706: I/dalvikvm(363): at org.example.android.servicedemo.IServiceCallback$Stub.onTransact(IServiceCallback.java:55)
05-07 12:45:30.706: I/dalvikvm(363): at android.os.Binder.execTransact(Binder.java:320)
05-07 12:45:30.706: I/dalvikvm(363): at dalvik.system.NativeStart.run(Native Method)
05-07 12:45:30.706: I/dalvikvm(363): "Binder Thread #2" prio=5 tid=8 NATIVE
05-07 12:45:30.706: I/dalvikvm(363): | group="main" sCount=0 dsCount=0 obj=0x405116f0 self=0x1f1590
05-07 12:45:30.706: I/dalvikvm(363): | sysTid=370 nice=0 sched=0/0 cgrp=default handle=2037072
05-07 12:45:30.714: I/dalvikvm(363): | schedstat=( 18375041 37002039 19 )
05-07 12:45:30.714: I/dalvikvm(363): at dalvik.system.NativeStart.run(Native Method)
05-07 12:45:30.714: E/dalvikvm(363): VM aborting

I think you create your Handler inside a method executed on a non-UI thread.
Make your Handler as a global variable.
private Handler _handler = new Handler();

Related

Ktor-server-test-host did not cleaned up Exposed database instannce across tests

I'm working on a web service using Ktor 1.6.8 and Exposed 0.39.2.
My application module and database is setup as following:
fun Application.module(testing: Boolean = false) {
val hikariConfig =
HikariConfig().apply {
driverClassName = "org.postgresql.Driver"
jdbcUrl = environment.config.propertyOrNull("ktor.database.url")?.getString()
username = environment.config.propertyOrNull("ktor.database.username")?.getString()
password = environment.config.propertyOrNull("ktor.database.password")?.getString()
maximumPoolSize = 10
isAutoCommit = false
transactionIsolation = "TRANSACTION_REPEATABLE_READ"
validate()
}
val pool = HikariDataSource(hikariConfig)
val db = Database.connect(pool, {}, DatabaseConfig { useNestedTransactions = true })
}
I use ktor-server-test-host, Test Containers and junit 5 to test the service. My test looks similar like below:
#Testcontainers
class SampleApplicationTest{
companion object {
#Container
val postgreSQLContainer = PostgreSQLContainer<Nothing>(DockerImageName.parse("postgres:13.4-alpine")).apply {
withDatabaseName("database_test")
}
}
#Test
internal fun `should make request successfully`() {
withTestApplication({
(environment.config as MapApplicationConfig).apply {
put("ktor.database.url", postgreSQLContainer.jdbcUrl)
put("ktor.database.user", postgreSQLContainer.username)
put("ktor.database.password", postgreSQLContainer.password)
}
module(testing = true)
}) {
handleRequest(...)
}
}
}
I observed an issue that if I ran multiple test classes together, some requests ended up using old Exposed db instance that was setup in a previous test class, causing the test case failed because the underlying database was already stopped.
When I ran one test class at a time, all were running fine.
Please refer to the log below for the error stack trace:
2022-10-01 08:00:36.102 [DefaultDispatcher-worker-5 #request#103] WARN Exposed - Transaction attempt #1 failed: java.sql.SQLTransientConnectionException: HikariPool-4 - Connection is not available, request timed out after 30001ms.. Statement(s): INSERT INTO cards (...)
org.jetbrains.exposed.exceptions.ExposedSQLException: java.sql.SQLTransientConnectionException: HikariPool-4 - Connection is not available, request timed out after 30001ms.
at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:49)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:143)
at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:128)
at org.jetbrains.exposed.sql.statements.Statement.execute(Statement.kt:28)
at org.jetbrains.exposed.sql.QueriesKt.insert(Queries.kt:73)
at com.example.application.services.CardService$createCard$row$1.invokeSuspend(CardService.kt:53)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt$suspendedTransactionAsyncInternal$1.invokeSuspend(Suspended.kt:127)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Caused by: java.sql.SQLTransientConnectionException: HikariPool-4 - Connection is not available, request timed out after 30001ms.
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:695)
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:197)
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:162)
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:100)
at org.jetbrains.exposed.sql.Database$Companion$connect$3.invoke(Database.kt:142)
at org.jetbrains.exposed.sql.Database$Companion$connect$3.invoke(Database.kt:139)
at org.jetbrains.exposed.sql.Database$Companion$doConnect$3.invoke(Database.kt:127)
at org.jetbrains.exposed.sql.Database$Companion$doConnect$3.invoke(Database.kt:128)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction$connectionLazy$1.invoke(ThreadLocalTransactionManager.kt:69)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction$connectionLazy$1.invoke(ThreadLocalTransactionManager.kt:68)
at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction.getConnection(ThreadLocalTransactionManager.kt:75)
at org.jetbrains.exposed.sql.Transaction.getConnection(Transaction.kt)
at org.jetbrains.exposed.sql.statements.InsertStatement.prepared(InsertStatement.kt:157)
at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:47)
... 19 common frames omitted
Caused by: org.postgresql.util.PSQLException: Connection to localhost:49544 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
at org.postgresql.Driver.makeConnection(Driver.java:465)
at org.postgresql.Driver.connect(Driver.java:264)
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358)
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477)
at com.zaxxer.hikari.pool.HikariPool.access$100(HikariPool.java:71)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:725)
at com.zaxxer.hikari.pool.HikariPool$PoolEntryCreator.call(HikariPool.java:711)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542)
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
I tried to add some cleanup code for Exposed's TransactionManager in my application module as following:
fun Application.module(testing: Boolean = false) {
// ...
val db = Database.connect(pool, {}, DatabaseConfig { useNestedTransactions = true })
if (testing) {
environment.monitor.subscribe(ApplicationStopped) {
TransactionManager.closeAndUnregister(db)
}
}
}
However, the issue still happened, and I also observed additional error as following:
2022-10-01 08:00:36.109 [DefaultDispatcher-worker-5 #request#93] ERROR Application - Unexpected error
java.lang.RuntimeException: database org.jetbrains.exposed.sql.Database#3bf4644c don't have any transaction manager
at org.jetbrains.exposed.sql.transactions.TransactionApiKt.getTransactionManager(TransactionApi.kt:149)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt.closeAsync(Suspended.kt:85)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt.access$closeAsync(Suspended.kt:1)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt$suspendedTransactionAsyncInternal$1.invokeSuspend(Suspended.kt:138)
(Coroutine boundary)
at org.mpierce.ktor.newrelic.KtorNewRelicKt$runPipelineInTransaction$2.invokeSuspend(KtorNewRelic.kt:178)
at org.mpierce.ktor.newrelic.KtorNewRelicKt$setUpNewRelic$2.invokeSuspend(KtorNewRelic.kt:104)
at io.ktor.routing.Routing.executeResult(Routing.kt:154)
at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:107)
at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:145)
at io.ktor.features.StatusPages$interceptCall$2.invokeSuspend(StatusPages.kt:102)
at io.ktor.features.StatusPages.interceptCall(StatusPages.kt:101)
at io.ktor.features.StatusPages$Feature$install$2.invokeSuspend(StatusPages.kt:142)
at io.ktor.features.CallLogging$Feature$install$2.invokeSuspend(CallLogging.kt:188)
at io.ktor.server.testing.TestApplicationEngine$callInterceptor$1.invokeSuspend(TestApplicationEngine.kt:296)
at io.ktor.server.testing.TestApplicationEngine$2.invokeSuspend(TestApplicationEngine.kt:50)
Caused by: java.lang.RuntimeException: database org.jetbrains.exposed.sql.Database#3bf4644c don't have any transaction manager
at org.jetbrains.exposed.sql.transactions.TransactionApiKt.getTransactionManager(TransactionApi.kt:149)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt.closeAsync(Suspended.kt:85)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt.access$closeAsync(Suspended.kt:1)
at org.jetbrains.exposed.sql.transactions.experimental.SuspendedKt$suspendedTransactionAsyncInternal$1.invokeSuspend(Suspended.kt:138)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Could someone show me what could be the issue here with my application code & test setup?
Thanks and regards.

io.helidon.webserver.ServerResponse.send() throwing error on server but file downloaded on front end

Trying to write one sample for (file upload and download) error in server.
Though it downloads the file in the frontend, getting error in server.
For Routing--
WebServer.builder(getRouting()).port(8080).build().start();
private static Routing getRouting() throws Exception{
return Routing.builder().register("/filetest", JerseySupport.builder().register(FileController.class).build())
.build();
}
#RequestScoped
public class FileController {
#Context
ServerRequest req;
#Context
ServerResponse res;
#GET
#Path("/{fname}")
public void download(#PathParam("fname") String fname) {
try {
//Getting the file
java.nio.file.Path filepath = Paths.get("c:/"+fname+".txt");
ResponseHeaders headers = res.headers();
headers.contentType(io.helidon.common.http.MediaType.APPLICATION_OCTET_STREAM);
headers.put(Http.Header.CONTENT_DISPOSITION, ContentDisposition.builder()
.filename(filepath.getFileName().toString())
.build()
.toString());
res.send(filepath);
}catch(Exception e) {
}
}
Jul 29, 2021 6:20:36 PM
io.helidon.webserver.RequestRouting$RoutedRequest defaultHandler
WARNING: Default error handler: Unhandled exception encountered.
java.util.concurrent.ExecutionException: Unhandled 'cause' of this
exception encountered. at
io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:397)
at
io.helidon.webserver.RequestRouting$RoutedRequest.nextNoCheck(RequestRouting.java:377)
at
io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:420)
at
io.helidon.webserver.jersey.ResponseWriter.failure(ResponseWriter.java:133)
at
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438)
at
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) at
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) at
org.glassfish.jersey.internal.Errors.process(Errors.java:292) at
org.glassfish.jersey.internal.Errors.process(Errors.java:274) at
org.glassfish.jersey.internal.Errors.process(Errors.java:244) at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
at
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
at
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
at
io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$3(JerseySupport.java:299)
at io.helidon.common.context.Contexts.runInContext(Contexts.java:117)
at
io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$5(ContextAwareExecutorImpl.java:154)
at
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834) Caused by:
io.helidon.common.http.AlreadyCompletedException: Response status code
and headers are already completed (sent to the client)! at
io.helidon.webserver.HashResponseHeaders$CompletionSupport.runIfNotCompleted(HashResponseHeaders.java:384)
at
io.helidon.webserver.HashResponseHeaders.httpStatus(HashResponseHeaders.java:251)
at io.helidon.webserver.Response.status(Response.java:122) at
io.helidon.webserver.Response.status(Response.java:48) at
io.helidon.webserver.jersey.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:81)
at
org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:607)
at
org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:373)
at
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:363)
at
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258)
... 14 more
Jul 29, 2021 6:20:36 PM
io.helidon.webserver.RequestRouting$RoutedRequest defaultHandler
WARNING: Cannot perform error handling of the throwable (see cause of
this exception) because headers were already sent
java.lang.IllegalStateException: Headers already sent. Cannot handle
the cause of this exception. at
io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:405)
at
io.helidon.webserver.RequestRouting$RoutedRequest.nextNoCheck(RequestRouting.java:377)
at
io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:420)
at
io.helidon.webserver.jersey.ResponseWriter.failure(ResponseWriter.java:133)
at
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438)
at
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:263)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) at
org.glassfish.jersey.internal.Errors$1.call(Errors.java:244) at
org.glassfish.jersey.internal.Errors.process(Errors.java:292) at
org.glassfish.jersey.internal.Errors.process(Errors.java:274) at
org.glassfish.jersey.internal.Errors.process(Errors.java:244) at
org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
at
org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
at
org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
at
io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$3(JerseySupport.java:299)
at io.helidon.common.context.Contexts.runInContext(Contexts.java:117)
at
io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$5(ContextAwareExecutorImpl.java:154)
at
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834) Caused by:
io.helidon.common.http.AlreadyCompletedException: Response status code
and headers are already completed (sent to the client)! at
io.helidon.webserver.HashResponseHeaders$CompletionSupport.runIfNotCompleted(HashResponseHeaders.java:384)
at
io.helidon.webserver.HashResponseHeaders.httpStatus(HashResponseHeaders.java:251)
at io.helidon.webserver.Response.status(Response.java:122) at
io.helidon.webserver.Response.status(Response.java:48) at
io.helidon.webserver.jersey.ResponseWriter.writeResponseStatusAndHeaders(ResponseWriter.java:81)
at
org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:607)
at
org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:373)
at
org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:363)
at
org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:258)
... 14 more
When using Helidon MP, Jersey is responsible for sending the response.
In the code above you are using the underlying WebServer and sending a response inside the body of a JAXRS resource method, when Jersey sends the response the exception is thrown because it was already sent.
Helidon MP Multipart example
Jersey Multipart documentation

Rollbacks using liquibase Api not being made

I am trying to do a rollback using Liquibase api but for some reason the rollbacks aren't being made
try {
Properties properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
String dbUrl=properties.getProperty("spring.datasource.url");
String dbUsername=properties.getProperty("spring.datasource.username");
String dbPassword=properties.getProperty("spring.datasource.password");
String changelogPath=properties.getProperty("spring.liquibase.change-log");
Connection connection = openConnection(dbUrl,dbUsername,dbPassword);
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase(changelogPath, new ClassLoaderResourceAccessor(), database);
liquibase.rollback(1,"");
} catch (SQLException | IOException | LiquibaseException e) {
logger.error(e.getMessage());
}
This is the message I am getting in the command line
INFO: Reading from `saft-demo-dump`.DATABASECHANGELOG
jun. 16, 2021 4:21:10 DA TARDE liquibase.lockservice
INFO: Successfully released change log lock
Do you want to see this operation's report in Liquibase Hub, which improves team collaboration?
If so, enter your email. If not, enter [N] to no longer be prompted, or [S] to skip for now, but ask again next time (default "S"):
jun. 16, 2021 4:21:51 DA TARDE liquibase.lockservice
INFO: Successfully acquired change log lock
Skipping auto-registration
jun. 16, 2021 4:21:51 DA TARDE liquibase.hub
WARNING: Skipping auto-registration
jun. 16, 2021 4:21:52 DA TARDE liquibase.lockservice
INFO: Successfully released change log lock
Am i doing something wrong?

Exception in tearDownDuskClass

Environment:
Dusk 5.0.2, Laravel 5.7.28, PHP 7.2.14, PHPUnit 7.5.8, MySQL 5.7, Laravel Homestead, ChromeDriver 2.45.615279
Problems:
№1
Exception in Tests\Browser\::tearDownDuskClass
Curl error thrown for http DELETE to /session/e76b9bfe8c9f9af519c2901601531c6a
Operation timed out after 30000 milliseconds with 0 bytes received
№2
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown
for http POST to /session/ce9d54d51ec982ff77aa5ca202159ca4/log with
params: {"type":"browser"}
Operation timed out after 30001 milliseconds with 0 bytes received
Caused by Facebook\WebDriver\Exception\WebDriverCurlException: Curl
error thrown for http GET to
/session/ce9d54d51ec982ff77aa5ca202159ca4/screenshot
Operation timed out after 30001 milliseconds with 0 bytes received
Test class:
class MyTest extends DuskTestCase
{
use DatabaseMigrations, WithFaker;
public function testExample()
{
$user = \factory(User::class)->create();
$this->browse(function (Browser $browser) use ($user) {
$browser
->loginAs($user)
->visitRoute(/**/)
->waitForText(/**/)
->type(/**/)
->type(/**/)
->type(/**/)
->attach(/**/, /**/)
->press(/**/)
->waitForText(/**/)
->assertSeeIn(/**/)
->click(/**/)
->assertSeeIn(/**/)
->click(/**/); //new tab is opened
$windows = collect($browser->driver->getWindowHandles());
//navigate to new tab
$browser->driver->switchTo()->window($windows->last());
$browser->assertPathIs(/**/);
});
}
}
Exceptions are thrown randomly. Usually the very first test after I up my VM is succeed.
Update: temporarily solved using
$window = $browser->driver->getWindowHandles()[1];
$browser->driver->switchTo()->window($window);
Update 2: even this code at the end of the test cause the error
$browser->driver->switchTo()->window($browser->driver->getWindowHandles()[1]);
$browser->screenshot('scr');
$browser->driver->switchTo()->window($browser->driver->getWindowHandles()[0]);

HTMLunit browser automation causes SSL error

I am writing a simple piece of Java code, to check my external IP and update my DNS entries if it has changed.
I use NetBeans and HTMLunit. The first part of the code works OK.
My code gets the proper External IP address from WhatIsMyIp.com
The second part, which involves logging into my DNS provider webpage (fastname.no) is attached below.
>
> public void LoginToDNS(HtmlPage LogInPage) throws Exception {
> try (final WebClient webClient = new WebClient()) {
>
> //get the webpage data we're looking for
> // also get the buttons and textfields
> final List<?> forms = LogInPage.getForms();
> final HtmlForm form = LogInPage.getFormByName("form1");
> final HtmlTextInput UsernameField = form.getInputByName("username");
> final HtmlPasswordInput PasswordField = form.getInputByName("password");
> final HtmlButton submit_button = form.getButtonByName("submit");
>
> // Set the Username and pasword
> UsernameField.setValueAttribute("myusername");
> PasswordField.setValueAttribute("mypassword");
>
> // submit the form by clicking the button and get back the control panel page
> final HtmlPage DNSPage = submit_button.click();
> CheckDNSEntries(DNSPage);
>
> }
> }
Although I can see that I have logged in properly (I know, because the page title changes to the correct one), I see an SSL error and the resulting webpage is not what I want.
java.util.concurrent.ExecutionException: java.io.IOException: Cannot init SSL
....
Caused by: java.io.IOException: Cannot init SSL
......
at org.eclipse.jetty.websocket.client.io.WebSocketClientSelectorManager.newConnection(WebSocketClientSelectorManager.java:96)
... 3 more
Right when I press the Submit_button, I see a hell of a lot of errors on the debugger coming from htmlunit. (see below)
Apr 12, 2016 10:36:49 AM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[https://www.fastname.no/panel/js/custom/bootstrap.js?bust=?v20160408110428] line=[66] lineSource=[null] lineOffset=[0]
Apr 12, 2016 10:36:49 AM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[https://www.fastname.no/panel/js/custom/bootstrap.js?bust=?v20160408110428] line=[66] lineSource=[null] lineOffset=[0]
Apr 12, 2016 10:36:51 AM com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl runSingleJob
SEVERE: Job run failed with unexpected RuntimeException: [object Object] (https://www.googletagmanager.com/gtm.js?id=GTM-TWH4RS&bust=?v20160408110428#65)
======= EXCEPTION START ========
Exception class=[net.sourceforge.htmlunit.corejs.javascript.JavaScriptException]
com.gargoylesoftware.htmlunit.ScriptException: [object Object] (https://www.googletagmanager.com/gtm.js?id=GTM-TWH4RS&bust=?v20160408110428#65)
Apr 12, 2016 10:36:52 AM com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManagerImpl runSingleJob
SEVERE: Job run failed with unexpected RuntimeException: [object Object] (https://www.googletagmanager.com/gtm.js?id=GTM-TWH4RS&bust=?v20160408110428#65)
======= EXCEPTION START ========
Exception class=[net.sourceforge.htmlunit.corejs.javascript.JavaScriptException]
com.gargoylesoftware.htmlunit.ScriptException: [object Object] (https://www.googletagmanager.com/gtm.js?id=GTM-TWH4RS&bust=?v20160408110428#65)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.handleJavaScriptException(JavaScriptEngine.java:982)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:894)
2016-04-12 10:36:55.106:INFO::JS executor for com.gargoylesoftware.htmlunit.WebClient#d1a10ac: Logging initialized #10613ms
Apr 12, 2016 10:36:55 AM com.gargoylesoftware.htmlunit.javascript.host.WebSocket run
SEVERE: WS connect error
java.util.concurrent.ExecutionException: java.io.IOException: Cannot init SSL
at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:123)
at com.gargoylesoftware.htmlunit.javascript.host.WebSocket$1.run(WebSocket.java:130)
I am not quite sure whether this is a HTMLunit problem handling JavaScript or it's just my code. Is there a better solution that I can use instead of HTMLunit that will get me where I want to go faster? After all I think the task I want to accomplish is relatively simple.
Try to enable SSL by adding:
webClient.getOptions().setUseInsecureSSL(true);
You can find more documentation here.
EDIT:
In order to hide the HtmlUnit logs, you have to add this:
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);