Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -18,7 +18,6 @@

import io.appium.java_client.android.AndroidDriver;
import io.github.selcukes.commons.config.ConfigFactory;
import io.github.selcukes.commons.exception.DriverSetupException;
import io.github.selcukes.wdb.enums.DriverType;
import lombok.CustomLog;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -57,47 +56,35 @@ public URL getServiceUrl() {
}

public WebDriver createBrowserDriver(String browser) {
WebDriver driver;
try {
logger.debug(() -> "Initiating New Mobile Browser Session...");
Capabilities capabilities = AppiumOptions.getUserOptions();
if (capabilities == null) {
String platform = ConfigFactory.getConfig().getMobile().getPlatform();
capabilities = BrowserOptions.getBrowserOptions(DriverType.valueOf(browser), isCloudAppium(), platform);
if (isCloudAppium()) {
capabilities = capabilities.merge(CloudOptions.getBrowserStackOptions(false));
}

logger.debug(() -> "Initiating New Mobile Browser Session...");
Capabilities capabilities = AppiumOptions.getUserOptions();
if (capabilities == null) {
String platform = ConfigFactory.getConfig().getMobile().getPlatform();
capabilities = BrowserOptions.getBrowserOptions(DriverType.valueOf(browser), isCloudAppium(), platform);
if (isCloudAppium()) {
capabilities = capabilities.merge(CloudOptions.getBrowserStackOptions(false));
}
driver = new RemoteWebDriver(getServiceUrl(), capabilities);
} catch (Exception e) {
throw new DriverSetupException("Driver was not setup properly.", e);

}
return driver;
return new RemoteWebDriver(getServiceUrl(), capabilities);
}

public WebDriver createAppDriver() {

WebDriver driver;
try {
logger.debug(() -> "Initiating New Mobile App Session...");
Capabilities capabilities = AppiumOptions.getUserOptions();
if (capabilities == null) {
if (isCloudAppium()) {
capabilities = CloudOptions.getBrowserStackOptions(true);
} else {
Path appPath = Paths.get(ConfigFactory.getConfig()
.getMobile().getApp());
String app = appPath.toAbsolutePath().toString();
logger.info(() -> "Using APP: " + app);
capabilities = AppiumOptions.getAndroidOptions(app);
}
logger.debug(() -> "Initiating New Mobile App Session...");
Capabilities capabilities = AppiumOptions.getUserOptions();
if (capabilities == null) {
if (isCloudAppium()) {
capabilities = CloudOptions.getBrowserStackOptions(true);
} else {
Path appPath = Paths.get(ConfigFactory.getConfig()
.getMobile().getApp());
String app = appPath.toAbsolutePath().toString();
logger.info(() -> "Using APP: " + app);
capabilities = AppiumOptions.getAndroidOptions(app);
}
driver = new AndroidDriver(getServiceUrl(), capabilities);
} catch (Exception e) {
throw new DriverSetupException("Driver was not setup properly.", e);
}
return driver;
return new AndroidDriver(getServiceUrl(), capabilities);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.appium.java_client.windows.WindowsDriver;
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;
Expand All @@ -31,17 +30,12 @@ public class DesktopManager extends AppiumManager {

@Override
public synchronized WebDriver createDriver() {
WebDriver windowsDriver;
try {
logger.debug(() -> "Initiating New Desktop Session...");
String app = ConfigFactory.getConfig().getWindows().getApp();
URL serviceUrl = Objects.requireNonNull(getServiceUrl());
AppiumOptions.setServiceUrl(serviceUrl);
windowsDriver = new WindowsDriver(serviceUrl, AppiumOptions.getWinAppOptions(app));
} catch (Exception e) {
throw new DriverSetupException("Driver was not setup properly.", e);
}
return windowsDriver;

logger.debug(() -> "Initiating New Desktop Session...");
String app = ConfigFactory.getConfig().getWindows().getApp();
URL serviceUrl = Objects.requireNonNull(getServiceUrl());
AppiumOptions.setServiceUrl(serviceUrl);
return new WindowsDriver(serviceUrl, AppiumOptions.getWinAppOptions(app));
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
import org.openqa.selenium.support.events.WebDriverListener;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@CustomLog
@UtilityClass
public class DriverManager {

private static final ThreadLocal<Object> DRIVER_THREAD = new InheritableThreadLocal<>();
private static final Set<Object> STORED_DRIVER = new HashSet<>();
private static final Map<Integer, Object> STORED_DRIVER = new ConcurrentHashMap<>();

public static synchronized <D extends WebDriver> D createDriver(DeviceType deviceType, Capabilities... capabilities) {
Arrays.stream(capabilities).findAny().ifPresent(AppiumOptions::setUserOptions);
Expand Down Expand Up @@ -74,15 +74,15 @@ public static <D extends WebDriver> D getDriver() {
return (D) DRIVER_THREAD.get();
}

public static <D extends WebDriver> void setDriver(D driveThread) {

DRIVER_THREAD.set(driveThread);
public static <D extends WebDriver> void setDriver(D driver) {
DRIVER_THREAD.set(driver);
STORED_DRIVER.putIfAbsent(driver.hashCode(), driver);
}

public static synchronized void removeDriver() {
try {
if (getDriver() != null) {
STORED_DRIVER.remove(getDriver());
STORED_DRIVER.remove(getDriver().hashCode());
getDriver().quit();
}
} finally {
Expand All @@ -92,11 +92,11 @@ public static synchronized void removeDriver() {

public static synchronized void removeAllDrivers() {
logger.debug(() -> String.format("Closing [%d] stored drivers..", STORED_DRIVER.size()));
STORED_DRIVER.stream().filter(Objects::nonNull).forEach(d -> {
STORED_DRIVER.values().stream().filter(Objects::nonNull).forEach(d -> {
try {
((WebDriver) d).quit();
} finally {
STORED_DRIVER.remove(d);
STORED_DRIVER.remove(d.hashCode());
}
});
DRIVER_THREAD.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.github.selcukes.core.driver;

import io.github.selcukes.commons.config.ConfigFactory;
import io.github.selcukes.commons.exception.DriverSetupException;
import io.github.selcukes.wdb.enums.DriverType;
import lombok.CustomLog;
import lombok.SneakyThrows;
Expand All @@ -38,31 +37,24 @@ public class WebManager implements RemoteManager {

public synchronized WebDriver createDriver() {
String browser = ConfigFactory.getConfig().getWeb().getBrowser().toUpperCase();
WebDriver driver;
try {
logger.debug(() -> "Initiating New Browser Session...");
Capabilities capabilities = AppiumOptions.getUserOptions();
if (capabilities == null) {
capabilities = BrowserOptions.getBrowserOptions(DriverType.valueOf(browser),
!(isLocalBrowser() || isCloudBrowser()));
if (isCloudBrowser()) {
capabilities = capabilities.merge(CloudOptions.getBrowserStackOptions(false));
}
logger.debug(() -> "Initiating New Browser Session...");
Capabilities capabilities = AppiumOptions.getUserOptions();
if (capabilities == null) {
capabilities = BrowserOptions.getBrowserOptions(DriverType.valueOf(browser),
!(isLocalBrowser() || isCloudBrowser()));
if (isCloudBrowser()) {
capabilities = capabilities.merge(CloudOptions.getBrowserStackOptions(false));
}
RemoteWebDriverBuilder driverBuilder = RemoteWebDriver.builder().oneOf(capabilities);
if (!isLocalBrowser()) {
logger.info(() -> "Starting Remote WebDriver session...");
driverBuilder.address(getServiceUrl());
} else {
logger.info(() -> "Starting Local WebDriver session...");
}

driver = driverBuilder.build();
} catch (Exception e) {
throw new DriverSetupException("Driver was not setup properly.", e);
}
RemoteWebDriverBuilder driverBuilder = RemoteWebDriver.builder().oneOf(capabilities);
if (!isLocalBrowser()) {
logger.info(() -> "Starting Remote WebDriver session...");
driverBuilder.address(getServiceUrl());
} else {
logger.info(() -> "Starting Local WebDriver session...");
}

return driver;
return driverBuilder.build();
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.listener;

import io.github.selcukes.core.driver.DriverManager;
import io.github.selcukes.core.driver.GridRunner;
import lombok.CustomLog;
import org.testng.ISuite;
import org.testng.ISuiteListener;

@CustomLog
public class TestLifecycle implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
logger.debug(() -> "Test Suite Execution started...");
}

@Override
public void onFinish(ISuite suite) {
DriverManager.removeAllDrivers();
GridRunner.stopAppium();
logger.debug(() -> "Test Suite Execution finished...");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
import org.testng.ITestClass;

@CustomLog
public class ClassResourceListener implements IClassListener {
public class TestLifecyclePerClass extends TestLifecycle implements IClassListener {
@Override
public void onBeforeClass(ITestClass testClass) {
logger.info(() -> "Before Class of " + testClass.getName());
logger.debug(() -> "Before Class of " + testClass.getClass().getSimpleName());
}

@Override
public void onAfterClass(ITestClass testClass) {
logger.info(() -> "After Class of " + testClass.getName());
logger.info(() -> "Cleanup Resource");
logger.debug(() -> "After Class of " + testClass.getClass().getSimpleName());
logger.debug(() -> "Cleanup Resource");
DriverManager.removeDriver();
ConfigFactory.cleanupConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.testng.ITestResult;

@CustomLog
public class MethodResourceListener implements IInvokedMethodListener {
public class TestLifecyclePerMethod extends TestLifecycle implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
logger.debug(() -> "Before invocation of " + method.getTestMethod().getMethodName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.github.selcukes.commons.config.ConfigFactory;
import io.github.selcukes.core.driver.GridRunner;
import io.github.selcukes.core.listener.MethodResourceListener;
import io.github.selcukes.core.listener.TestLifecyclePerMethod;
import io.github.selcukes.core.page.Pages;
import io.github.selcukes.wdb.enums.DriverType;
import lombok.CustomLog;
Expand All @@ -28,7 +28,7 @@
import org.testng.annotations.Test;

@CustomLog
@Listeners(MethodResourceListener.class)
@Listeners(TestLifecyclePerMethod.class)
public class ClassicGridTest {
@BeforeSuite
public static void beforeSuite() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package io.github.selcukes.core.tests.web;

import io.github.selcukes.core.listener.MethodResourceListener;
import io.github.selcukes.core.listener.TestLifecyclePerMethod;
import io.github.selcukes.core.page.Pages;
import io.github.selcukes.core.page.WebPage;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MethodResourceListener.class)
@Listeners(TestLifecyclePerMethod.class)
public class PrintTest {
WebPage page;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package io.github.selcukes.core.tests.web;

import io.github.selcukes.core.listener.MethodResourceListener;
import io.github.selcukes.core.listener.TestLifecyclePerMethod;
import io.github.selcukes.core.page.Pages;
import io.github.selcukes.core.page.WebPage;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(MethodResourceListener.class)
@Listeners(TestLifecyclePerMethod.class)
public class WebTest {
WebPage page;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,17 @@

package io.github.selcukes.core.tests.win;

import io.github.selcukes.core.driver.DriverManager;
import io.github.selcukes.core.driver.GridRunner;
import io.github.selcukes.core.listener.TestLifecyclePerClass;
import io.github.selcukes.core.page.Pages;
import io.github.selcukes.core.page.WinPage;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(TestLifecyclePerClass.class)
public class NotepadTest {

@BeforeMethod
public void beforeTest() {
GridRunner.startAppium();
}

@Test(enabled = false)
public void notepadTest() {

Expand All @@ -47,9 +41,4 @@ public void notepadTest() {
.click(By.name("Don't Save"));
}

@AfterMethod
public void afterTest() {
DriverManager.removeDriver();
GridRunner.stopAppium();
}
}
2 changes: 1 addition & 1 deletion webdriver-binaries/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</dependencies>
</project>