Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.9k views
by

1 Answer

0 like 0 dislike
by

I have tried this earlier using the grid. Usually we used to have a location to the file be known. So, we login via FTP or someother way and download the file once it is downloaded and use it.

Fore browserstack you can try following this,

To download files through the test files, you need to create a profile capability containing all the necessary parameters, and then associate it with the WebDriver.

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 0);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
/* you will need to find the content-type of your app and set it here. */
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", "firefox");
caps.setCapability("name", "Bstack-[Java] Download Test");
caps.setCapability("browserstack.debug", "true");
caps.setCapability(FirefoxDriver.PROFILE, profile);

WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.get("https://rubygems.org/gems/selenium-webdriver");
WebElement element = driver.findElement(By.id("download"));
element.click();

Thread.sleep(50000);

Downloaded File Interaction

You can use JavascriptExecutor to send javascript commands in your test, the JavascriptExecutor performs actions in the browser you are using for your test. BrowserStack has developed custom JavascriptExecutor methods which makes testing file download functionality very easy.

The syntax of using custom BrowserStack JavascriptExecutor method is

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("browserstack_executor: {\"action\": \"<action name>\", \"arguments\": {<has of arguments>}}")

JavascriptExecutor to check if the file got downloaded:

// check if file exists
System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"fileExists\", \"arguments\": {\"fileName\": \"karma-browserstack-launcher-0.1.5.zip\"}}"));

JavascriptExecutor to get the file properties. It returns the hash of file properties:

// get file properties
System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"getFileProperties\", \"arguments\": {\"fileName\": \"karma-browserstack-launcher-0.1.5.zip\"}}"));

JavascriptExecutor to get the content of the file. The executor returns the content with Base64 encoding, we need to decode it. It saves the file in your local machine which runs the Selenium test script:

// Download file
String base64EncodedFile = (String)jse.executeScript("browserstack_executor: {\"action\": \"getFileContent\", \"arguments\": {\"fileName\": \"karma-browserstack-launcher-0.1.5.zip\"}}");
byte[] data = Base64.getDecoder().decode(base64EncodedFile);
OutputStream stream = new FileOutputStream("karma-browserstack-launcher-0.1.5.zip");
stream.write(data);

src: https://www.browserstack.com/automate/java#enhancements-uploads-downloads


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...