Hi All,
My company wants to execute the test cases of Web Automation and Mobile automation from single Framework, currently the framework I am referring can handle only Android and IOS automation, can anyone please suggest any hints how to initiate web driver test cases also. I am new to this , Below are my sample files
Capabilities Manager
package utilities;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
public class CapabilitiesManager {
//GenericUtility utils = new GenericUtility();
public DesiredCapabilities getCaps() throws IOException {
GlobalParams params = new GlobalParams();
Properties props = new PropertyManager().getProps();
try{
//utils.log().info("getting capabilities");
System.out.println("getting capabilities");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, params.getPlatformName());
caps.setCapability(MobileCapabilityType.UDID, params.getUDID());
caps.setCapability(MobileCapabilityType.DEVICE_NAME, params.getDeviceName());
switch(params.getPlatformName()){
case "Android":
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, props.getProperty("androidAutomationName"));
caps.setCapability("appPackage", props.getProperty("androidAppPackage"));
caps.setCapability("appActivity", props.getProperty("androidAppActivity"));
caps.setCapability("appWaitforLaunch", "false");
caps.setCapability("adbExecTimeout", props.getProperty("adbExecTimeout"));
caps.setCapability("fullReset", "true");
caps.setCapability("systemPort", params.getSystemPort());
caps.setCapability("chromeDriverPort", params.getChromeDriverPort());
//String chromeDriverPath = System.getProperty("user.dir") + "\\src\\main\\resources\\chromedriver.exe";
//caps.setCapability("chromedriverExecutable", chromeDriverPath);
//String androidAppUrl = getClass().getResource(props.getProperty("androidAppLocation")).getFile();
String androidAppUrl = "/Users/avishsharma/Downloads/Reapp.apk";//
//utils.log().info("appUrl is" + androidAppUrl);
caps.setCapability("app", androidAppUrl);
break;
case "iOS":
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, props.getProperty("iOSAutomationName"));
//String iOSAppUrl = getClass().getResource(props.getProperty("iOSAppLocation")).getFile();
String iOSAppUrl="/Users/avishsharma/Desktop/latest/Instore_Sales.app";
System.out.println("appUrl is" + iOSAppUrl);
//caps.setCapability("fullReset", "true");
//utils.log().info("appUrl is" + iOSAppUrl);
caps.setCapability("bundleId", props.getProperty("iOSBundleId"));
caps.setCapability("includeSafariInWebviews",true);
caps.setCapability("webviewConnectTimeout", "90000");
//caps.setCapability("app", iOSAppUrl);
//caps.setCapability("wdaLocalPort", params.getWdaLocalPort());
//caps.setCapability("webkitDebugProxyPort", params.getWebkitDebugProxyPort());
//caps.setCapability("app", iOSAppUrl);
break;
}
return caps;
} catch(Exception e){
e.printStackTrace();
//utils.log().fatal("Failed to load capabilities. ABORT!!" + e.toString());
throw e;
}
}
}
Driver Manager
package utilities;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import java.io.IOException;
public class DriverManager {
private static ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();
GlobalParams params = new GlobalParams();
PropertyManager props = new PropertyManager();
//GenericUtility utils = new GenericUtility();
public AppiumDriver getDriver() {
return driver.get();
}
public void setDriver(AppiumDriver driver2) {
driver.set(driver2);
}
public void initializeDriver() throws Exception {
AppiumDriver driver = null;
GlobalParams params = new GlobalParams();
PropertyManager props = new PropertyManager();
if (driver == null) {
try {
// utils.log().info("initializing Appium driver");
switch (params.getPlatformName()) {
case "Android":
driver = new AndroidDriver(new ServerManager().getServer().getUrl(), new CapabilitiesManager().getCaps());
break;
case "iOS":
driver = new IOSDriver(new ServerManager().getServer().getUrl(), new CapabilitiesManager().getCaps());
break;
}
if (driver == null) {
throw new Exception("driver is null. ABORT!!!");
}
// utils.log().info("Driver is initialized");
this.driver.set(driver);
} catch (IOException e) {
e.printStackTrace();
//utils.log().fatal("Driver initialization failure. ABORT !!!!" + e.toString());
throw e;
}
}
}
}
Server Manager
package utilities;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;
import java.io.File;
import java.util.HashMap;
public class ServerManager {
private static ThreadLocal server = new ThreadLocal<>();
// GenericUtility utils = new GenericUtility();
public AppiumDriverLocalService getServer(){
return server.get();
}
public void startServer(){
// utils.log().info("starting appium server");
System.out.println("starting appium server");
AppiumDriverLocalService server = MacGetAppiumService();
server.start();
if(server == null || !server.isRunning()){
//utils.log().fatal("Appium server not started. ABORT!!!");
System.out.println("Appium server not started. ABORT!!!");
throw new AppiumServerHasNotBeenStartedLocallyException("Appium server not started. ABORT!");
}
server.clearOutPutStreams(); //it will o/p server logs in console windows
this.server.set(server); //we need to assign server obj to our threadlocal object
System.out.println("Appium server started");//utils.log().info("Appium server started");
}
public AppiumDriverLocalService getAppiumServerDefault() {
return AppiumDriverLocalService.buildDefaultService();
}
//creating new server instance for each mobile devices
public AppiumDriverLocalService WindowsGetAppiumService() {
GlobalParams params = new GlobalParams();
return AppiumDriverLocalService.buildService(new AppiumServiceBuilder().withArgument(() -> “–base-path”, “/wd/hub”)
.usingAnyFreePort().withArgument(() -> “–allow-insecure”,“chromedriver_autodownload”)
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File(params.getPlatformName() + “_”
+ params.getDeviceName() + File.separator + “Server.log”)));//saving server logs
}
public AppiumDriverLocalService MacGetAppiumService() {
GlobalParams params = new GlobalParams();
HashMap<String, String> environment = new HashMap<String, String>();
environment.put("PATH", "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:/Users/avishsharma/Library/Android/sdk/tools:/Users/avishsharma/Library/Android/sdk/platform-tools:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin" + System.getenv("PATH"));
environment.put("ANDROID_HOME", "/Users/avishsharma/Library/Android/sdk");
environment.put("JAVA_HOME", "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home");
return AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
.usingDriverExecutable(new File("/usr/local/bin/node"))
.withAppiumJS(new File("/usr/local/lib/node_modules/appium/build/lib/main.js"))
.usingAnyFreePort()
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withEnvironment(environment));
//.withLogFile(new File(params.getPlatformName() + "_"
// + params.getDeviceName() + File.separator + "Server.log")));
}
}
Global Parameters
package utilities;
public class GlobalParams {
private static ThreadLocal<String> platformName = new ThreadLocal<String>();
private static ThreadLocal<String> udid = new ThreadLocal<String>();
private static ThreadLocal<String> deviceName = new ThreadLocal<String>();
private static ThreadLocal<String> systemPort = new ThreadLocal<String>();
private static ThreadLocal<String> chromeDriverPort = new ThreadLocal<String>();
private static ThreadLocal<String> wdaLocalPort = new ThreadLocal<String>();
private static ThreadLocal<String> webkitDebugProxyPort = new ThreadLocal<String>();
public void setPlatformName(String platformName1){
platformName.set(platformName1);
}
public String getPlatformName(){
return platformName.get();
}
public String getUDID() {
return udid.get();
}
public void setUDID(String udid2) {
udid.set(udid2);
}
public String getDeviceName() {
return deviceName.get();
}
public void setDeviceName(String deviceName2) {
deviceName.set(deviceName2);
}
public String getSystemPort() {
return systemPort.get();
}
public void setSystemPort(String systemPort2) {
systemPort.set(systemPort2);
}
public String getChromeDriverPort() {
return chromeDriverPort.get();
}
public void setChromeDriverPort(String chromeDriverPort2) {
chromeDriverPort.set(chromeDriverPort2);
}
public String getWdaLocalPort() {
return wdaLocalPort.get();
}
public void setWdaLocalPort(String wdaLocalPort2) {
wdaLocalPort.set(wdaLocalPort2);
}
public String getWebkitDebugProxyPort() {
return webkitDebugProxyPort.get();
}
public void setWebkitDebugProxyPort(String webkitDebugProxyPort2) {
webkitDebugProxyPort.set(webkitDebugProxyPort2);
}
//@Parameters({“androidEmulatorudid”,“androidDeviceName”,“androidDeviceName1”,“androidEmulatorudid1”})
public void initializeGlobalParams(){
GlobalParams params = new GlobalParams();
params.setPlatformName(System.getProperty(“platformName”, “iOS”));
params.setUDID(System.getProperty(“IOSEmulatorudid”,“912F76BD-52A8-4D10-B179-3FCF36BD9FB6”));
params.setDeviceName(System.getProperty(“IOSDeviceName”, “iPad”));
switch(params.getPlatformName()){
case "Android":
//params.setSystemPort(System.getProperty("systemPort", "1003"));
//params.setChromeDriverPort(System.getProperty("chromeDriverPort", "11000"));
break;
case "iOS":
//params.setWdaLocalPort(System.getProperty("wdaLocalPort", "10001"));
//params.setWebkitDebugProxyPort(System.getProperty("webkitDebugProxyPort", "11001"));
break;
default:
throw new IllegalStateException("Invalid Platform Name!");
}
}
}
Property Manager
package utilities;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyManager {
private static Properties props = new Properties();
//GenericUtility utils = new GenericUtility();
public Properties getProps() throws IOException {
InputStream is = null;
String propsFileName = "config.properties";
if(props.isEmpty()){
try{
// utils.log().info("I am loading the config properties");
System.out.println("loading config properties");
is = getClass().getClassLoader().getResourceAsStream(propsFileName);
props.load(is);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Failed to load config properties. ABORT!!" + e.toString());
// utils.log().fatal("Failed to load config properties. ABORT!!" + e.toString());
throw e;
} finally {
if(is != null){
is.close();
}
}
}
return props;
}
}
Application Hooks
package stepdefinations;
import base.TestDriverSession;
import io.appium.java_client.AppiumDriver;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import utilities.DriverManager;
import utilities.GlobalParams;
import utilities.ServerManager;
import java.util.Properties;
public class ApplicationHooks {
public static AppiumDriver driver;
Properties properties;
public TestDriverSession dsession;
@Before
public void initialize(Scenario scenario) throws Exception {
/* //dsession=new TestDriverSession();
//driver=dsession.initializeDriver("iOS");
System.out.println("print driver from hooks file :"+driver);
System.out.println("hooks started");
System.out.println("scenario started is :"+scenario.getName());
*/
GlobalParams params=new GlobalParams();
params.initializeGlobalParams();
new ServerManager().startServer();
new DriverManager().initializeDriver();
}
@After
public void quit() {
DriverManager driverManager=new DriverManager();
if(driverManager.getDriver()!=null)
{
driverManager.getDriver().quit();
driverManager.setDriver(null);
}
ServerManager serverManager=new ServerManager();
if(serverManager.getServer()!=null)
{
serverManager.getServer().stop();
}
}
}
config properties file
#Test Environment, useful in reading OTP
testEnvironment=SIT
#appium server
#platform name (Android or IOS)
platformName=iOS
IOSDeviceName=iPad
IOSEmulatorudid=912F76BD-52A8-4D10-B179-3FCF36BD9FB6
iOSAutomationName=XCUITest
iOSBundleId=com.xyz.instoresales
iOSAppLocation=/Users/avishsharma/Desktop/latest/Instore_Sales.app
#IOSVersion=10.0.0
#server command time out
appiumNewCommandTimeout=180
Can anyone please help me out to suggest me where I can initialise the selenium web driver in a way that I can execute the test cases on Desktop browser also not in a mobile browser.
Any GitHub repository link would be also helpful where selenium and appium integration would be implemented or any suggestions on the above files.
This is a blocker for me and I will be highly appreciated if anyone can help me out on this.
Thanks