Skip to content

Commit

Permalink
Added webdriver config for Android and IOS
Browse files Browse the repository at this point in the history
  • Loading branch information
martingrossmann committed Jul 18, 2024
1 parent a86717d commit 9d83416
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class Browsers {
public static final String windows = "windows";
public static final String mobile_chrome = "mobile_chrome";
public static final String mobile_safari = "mobile_safari";

// Value for app tests
public static final String android = "android";
public static final String ios = "ios";

// Default value for AppiumDriverRequest
public static final String mobile = "mobile";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Testerra
*
* (C) 2024, Martin Großmann, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you 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 eu.tsystems.mms.tic.testframework.mobile.driver;

import eu.tsystems.mms.tic.testframework.useragents.UserAgentConfig;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.ios.options.XCUITestOptions;

/**
* Created on 2024-07-18
*
* @author mgn
*/
public interface AndroidConfig extends UserAgentConfig<UiAutomator2Options> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import eu.tsystems.mms.tic.testframework.pageobjects.internal.core.GuiElementData;
import eu.tsystems.mms.tic.testframework.report.model.context.SessionContext;
import eu.tsystems.mms.tic.testframework.report.utils.IExecutionContextController;
import eu.tsystems.mms.tic.testframework.testing.WebDriverManagerProvider;
import eu.tsystems.mms.tic.testframework.utils.AppiumProperties;
import eu.tsystems.mms.tic.testframework.utils.TimerUtils;
import eu.tsystems.mms.tic.testframework.webdriver.WebDriverFactory;
Expand All @@ -48,11 +49,12 @@
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.remote.MobileBrowserType;
import io.appium.java_client.remote.options.BaseOptions;
import io.appium.java_client.safari.options.SafariOptions;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.support.events.EventFiringDecorator;
import org.testng.TestException;
Expand All @@ -69,7 +71,11 @@
*
* @author Eric Kubenka
*/
public class AppiumDriverFactory implements WebDriverFactory, Loggable, AppiumCapabilityHelper {
public class AppiumDriverFactory implements
WebDriverFactory,
Loggable,
AppiumCapabilityHelper,
WebDriverManagerProvider {

@Override
public WebDriverRequest prepareWebDriverRequest(WebDriverRequest webDriverRequest) {
Expand All @@ -83,9 +89,6 @@ public WebDriverRequest prepareWebDriverRequest(WebDriverRequest webDriverReques
finalRequest.setBrowser(webDriverRequest.getBrowser());
}

MutableCapabilities requestCapabilities = finalRequest.getMutableCapabilities();
IExecutionContextController executionContext = Testerra.getInjector().getInstance(IExecutionContextController.class);

Platform platform = new MobileOsChecker().getPlatform(webDriverRequest);
Capabilities userAgentCapabilities = null;

Expand All @@ -96,7 +99,15 @@ public WebDriverRequest prepareWebDriverRequest(WebDriverRequest webDriverReques
UiAutomator2Options androidOptions = new UiAutomator2Options();
if (Browsers.mobile_chrome.equals(webDriverRequest.getBrowser())) {
androidOptions.setCapability(CapabilityType.BROWSER_NAME, MobileBrowserType.CHROME);
ChromeOptions chromeOptions = new ChromeOptions();
WEB_DRIVER_MANAGER.getUserAgentConfig(Browsers.mobile_chrome)
.ifPresent(userAgentConfig -> userAgentConfig.configure(chromeOptions));
androidOptions = androidOptions.merge(chromeOptions);
}
UiAutomator2Options customAndroidOptions = new UiAutomator2Options();
WEB_DRIVER_MANAGER.getUserAgentConfig(Browsers.android)
.ifPresent(userAgentConfig -> userAgentConfig.configure(customAndroidOptions));
androidOptions = androidOptions.merge(customAndroidOptions);
userAgentCapabilities = androidOptions;
break;
}
Expand All @@ -105,7 +116,16 @@ public WebDriverRequest prepareWebDriverRequest(WebDriverRequest webDriverReques
XCUITestOptions iosOptions = new XCUITestOptions();
if (Browsers.mobile_safari.equals(webDriverRequest.getBrowser())) {
iosOptions.setCapability(CapabilityType.BROWSER_NAME, MobileBrowserType.SAFARI);
SafariOptions safariOptions = new SafariOptions();

WEB_DRIVER_MANAGER.getUserAgentConfig(Browsers.mobile_safari)
.ifPresent(userAgentConfig -> userAgentConfig.configure(safariOptions));
iosOptions = iosOptions.merge(safariOptions);
}
XCUITestOptions customIosOptions = new XCUITestOptions();
WEB_DRIVER_MANAGER.getUserAgentConfig(Browsers.ios)
.ifPresent(userAgentConfig -> userAgentConfig.configure(customIosOptions));
iosOptions = iosOptions.merge(customIosOptions);
userAgentCapabilities = iosOptions;
break;
}
Expand Down Expand Up @@ -133,6 +153,7 @@ public WebDriverRequest prepareWebDriverRequest(WebDriverRequest webDriverReques
}
}

IExecutionContextController executionContext = Testerra.getInjector().getInstance(IExecutionContextController.class);
otherOptions.setCapability(APPIUM_CAPABILITY_NAME_TEST_NAME, executionContext.getExecutionContext().getRunConfig().getReportName());

// TODO: Handle app capabilities from test.properties
Expand Down Expand Up @@ -239,7 +260,13 @@ public WebDriver setupNewWebDriverSession(WebDriver webDriver, SessionContext se

@Override
public List<String> getSupportedBrowsers() {
return Arrays.asList(Browsers.mobile_chrome, Browsers.mobile_safari, Browsers.mobile);
return Arrays.asList(
Browsers.mobile_chrome,
Browsers.mobile_safari,
Browsers.mobile,
Browsers.android,
Browsers.ios
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Testerra
*
* (C) 2024, Martin Großmann, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you 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 eu.tsystems.mms.tic.testframework.mobile.driver;

import eu.tsystems.mms.tic.testframework.useragents.UserAgentConfig;
import io.appium.java_client.ios.options.XCUITestOptions;

/**
* Created on 2024-07-18
*
* @author mgn
*/
public interface IOSConfig extends UserAgentConfig<XCUITestOptions> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,22 @@ public Platform getPlatform(WebDriver driver) {
}
}

// Returns true if WebDriverRequest contains typical app capabilities
/**
* Returns true if WebDriverRequest contains typical app capabilities.
*
* The method checks all possible caps and there values to find out the platform.
*/
public boolean isAppTest(WebDriverRequest webDriverRequest, Platform platform) {
Capabilities capabilities = webDriverRequest.getCapabilities();
MutableCapabilities mutableCapabilities = ((AbstractWebDriverRequest) webDriverRequest).getMutableCapabilities();

switch (platform) {
case ANDROID:
// should be instance of UiAutomator2Options --> platform already set
return "Espresso".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
return capabilities.getPlatformName() == Platform.ANDROID // if instance of UiAutomator2Options --> platform already set
|| mutableCapabilities.getPlatformName() == Platform.ANDROID
|| webDriverRequest.getBrowser().equalsIgnoreCase(Browsers.android)
|| capabilities.getBrowserName().equalsIgnoreCase(Browsers.android)
|| "Espresso".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
|| "UiAutomator2".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
|| "UiAutomator".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
|| getCap(capabilities, APPIUM_APP_PACKAGE) != null
Expand All @@ -80,8 +87,11 @@ public boolean isAppTest(WebDriverRequest webDriverRequest, Platform platform) {
|| getCap(mutableCapabilities, APPIUM_APP_PACKAGE) != null
|| getCap(mutableCapabilities, APPIUM_APP_ACTIVITY) != null;
case IOS:
// should be instance of XCUITestOptions --> platform already set
return "XCUITest".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
return capabilities.getPlatformName() == Platform.IOS // if instance of XCUITestOptions --> platform already set
|| mutableCapabilities.getPlatformName() == Platform.IOS
|| webDriverRequest.getBrowser().equalsIgnoreCase(Browsers.ios)
|| capabilities.getBrowserName().equalsIgnoreCase(Browsers.ios)
|| "XCUITest".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
|| "UIAutomation".equalsIgnoreCase(getCap(capabilities, APPIUM_AUTOMATION_NAME))
|| getCap(capabilities, APPIUM_BUNDLE_ID) != null
|| "XCUITest".equalsIgnoreCase(getCap(mutableCapabilities, APPIUM_AUTOMATION_NAME))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Testerra
*
* (C) 2024, Martin Großmann, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you 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 eu.tsystems.mms.tic.testframework.mobile.driver;

import eu.tsystems.mms.tic.testframework.useragents.UserAgentConfig;
import io.appium.java_client.safari.options.SafariOptions;

/**
* Created on 2024-07-18
*
* @author mgn
*/
public interface SafariMobileConfig extends UserAgentConfig<SafariOptions> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setDeviceQuery(String deviceQuery) {
}

public String getDeviceQuery() {
return this.getMutableCapabilities().getCapability(APPIUM_DEVICE_QUERY).toString();
return (String) this.getMutableCapabilities().getCapability(APPIUM_DEVICE_QUERY);
}

public void setAccessKey(String accessKey) {
Expand All @@ -72,30 +72,30 @@ public void setAppiumEngine(String engine) {
}

public String getAppiumEngine() {
return this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_AUTOMATION_NAME)).toString();
return (String) this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_AUTOMATION_NAME));
}

public void setDeviceName(String deviceName) {
this.getMutableCapabilities().setCapability(APPIUM_DEVICE_NAME, deviceName);
}

public String getDeviceName() {
return this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_DEVICE_NAME)).toString();
return (String) this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_DEVICE_NAME));
}

public void setPlatformVersion(String platformVersion) {
this.getMutableCapabilities().setCapability(APPIUM_PLATFORM_VERSION, platformVersion);
}

public String getPlatformVersion() {
return this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_PLATFORM_VERSION)).toString();
return (String) this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_PLATFORM_VERSION));
}

