Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
a72e706
Created ShutdownManager
mei-kyoseva Jul 15, 2024
8a72fbf
Added containsKey and containsValue methods to SingletonFactory
mei-kyoseva Jul 15, 2024
7c02244
DriverService and PlaywrightService changes
mei-kyoseva Jul 15, 2024
94174a1
Update ShutdownManager.java
mei-kyoseva Jul 15, 2024
efff035
Changes to DriverService and PlaywrightService
mei-kyoseva Jul 16, 2024
9a82266
minor fixes
mei-kyoseva Jul 16, 2024
21006b2
Renamed BrowserTypes enum to Browsers in Playwright module
mei-kyoseva Jul 16, 2024
4183ed7
Removed "forceCloseBrowser" setting
mei-kyoseva Jul 16, 2024
e294604
Revert "Renamed BrowserTypes enum to Browsers in Playwright module"
mei-kyoseva Jul 16, 2024
e6aa3d6
Update BaseTest.java
mei-kyoseva Aug 6, 2024
c57e4b3
Update ShutdownManager.java
mei-kyoseva Aug 6, 2024
d20caa7
Update PlaywrightService.java
mei-kyoseva Aug 6, 2024
f97b57b
Update DriverService.java
mei-kyoseva Aug 6, 2024
632b24e
Merge branch 'main' into fixes-and-refactoring
mei-kyoseva Jan 9, 2025
6861fb4
Merging conflict resolved
mei-kyoseva Jan 9, 2025
6d7debd
refactoring: moved retry logic into a separate method in ShadowDomSer…
mei-kyoseva Jan 9, 2025
5573f2c
Update PlaywrightService.java
mei-kyoseva Jan 13, 2025
00ab083
Update DriverService.java
mei-kyoseva Jan 13, 2025
7bbcc7c
revert changes to testFrameworkSettings.dev.json
mei-kyoseva Jan 15, 2025
5229a34
Merge branch 'main' into fixes-and-refactoring
mei-kyoseva Jan 15, 2025
693aa75
Merge remote-tracking branch 'refs/remotes/origin/main' into fixes-an…
Jan 22, 2025
876f283
fixing formatting of PlaywrightService file
mei-kyoseva Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,10 @@
public class BaseTest extends UsesPlugins {
static final ThreadLocal<TestResult> CURRENT_TEST_RESULT = new ThreadLocal<>();
static final ThreadLocal<TimeRecord> CURRENT_TEST_TIME_RECORD = ThreadLocal.withInitial(TimeRecord::new);
private static final ThreadLocal<Boolean> CONFIGURATION_EXECUTED = new ThreadLocal<>();
private static final ThreadLocal<Boolean> CONFIGURATION_EXECUTED = ThreadLocal.withInitial(() -> false);
private static final List<String> ALREADY_EXECUTED_BEFORE_CLASSES = Collections.synchronizedList(new ArrayList<>());
private TestInfo testInfo;

public BaseTest() {
// try {
// Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
// theUnsafe.setAccessible(true);
// Unsafe u = (Unsafe)theUnsafe.get(null);
//
// Class<?> cls = Class.forName("jdk.internal.module.IllegalAccessLogger");
// Field logger = cls.getDeclaredField("logger");
// u.putObjectVolatile(cls, u.staticFieldOffset(logger), null);
// } catch (Exception ignored) {}
CONFIGURATION_EXECUTED.set(false);
}

@BeforeEach
public void beforeMethodCore(TestInfo testInfo) throws Exception {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package solutions.bellatrix.core.utilities;

import lombok.experimental.UtilityClass;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@UtilityClass
public class ShutdownManager {
private static final List<Runnable> instructions = new CopyOnWriteArrayList<>();
private static final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

static {
Runtime.getRuntime().addShutdownHook(new Thread(ShutdownManager::runAllInstructions));
}

public static void register(Runnable runnable) {
instructions.add(runnable);
}

private static void runAllInstructions() {
if (instructions.isEmpty()) return;

try {
// Submit all instructions for parallel execution
for (var instruction : instructions) {
executor.submit(() -> {
try {
instruction.run();
} catch (Exception ex) {
DebugInformation.debugInfo(ex.getMessage());
}
});
}
} finally {
shutdownAndAwaitTermination(executor);
}
}

private static void shutdownAndAwaitTermination(ExecutorService executor) {
executor.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executor.awaitTermination(60, TimeUnit.SECONDS)) {
System.err.println("Executor did not terminate.");
}
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
executor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public static <T> void register(Class<?> classKey, T instance) {
mapHolder.get().put(classKey, instance);
}

public static boolean containsKey(Class<?> classOf) {
return mapHolder.get().containsKey(classOf);
}

public static boolean containsValue(Object object) {
return mapHolder.get().containsValue(object);
}

public static void clear() {
mapHolder.get().clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,8 @@ private static String[] getAbsoluteCss(ShadowRoot shadowRoot, String locator) {
.evaluate(String.format("(el, [locator]) => (%s)(el, locator)", javaScript), new Object[] { locator }))
.toArray(String[]::new);
};
if (Wait.retry(() -> {
String[] foundElements;
try {
foundElements = js.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
if (foundElements == null || foundElements.length == 0) {
throw new IllegalArgumentException();
}
}, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) {
try {
return js.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
throw new IllegalArgumentException("No elements inside the shadow DOM were found with the locator: " + locator);
}

return getCss(js, locator);
}

private static String[] getRelativeCss(ShadowRoot shadowRoot, String locator, String parentLocator) {
Expand All @@ -134,10 +117,14 @@ private static String[] getRelativeCss(ShadowRoot shadowRoot, String locator, St
.toArray(String[]::new);
};

return getCss(js, locator);
}

private static String[] getCss(Callable<String[]> callable, String locator) {
if(Wait.retry(() -> {
String[] foundElements;
try {
foundElements = js.call();
foundElements = callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -146,15 +133,14 @@ private static String[] getRelativeCss(ShadowRoot shadowRoot, String locator, St
}
}, Duration.ofSeconds(ConfigurationService.get(WebSettings.class).getTimeoutSettings().getElementWaitTimeout()), Duration.ofSeconds(1), false)) {
try {
return js.call();
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
} else {
throw new IllegalArgumentException("No elements inside the shadow DOM were found with the locator: " + locator);
}
}

private static <TComponent extends WebComponent> TComponent buildMissingShadowRootsAndCreate(Class<TComponent> clazz, ShadowRoot parentComponent, Ref<String> fullCss) {
var component = InstanceFactory.create(clazz);

Expand Down
Loading
Loading