From 9619172216ca7e79edf98a9376f45bfbebe81d2a Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Fri, 3 Jun 2022 21:46:05 +0530 Subject: [PATCH] Fix for #130 (#132) * Starting AppiumEngine if not started --- .../selcukes/core/driver/AppiumEngine.java | 33 +++++++--------- .../selcukes/core/driver/DesktopManager.java | 7 ++++ .../selcukes/core/driver/DriverManager.java | 4 +- .../selcukes/core/driver/GridRunner.java | 8 ++-- .../core/validation/PageValidations.java | 4 ++ .../core/tests/web/SampleTestListener.java | 39 ------------------- .../core/tests/{ => win}/NotepadTest.java | 2 +- 7 files changed, 31 insertions(+), 66 deletions(-) delete mode 100644 selcukes-core/src/test/java/io/github/selcukes/core/tests/web/SampleTestListener.java rename selcukes-core/src/test/java/io/github/selcukes/core/tests/{ => win}/NotepadTest.java (97%) diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/driver/AppiumEngine.java b/selcukes-core/src/main/java/io/github/selcukes/core/driver/AppiumEngine.java index fb02fb551..54d3b317d 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/driver/AppiumEngine.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/driver/AppiumEngine.java @@ -24,8 +24,6 @@ import java.net.URL; -import static io.github.selcukes.core.driver.RunMode.isCloudAppium; - @CustomLog class AppiumEngine { private AppiumDriverLocalService service; @@ -39,29 +37,26 @@ public static AppiumEngine getInstance() { URL getServiceUrl() { if (service == null) { - throw new DriverSetupException("Appium Local server is not started...\n" + - "Please use 'GridRunner.startAppium' method to start."); + logger.debug(() -> "Appium server is not started yet. \nStarting Appium Server now..."); + startLocalServer(); } return service.getUrl(); } void startLocalServer() { - if (!isCloudAppium()) { - try { - service = new AppiumServiceBuilder() - .withIPAddress("127.0.0.1") - .usingAnyFreePort() - .withArgument(GeneralServerFlag.SESSION_OVERRIDE) - .withArgument(GeneralServerFlag.BASEPATH, "/wd/") - .build(); - logger.info(() -> "Starting Appium server..."); - service.start(); - logger.debug(() -> String.format("Using Local ServiceUrl[%s]", service.getUrl())); - } catch (Exception e) { - throw new DriverSetupException("Failed starting Appium Server..", e); - } + try { + service = new AppiumServiceBuilder() + .withIPAddress("127.0.0.1") + .usingAnyFreePort() + .withArgument(GeneralServerFlag.SESSION_OVERRIDE) + .withArgument(GeneralServerFlag.BASEPATH, "/wd/") + .build(); + logger.info(() -> "Starting Appium server..."); + service.start(); + logger.debug(() -> String.format("Using Local ServiceUrl[%s]", service.getUrl())); + } catch (Exception e) { + throw new DriverSetupException("Failed starting Appium Server..", e); } - } void stopServer() { diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/driver/DesktopManager.java b/selcukes-core/src/main/java/io/github/selcukes/core/driver/DesktopManager.java index a3f25c980..e0e3e36a9 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/driver/DesktopManager.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/driver/DesktopManager.java @@ -20,6 +20,7 @@ import io.github.selcukes.commons.config.ConfigFactory; import io.github.selcukes.commons.exception.DriverSetupException; import lombok.CustomLog; +import lombok.SneakyThrows; import org.openqa.selenium.WebDriver; import java.net.URL; @@ -42,4 +43,10 @@ public synchronized WebDriver createDriver() { } return windowsDriver; } + + @SneakyThrows + @Override + public URL getServiceUrl() { + return AppiumEngine.getInstance().getServiceUrl(); + } } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/driver/DriverManager.java b/selcukes-core/src/main/java/io/github/selcukes/core/driver/DriverManager.java index 1609223c9..0d84d157c 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/driver/DriverManager.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/driver/DriverManager.java @@ -79,7 +79,7 @@ public static void setDriver(D driveThread) { DRIVER_THREAD.set(driveThread); } - public static void removeDriver() { + public static synchronized void removeDriver() { try { if (getDriver() != null) { STORED_DRIVER.remove(getDriver()); @@ -90,7 +90,7 @@ public static void removeDriver() { } } - public static void removeAllDrivers() { + public static synchronized void removeAllDrivers() { logger.debug(() -> String.format("Closing [%d] stored drivers..", STORED_DRIVER.size())); STORED_DRIVER.stream().filter(Objects::nonNull).forEach(d -> { try { diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/driver/GridRunner.java b/selcukes-core/src/main/java/io/github/selcukes/core/driver/GridRunner.java index 6e0e6ef4b..6b5a8d18b 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/driver/GridRunner.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/driver/GridRunner.java @@ -24,8 +24,7 @@ import java.util.Arrays; -import static io.github.selcukes.core.driver.RunMode.isCloudBrowser; -import static io.github.selcukes.core.driver.RunMode.isLocalBrowser; +import static io.github.selcukes.core.driver.RunMode.*; @CustomLog @UtilityClass @@ -47,14 +46,13 @@ public static synchronized void startSelenium(DriverType... driverType) { } } - static boolean isSeleniumServerNotRunning() { return !isLocalBrowser() && !GridRunner.isRunning; } public static void startAppium() { - AppiumEngine.getInstance().startLocalServer(); - } + if (!isCloudAppium()) + AppiumEngine.getInstance().startLocalServer(); } public static void stopAppium() { AppiumEngine.getInstance().stopServer(); diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java b/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java index 81292bce5..dbce6d425 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/validation/PageValidations.java @@ -16,6 +16,7 @@ package io.github.selcukes.core.validation; +import io.appium.java_client.AppiumBy; import io.github.selcukes.core.page.Page; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; @@ -43,6 +44,9 @@ public void titleContains(String expectedTitle) { } } + public ElementValidation element(String accessibilityId) { + return new ElementValidation(isSoft, page, page.find(AppiumBy.accessibilityId(accessibilityId))); + } public ElementValidation element(By by) { return new ElementValidation(isSoft, page, page.find(by)); } diff --git a/selcukes-core/src/test/java/io/github/selcukes/core/tests/web/SampleTestListener.java b/selcukes-core/src/test/java/io/github/selcukes/core/tests/web/SampleTestListener.java deleted file mode 100644 index 923a883ff..000000000 --- a/selcukes-core/src/test/java/io/github/selcukes/core/tests/web/SampleTestListener.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) Ramesh Babu Prudhvi. - * - * 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 io.github.selcukes.core.tests.web; - -import lombok.CustomLog; -import org.testng.*; - -@CustomLog -public class SampleTestListener implements ITestListener, IInvokedMethodListener { - public void onStart(ITestContext context) { - logger.info(() -> "Before Test: " + context.getName()); - } - - public void onFinish(ITestContext context) { - logger.info(() -> "After Test: " + context.getName()); - } - - public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { - logger.info(() -> "beforeInvocation: " + method.getTestMethod().getMethodName()); - } - - public void afterInvocation(IInvokedMethod method, ITestResult testResult) { - logger.info(() -> "afterInvocation: " + method.getTestMethod().getMethodName()); - } -} diff --git a/selcukes-core/src/test/java/io/github/selcukes/core/tests/NotepadTest.java b/selcukes-core/src/test/java/io/github/selcukes/core/tests/win/NotepadTest.java similarity index 97% rename from selcukes-core/src/test/java/io/github/selcukes/core/tests/NotepadTest.java rename to selcukes-core/src/test/java/io/github/selcukes/core/tests/win/NotepadTest.java index 28a26ab8b..66518c5a9 100644 --- a/selcukes-core/src/test/java/io/github/selcukes/core/tests/NotepadTest.java +++ b/selcukes-core/src/test/java/io/github/selcukes/core/tests/win/NotepadTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.github.selcukes.core.tests; +package io.github.selcukes.core.tests.win; import io.appium.java_client.windows.WindowsDriver; import io.github.selcukes.core.driver.DriverManager;