public void setDeviceId(String id) {
this.getMutableCapabilities().setCapability(APPIUM_UDID, id);
}

public String getDeviceId() {
return this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_UDID)).toString();
return (String) this.getMutableCapabilities().getCapability(getAppiumCap(APPIUM_UDID));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package eu.tsystems.mms.tic.testframework.mobile.test.apps;

import eu.tsystems.mms.tic.testframework.appium.Browsers;
import eu.tsystems.mms.tic.testframework.mobile.driver.AndroidConfig;
import eu.tsystems.mms.tic.testframework.mobile.driver.IOSConfig;
import eu.tsystems.mms.tic.testframework.mobile.test.AbstractAppiumTest;
import eu.tsystems.mms.tic.testframework.utils.TimerUtils;
import eu.tsystems.mms.tic.testframework.utils.UITestUtils;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
* Created on 2024-07-17
*
* @author mgn
*/
public class TesterraMobileApp2Test extends AbstractAppiumTest {

@BeforeClass
public void initCaps() {
WEB_DRIVER_MANAGER.setUserAgentConfig(Browsers.android, (AndroidConfig) options -> {
options.setApp("cloud:com.telekom.mms.cqa.mdc.androidapp/.HomeActivity");
options.setAppPackage("com.telekom.mms.cqa.mdc.androidapp");
options.setAppActivity(".HomeActivity");
});

WEB_DRIVER_MANAGER.setUserAgentConfig(Browsers.ios, (IOSConfig) options -> {
options.setApp("cloud:com.telekom.mms.cqa.mdc.iosapp");
options.setBundleId("com.telekom.mms.cqa.mdc.iosapp");
});
}

/**
* This test opens the mobile app on iOS and Android.
* <p>
* The platform specific caps are set by the method 'initCaps'.
* The property 'tt.browser' defines which platform should used for test:
* tt.browser=android or tt.browser=ios
* <p>
* If a device query is needed, the properties 'tt.mobile.device.query.ios' and
* 'tt.mobile.device.query.android' are used to select a device from a cloud.
*/
@Test
public void testT01OpenApp() {
WebDriver webDriver = WEB_DRIVER_MANAGER.getWebDriver();
TimerUtils.sleep(4000);
UITestUtils.takeScreenshots();
}

}
Loading

0 comments on commit 9d83416

Please sign in to comment.