Skip to content

Commit

Permalink
test(java): ✅ added tests for session creation
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB committed Oct 18, 2023
1 parent 0e2cd31 commit 3e6bd2b
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 41 deletions.
32 changes: 18 additions & 14 deletions core-java/src/main/java/com/github/wasiqb/boyka/enums/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public enum Message {
/**
* Action not supported error.
*/
ACTION_NOT_SUPPORTED_ON_PLATFORM ("[{0}] action is not supported on [{1}] platform..."),
ACTION_NOT_SUPPORTED_ON_PLATFORM ("({0}) action is not supported on ({1}) platform..."),
/**
* Basic auth password is empty.
*/
AUTH_PASSWORD_REQUIRED ("Basic auth password is required..."),
/**
* Browser option not supported Error.
*/
BROWSER_OPTION_NOT_SUPPORTED ("Browser options not supported for [{0}] browser..."),
BROWSER_OPTION_NOT_SUPPORTED ("Browser options not supported for ({0}) browser..."),
/**
* Remote execution requires capabilities
*/
Expand All @@ -48,7 +48,7 @@ public enum Message {
* Config key is not found.
*/
CONFIG_KEY_NOT_FOUND (
"The key [{0}] mentioned is not found in the config file, please provide a valid keys from the following [{1}]..."),
"The key ({0}) mentioned is not found in the config file, please provide a valid keys from the following ({1})..."),
/**
* Content type must be set before setting request body
*/
Expand All @@ -57,7 +57,7 @@ public enum Message {
* Context switching not allowed for apps other than Hybrid app.
*/
CONTEXT_SWITCHING_NOT_ALLOWED (
"Context switching is not allowed for application other than Hybrid, current application type is [{0}]..."),
"Context switching is not allowed for application other than Hybrid, current application type is ({0})..."),
/**
* Error when driver is null.
*/
Expand All @@ -73,7 +73,7 @@ public enum Message {
/**
* Element not found.
*/
ELEMENT_NOT_FOUND ("Element [{0}] not found for platform [{1}]..."),
ELEMENT_NOT_FOUND ("Element ({0}) not found for platform ({1})..."),
/**
* Empty browser is not allowed.
*/
Expand Down Expand Up @@ -113,7 +113,7 @@ public enum Message {
/**
* Error reading file
*/
ERROR_READING_FILE ("Error occurred reading file [{0}]..."),
ERROR_READING_FILE ("Error occurred reading file ({0})..."),
/**
* Error while saving screenshot.
*/
Expand All @@ -133,15 +133,15 @@ public enum Message {
/**
* Error writing file
*/
ERROR_WRITING_FILE ("Error occurred writing file [{0}]..."),
ERROR_WRITING_FILE ("Error occurred writing file ({0})..."),
/**
* Error while writing logs to file.
*/
ERROR_WRITING_LOGS ("Error occurred while writing logs..."),
/**
* Finger should be within the bounds.
*/
FINGER_OUT_OF_BOUND ("Finger co-ordinates [{0}] is outside the bounds [{1}]..."),
FINGER_OUT_OF_BOUND ("Finger co-ordinates ({0}) is outside the bounds ({1})..."),
/**
* Host name is required for Remote execution
*/
Expand All @@ -157,11 +157,11 @@ public enum Message {
/**
* Invalid listener class provided.
*/
INVALID_LISTENER_FOUND ("Invalid Listener class [{0}] provided in the config..."),
INVALID_LISTENER_FOUND ("Invalid Listener class ({0}) provided in the config..."),
/**
* Invalid platform for operation
*/
INVALID_PLATFORM_FOR_OPERATION ("Platform [{0}] is not supported for this setting..."),
INVALID_PLATFORM_FOR_OPERATION ("Platform ({0}) is not supported for this setting..."),
/**
* Invalid Remote session URL.
*/
Expand All @@ -185,7 +185,7 @@ public enum Message {
/**
* No JSON file found
*/
NO_JSON_FILE_FOUND ("JSON file [{0}] not found..."),
NO_JSON_FILE_FOUND ("JSON file ({0}) not found..."),
/**
* No virtual keyboard for Web platform.
*/
Expand All @@ -201,19 +201,23 @@ public enum Message {
/**
* Protocol is required for host name
*/
PROTOCOL_REQUIRED_FOR_HOST ("Protocol is required for host [{0}]..."),
PROTOCOL_REQUIRED_FOR_HOST ("Protocol is required for host ({0})..."),
/**
* Schema validation assert failure
*/
RESPONSE_SCHEMA_NOT_MATCHING ("Schema validation assert failure..."),
/**
* Session already cleared.
*/
SESSION_ALREADY_CLEARED ("Session already cleared for ({0}) persona..."),
/**
* Session already created.
*/
SESSION_ALREADY_CREATED ("Session is already created for [{0}] persona..."),
SESSION_ALREADY_CREATED ("Session is already created for ({0}) persona..."),
/**
* Session has not been created.
*/
SESSION_NOT_CREATED ("Session has not been created for [{0}] persona..."),
SESSION_NOT_CREATED ("Session has not been created for ({0}) persona..."),
/**
* Error when starting session.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.github.wasiqb.boyka.manager;

import static com.github.wasiqb.boyka.enums.Message.SESSION_ALREADY_CLEARED;
import static com.github.wasiqb.boyka.enums.Message.SESSION_ALREADY_CREATED;
import static com.github.wasiqb.boyka.enums.Message.SESSION_NOT_CREATED;
import static com.github.wasiqb.boyka.enums.Message.SESSION_PERSONA_CANNOT_BE_NULL;
Expand Down Expand Up @@ -72,13 +73,16 @@ public static synchronized void clearAllSessions () {
*/
public static void clearSession () {
LOGGER.info ("Clearing session for persona [{}]...", getCurrentPersona ());
final var session = SESSION.get ();
if (session.isEmpty ()) {
throwError (SESSION_ALREADY_CLEARED, getCurrentPersona ());
}
getSession ().clearListeners ();
getSession ().clearSharedData ();
ofNullable (getSession ().getDriver ()).ifPresent (WebDriver::quit);
if (getSession ().getPlatformType () != WEB) {
ofNullable (getSession ().getServiceManager ()).ifPresent (ServiceManager::stopServer);
}
final var session = SESSION.get ();
if (!session.isEmpty ()) {
session.remove (getCurrentPersona ());
}
Expand Down Expand Up @@ -138,7 +142,7 @@ public static synchronized <D extends WebDriver> DriverSession<D> getSession ()
final var session = SESSION.get ()
.containsKey (currentPersona);
if (!session) {
throwError (SESSION_NOT_CREATED);
throwError (SESSION_NOT_CREATED, currentPersona);
}
return LOGGER.traceExit ((DriverSession<D>) SESSION.get ()
.get (currentPersona));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.github.wasiqb.boyka.testng.others;

import static com.github.wasiqb.boyka.enums.Message.TEST_ERROR;
import static com.github.wasiqb.boyka.utils.ErrorHandler.handleAndThrow;

import java.io.IOException;

import com.github.wasiqb.boyka.enums.Message;
import com.github.wasiqb.boyka.exception.FrameworkError;
import org.testng.annotations.Test;

Expand All @@ -36,6 +36,6 @@ public class ErrorHandlingTest {
*/
@Test (expectedExceptions = FrameworkError.class, expectedExceptionsMessageRegExp = "Test error...")
public void testFrameworkError () {
handleAndThrow (Message.TEST_ERROR, new IOException ("File not found"));
handleAndThrow (TEST_ERROR, new IOException ("File not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2022 Wasiq Bhamla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package com.github.wasiqb.boyka.testng.others;

import static com.github.wasiqb.boyka.enums.PlatformType.WEB;
import static com.github.wasiqb.boyka.manager.ParallelSession.clearAllSessions;
import static com.github.wasiqb.boyka.manager.ParallelSession.clearSession;
import static com.github.wasiqb.boyka.manager.ParallelSession.createSession;
import static com.github.wasiqb.boyka.manager.ParallelSession.getSession;

import com.github.wasiqb.boyka.exception.FrameworkError;
import org.testng.annotations.Test;

/**
* Test class for testing Parallel Session class.
*
* @author Wasiq Bhamla
* @since 18-Oct-2023
*/
public class SessionTest {
private static final String PERSONA = "SessionTest";

/**
* Test duplicate clear session.
*/
@Test (description = "Test duplicate clear session", expectedExceptions = FrameworkError.class, expectedExceptionsMessageRegExp = "Session already cleared for \\(\\) persona...")
public void testDuplicateClearSession () {
try {
createSession (PERSONA, WEB, "test_local_chrome");
} finally {
clearSession ();
clearSession ();
}
}

/**
* Test Duplicate Session creation.
*/
@Test (description = "Test Duplicate Session creation", expectedExceptions = FrameworkError.class, expectedExceptionsMessageRegExp = "Session is already created for \\(SessionTest\\) persona...")
public void testDuplicateSessionCreation () {
try {
createSession (PERSONA, WEB, "test_local_chrome");
createSession (PERSONA, WEB, "test_local_chrome");
} finally {
clearAllSessions ();
}
}

/**
* Test get session without session creation.
*/
@Test (description = "Test get session without session creation", expectedExceptions = FrameworkError.class, expectedExceptionsMessageRegExp = "Session has not been created for \\(\\) persona...")
public void testGetSessionWithoutSessionCreated () {
getSession ();
}

/**
* Test session creation with Null persona.
*/
@Test (description = "Test session creation with Null persona", expectedExceptions = FrameworkError.class, expectedExceptionsMessageRegExp = "Session Persona cannot be empty or null...")
public void testSessionCreationWithNullPersona () {
createSession (null, WEB, "test_local_chrome");
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@commitlint/cli": "^17.8.0",
"@commitlint/config-conventional": "^17.8.0",
"@lerna/child-process": "^7.3.1",
"@lerna/child-process": "^7.4.1",
"@lerna/version": "^6.6.2",
"@types/node": "^20.8.6",
"@typescript-eslint/eslint-plugin": "^6.8.0",
Expand All @@ -46,7 +46,7 @@
"eslint-plugin-react": "^7.33.2",
"husky": "^8.0.3",
"js-yaml": "^4.1.0",
"lerna": "7.3.1",
"lerna": "7.4.1",
"lerna-changelog": "^2.2.0",
"lint-staged": "^15.0.1",
"lodash": "^4.17.21",
Expand Down
Loading

0 comments on commit 3e6bd2b

Please sign in to comment.