How to fetch DRIVER_EXE_PROPERTY for different browsers dynamically?

1_6KbvW_GQYI9u6Cc_S2AHLQ@2x

   In case of Selenium Automation for Web application, most of the time people needs to set DRIVER_EXE_PROPERTY in the code level prior to driver instantiation. For that we need to download the respective DRIVER_EXE and set the path of the DRIVER_EXE in the code.

  Here my piece of code explain how to fetch DRIVER_EXE_PROPERTY automatically while script execution for different browsers. I have done it for chrome, Firefox and IE in Windows system as well as chrome and Firefox in MAC system. Following are the details:

  Below code is necessary to instantiate chrome driver session. In this code I used  getWebAutomationDriver(browserName) to get the DRIVER_EXE path from the system.

System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, getWebAutomationDriver(browserName));
ChromeDriverService chromeservice = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(chromeservice); 

Following is the explanation for getWebAutomationDriver(browserName) method and there again we called  getActualDriverPath(browserName) as a private method to get the actual DRIVER_EXE path,

public static String getWebAutomationDriver(String browserName) throws InterruptedException {
String driverPath = null;
final String operatingSystem = System.getProperty(“os.name“);
try {
if (getActualDriverPath(browserName) == null) {
if (operatingSystem.contains(“Windows”)) {
if (browserName.equalsIgnoreCase(“chrome”)) {
System.out.println(
“chromedriver not installed on system, go to command-line and enter ‘npm install chromedriver’. \nAlso set PATH variable ‘C:\\Users\\…..\\node_modules\\chromedriver\\lib\\chromedriver’ in your system.\nOnce it done, you may need to restart your system”);
System.exit(0);
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
System.out.println(
“geckodriver not installed on system, go to command-line and enter ‘npm install geckodriver’. \nAlso set PATH variable ‘C:\\Users\\…..\\node_modules\\geckodriver’ in your system.\nOnce it done, you may need to restart your system”);
System.exit(0);
}
if (browserName.equalsIgnoreCase(“IE”) || browserName.equalsIgnoreCase(“Internet Explorer”)
|| browserName.equalsIgnoreCase(“InternetExplorer”)) {
System.out.println(
“iedriver not installed on system, go to command-line and enter ‘npm i iedriver@3.7.0’. \nAlso set PATH variable ‘C:\\Users\\…..\\node_modules\\iedriver\\lib\\iedriver64’ in your system.\nOnce it done, you may need to restart your system”);
System.exit(0);
}
} else {
if (browserName.equalsIgnoreCase(“chrome”)) {
System.out.println(
“chromedriver not installed on MAC, go to terminal and enter ‘brew install chromedriver’. \nAlso set PATH variable in Run Configurations of your project. \nSteps: Right click on your project > Run As > Run Configurations > Environment tab”);
System.exit(0);
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
System.out.println(
“geckodriver not installed on MAC, go to terminal and enter ‘brew install geckodriver’. \nAlso set PATH variable in Run Configurations of your project. \nSteps: Right click on your project > Run As > Run Configurations > Environment tab”);
System.exit(0);
}
}
} else {
driverPath = getActualDriverPath(browserName);
}
} catch (Exception e) {
e.printStackTrace();
}
return driverPath;
}

    Following is the explanation for getActualDriverPath(browserName) to fetch the actual DRIVER_EXE path,

private static String getActualDriverPath(String browserName) {
String actualDriverPath = null;
try {
String command = null;
final String operatingSystem = System.getProperty(“os.name“);
if (operatingSystem.contains(“Windows”)) {
if (browserName.equalsIgnoreCase(“chrome”)) {
command = “where ” + “chromedriver”;
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
command = “where ” + “geckodriver”;
}
if (browserName.equalsIgnoreCase(“IE”) || browserName.equalsIgnoreCase(“Internet Explorer”)
|| browserName.equalsIgnoreCase(“InternetExplorer”)) {
command = “where ” + “IEDriverServer”;
}
} else {
if (browserName.equalsIgnoreCase(“chrome”)) {
command = “which ” + “chromedriver”;
}
if (browserName.equalsIgnoreCase(“Firefox”)) {
command = “which ” + “geckodriver”;
}
}
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = “”;
while ((line = reader.readLine()) != null) {
actualDriverPath = line;
break;
}
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
return actualDriverPath;
}

   Try to use above methods to get the DRIVER_EXE path automatically into your automation script.

make it perfect !

Different Driver Services in Selenium and Appium

     mobile-application-testing-with-selenium-and-appium

     As we know that the importance of Automation is increasing day-by-day. And also most of them are looking for open source tools like Selenium and Appium. In 2017, there are some modification happened in Driver instantiation for Selenium Drivers and Appium Drivers. They are commonly using DriverServices to build and start the services for different automation sessions. Here I would like to explain important Driver Services from Selenium and Appium.

For Internet Explorer:
WebDriver driver;
InternetExplorerDriverService service = new InternetExplorerDriverService.Builder().usingDriverExecutable(new File(<path to IEdriver executable>)).usingAnyFeePort().withLogFile(new File(System.getProperty(“userdir”)+”\\Logs\\Browser_logs\\InterntExplorer_”+getCurrentDateAndTime()+”.txt”)).withLogLevel(InternetExploreDriverLogLevel.DEBUG).build();
driver = new InternetExplorerDriver(service);

For Chrome:

WebDriver driver;
ChromeDriverService chromeservices = new ChromeDriverService.Builder().usingDriverExecutable(new File(<path to chromedriver executable>)).usingAnyFreePort().withLogFile(new File(System.getProperty(“userdir”)+”\\Logs\\Browser_logs\\Chrome_”+ getCurrentDateAndTime() + “.txt”)).withVerbose(true).build();
driver = new ChromeDriver(chromeservices);

For Firefox:

WebDriver driver;
FirefoxOptions options = new FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.DEBUG);
GeckoDriverService geckodriverservice = new GeckoDriverService.Builder().usingDriverExecutable(new File(<path to geckodriver executable>)).usingAnyFreePort().withLogFile(new File(System.getProperty(“user.dir”) + “\\Logs\\Browser_logs\\Firefox”+getCurrentDateAndTime() + “.txt”)).build();
driver = new FirefoxDriver(geckodriverservice, options);

For Edge:

WebDriver driver;
EdgeDriverService edgeservice = new EdgeDriverService.Builder().usingDriverExecutable(new File(<path to edgedriver executable>)).usingAnyFreePort().withLogFile(new File(System.getProperty(“user.dir”)+”\\Logs\\Browser_logs\\Edge_”+ getCurrentDateAndTime() + “.txt”)).build();
driver = new EdgeDriver(edgeservice);

For PhantomJS:

WebDriver driver;
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability(“takesScreenshot”, true);
PhantomJSDriverService phantomjsservice = new PhantomJSDriverService.Builder().usingPhantomJSExecutable(new File(<path to phantomjs executable>)).usingAnyFreePort().withLogFile(new File(System.getProperty(“user.dir”)+”\\Logs\\Browser_logs\\PhantomJS_”+ getCurrentDateAndTime() + “.txt”)).build();
driver = new PhantomJSDriver(phantomjsservice, caps);

For Electron Rendered Applications:

WebDriver driver;
ChromeOptions options = new ChromeOptions();
options.setBinary();
ChromeDriverService chromeservices = new ChromeDriverService.Builder().usingDriverExecutable(new File(< path to chromedriver executable>)).usingAnyFreePort().withLogFile(new File(System.getProperty(“user.dir”) +”\\Logs\\Browser_logs\\Electron_”+ getCurrentDateAndTime() + “.txt”)).withVerbose(true).build();
driver = new ChromeDriver(chromeservices, options);

For Safari:

WebDriver driver;
SafariOptions options = new SafariOptions();
options.useCleanSession(true);
driver = new SafariDriver(options);

For Appium:

WebDriver driver;
AppiumDriverLocalService driverService = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
.withAppiumJS(new File()).usingDriverExecutable(new File()).withIPAddress(“127.0.0.1″).usingPort(4723).withArgument(AndroidServerFlag.BOOTSTRAP_PORT_NUMBER,”4724″).withArgument(AndroidServerFlag.CHROME_DRIVER_PORT,”4725”).withLogFile(new File(new File( System.getProperty(“user.dir”) +”\\Appium_logs\\appiumLogs_” + getCurrentDateAndTime() + “.txt”))
.withArgument(GeneralServerFlag.LOG_NO_COLORS));
driverService.start();

For Winium (Windows Desktop Applications):

WebDriver driver;
DesktopOptions options = new DesktopOptions();
options.setApplicationPath(<path to windowsApplication>);
WiniumDriverService service = new WiniumDriverService.Builder().usingDriverExecutable(new File(< path to winiumDriver executable>)).usingPort(9999) .withLogFile(new File(System.getProperty(“user.dir”) + “\\Logs\\Windows_logs\\Win_” + getCurrentDateAndTime() + “.txt”)) .withVerbose(false).withSilent(false).buildDesktopService();
service.start();
driver = new WiniumDriver(service, options);

Try to use above mentioned Driver Services in your automation scripts.

make it perfect !

Appium Desired Capabilities for Android and iOS

Screen-Shot-2016-09-28-at-11.03.39-AM

     Most of the time I observed that people facing the issues at the initial stage of running their automation scripts due to lack of knowledge in the selection of Desired Capabilities based on their Automation platform. Here, I would like to explain important Desired Capabilities for following:

  • Mobile Web Android
  • Mobile Web iOS
  • Mobile Native Android
  • Mobile Native iOS

Mobile Web Android
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “name of your test device”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “OS version of your test device”);
capabilities.setCapability(“udid”, “UDID of your test device”);
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, “Chrome”);

