Created
June 11, 2021 21:39
-
-
Save Jaimin180296/d3724dfc6e8b658ade3dbac2d9b14c32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.firefox.FirefoxProfile; | |
import org.openqa.selenium.firefox.FirefoxDriver; | |
import org.openqa.selenium.JavascriptExecutor; | |
import java.io.FileOutputStream; | |
import java.io.OutputStream; | |
import java.util.Base64; | |
import java.net.URL; | |
public class script { | |
public static final String USERNAME = "<username>"; | |
public static final String AUTOMATE_KEY = "<accesskey>"; | |
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; | |
public static void main(String[] args) throws Exception { | |
FirefoxProfile profile = new FirefoxProfile(); | |
profile.setPreference("browser.download.folderList", 1); | |
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("browser", "Firefox"); | |
caps.setCapability("browser_version", "latest"); | |
caps.setCapability("os", "Windows"); | |
caps.setCapability("os_version", "10"); | |
caps.setCapability("name", "Bstack-[Java] Sample file download"); | |
caps.setCapability(FirefoxDriver.PROFILE, profile); | |
WebDriver driver = new RemoteWebDriver(new URL(URL), caps); | |
JavascriptExecutor jse = (JavascriptExecutor) driver; | |
// Navigate to the link | |
driver.get("https://www.browserstack.com/test-on-the-right-mobile-devices"); | |
Thread.sleep(2000); | |
// Accept the cookie popup | |
WebElement pop = driver.findElement(By.id("accept-cookie-notification")); | |
pop.click(); | |
// Find element by class name and store in variable "Element" | |
WebElement Element = driver.findElement(By.className("icon-csv")); | |
// This will scroll the page till the element is found | |
jse.executeScript("arguments[0].scrollIntoView();", Element); | |
jse.executeScript("window.scrollBy(0,-100)"); | |
Thread.sleep(1000); | |
// Click on the element to download the file | |
Element.click(); | |
Thread.sleep(2000); | |
// Check if file exists | |
System.out.println(jse.executeScript("browserstack_executor: {\"action\": \"fileExists\", \"arguments\": {\"fileName\": \"BrowserStack - List of devices to test on.csv\"}}")); | |
driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment