File download with headless automation

1_aoUGQKFGoplkAYSbPv8i5g

      In selenium headless automation, we had a scenario to download a file and verify the file content. We tried with chrome browser as headless for execution but failed to download the file into a respective path mentioned in the ChromeOptions via setExperimentalOption. 

     Finally, we derived a solution to download the file into respective path when the application under headless execution. We used to send the Page.setDownloadBehavior value as a request. This request needs to execute after the headless driver instantiation. Following is the sample code snippet which helped us to solve the file download issue under headless automation execution,

String dowloadPath = “directory path to your file to download”;

System.setProperty(“webdriver.chrome.driver”, “path to chromedriver.exe”);

ChromeOptions options = new ChromeOptions();
options.addArguments(“–test-type”);
options.addArguments(“–headless”);
options.addArguments(“–disable-extensions”);
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, options);

Map<String, Object> commandParams = new HashMap<>();
commandParams.put(“cmd”, “Page.setDownloadBehavior”);
Map<String, String> params = new HashMap<>();
params.put(“behavior”, “allow”);
params.put(“downloadPath”, dowloadPath);
commandParams.put(“params”, params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + “/session/” + driver.getSessionId() + “/chromium/send_command”;
HttpPost request = new HttpPost(u);
request.addHeader(“content-type”, “application/json”);
request.setEntity(new StringEntity(command));
httpClient.execute(request);

     Try to use the above code snippet if you are facing the file download issue during headless automation execution.

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 !

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 !