Mobile Web iOS
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “iOS”);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “name of your test device”);
capabilities.setCapability(“udid”, “UDID of your test device”);
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.SAFARI);
capabilities.setCapability(nativeWebTap, true);
capabilities.setCapability(“startIWDP”, true);
capabilities.setCapability(IOSMobileCapabilityType.START_IWDP, true);
capabilities.setCapability(“app”, “path of SAFAILAUNCHERAPPPATH”);
//SAFAILAUNCHERAPPPATH = “/usr/local/lib/node_modules/appium/node_modules/appium-ios-driver/build/SafariLauncher/SafariLauncher.app”
capabilities.setCapability(“automationName”, “XCUITest”);
capabilities.setCapability(“realDeviceLogger”, “path of DEVICECONSOLEPATH”);
//DEVICECONSOLEPATH=”/usr/local/lib/node_modules/deviceconsole/deviceconsole”

Mobile Native Android
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “name of your test device”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “OS version of your test device”);
capabilities.setCapability(MobileCapabilityType.UDID, “UDID of your test device”);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);
capabilities.setCapability(MobileCapabilityType.APP, “location of your .APK”);
capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, “package name of your Android application”);
capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, “activity name of your Android application”);

Mobile Native iOS
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “iOS”);
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, “name of your test device”);
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “OS version of your test device”);
capabilities.setCapability(MobileCapabilityType.UDID, “UDID of your test device”);
capabilities.setCapability(MobileCapabilityType.APP, “location of your .iPA or .APP”);
capabilities.setCapability(IOSMobileCapabilityType.BUNDLE_ID, “bundle ID of your iOS application”);
capabilities.setCapability(“automationName”, “XCUITest”);
capabilities.setCapability(“realDeviceLogger”, “path of DEVICECONSOLEPATH”);
//DEVICECONSOLEPATH=”/usr/local/lib/node_modules/deviceconsole/deviceconsole”

     Once you set your Desired Capabilities, next need to instantiate the drivers for Android and iOS in the following way:

For Android
WebDriver driver;
driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub&#8221;), capabilities)

For iOS
WebDriver driver;
driver = new IOSDriver(new URL(“http://127.0.0.1:4723/wd/hub&#8221;), capabilities)

Try to use above mentioned Desired Capabilities in your automation scripts.

make it perfect !

Selenium for Electron Applications

0_WMsq_uI0pTnauBUs

     Electron allows for the development of desktop GUI applications using front and back end components originally developed for web applications: Node.js run-time for the back-end and Chromium for the front-end.

    Electron uses HTML, JavaScript and CSS to build desktop applications. The vision of same codebase for web, mobile and desktop applications has never been closer.

   There is a small difference in the driver creation section when we compare with normal driver instantiation. Once the driver creation done; rest of the automation script executions are same as Selenium. Here I am explaining the driver creation for old and new selenium versions:

New selenium version(3.7.x) method driver creation in Electron Applications:

WebDriver driver;
System.setProperty(“webdriver.chrome.driver”, “pathTochromedriver”);
ChromeOptions options = new ChromeOptions();
options.setBinary(“pathToElectronApplication”);
ChromeDriverService chromeservices = new ChromeDriverService.Builder().build(); driver = new ChromeDriver(chromeservices, options);

Old selenium version method for driver creation in Electron Applications:

WebDriver driver;
System.setProperty(“webdriver.chrome.driver”, “pathTochromedriver”);
ChromeOptions options = new ChromeOptions();
options.setBinary(“pathToElectronApplication”);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“chromeOptions”, options ); capabilities.setBrowserName(“chrome”);
driver = new ChromeDriver(capabilities);

Shortcut to open inspect mode in Electron Applications,

Windows: Ctrl+Shift+i

MAC: Command+Alt+i

make it perfect !

Selenium Webdriver Instantiation in Selenium(3.7.x)

Capture
In latest selenium 3.7.x version, most of the drivers (Chrome, Firefox, IE, Safari) are deprecated; in the next release of selenium will remove those driver instantiation logic. Following are the new way of driver instantiation in selenium 3.7.x:
For Internet Explorer:
WebDriver driver;
System.setProperty(“webdriver.ie.driver”, <path to IEDriverServer>);
InternetExplorerDriverService ieservice= new InternetExplorerDriverService.Builder().build();
driver = new InternetExplorerDriver(ieservice);
For Chrome:
WebDriver driver;
System.setProperty(“webdriver.chrome.driver”, <path to chromedriver>);
ChromeDriverService chromeservice= new ChromeDriverService.Builder().build();
driver = new ChromeDriver(chromeservice);
For Firefox:
WebDriver driver;
System.setProperty(“webdriver.gecko.driver”, <path to geckodriver>);
GeckoDriverService geckoservice= new GeckoDriverService.Builder().build();
driver = new FirefoxDriver(geckoservice);
For Safari (on MAC)
WebDriver driver;
SafariOptions options = new SafariOptions();
options.useCleanSession(true);
driver = new SafariDriver(options);
Happy Automation Testing !
Make it perfect !