Created
October 17, 2018 16:36
-
-
Save ckzgraphics/7ec42d2facbe4a009025001183794c5a to your computer and use it in GitHub Desktop.
This code downloads a file on the BrowserStack machine and uploads the same to verify if the file was downloaded successfully or not
This file contains 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 java.io.File; | |
import java.io.FileReader; | |
import java.net.URL; | |
import org.apache.commons.io.FileUtils; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.OutputType; | |
import org.openqa.selenium.TakesScreenshot; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
import org.openqa.selenium.remote.RemoteWebDriver; | |
public class Verify_Downloaded_File { | |
public static void main(String[] args) { | |
// VALIDATE YOUR BROWSERSTACK ACCOUNT DETAILS HERE - https://www.browserstack.com/accounts/settings | |
String USERNAME = "<BROWSERSTACK_USERNAME>"; | |
String AUTOMATE_KEY = "BROWSERSTACK_ACCESS_KEY"; | |
String HUB_URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub"; | |
String AUT_URL = "https://the-internet.herokuapp.com/download"; | |
String file_name = null; | |
String file_path = null; | |
URL URLObj = null; | |
WebDriver webDriver = null; | |
WebElement webElement = null; | |
DesiredCapabilities caps = null; | |
try { | |
URLObj = new URL(HUB_URL); | |
caps = new DesiredCapabilities(); | |
caps.setCapability("os", "Windows"); | |
caps.setCapability("os_version", "10"); | |
caps.setCapability("browserName", "Chrome"); | |
caps.setCapability("browser_version", "69.0"); | |
caps.setCapability("project", "Test Run"); | |
caps.setCapability("build", "Automate Feature"); | |
caps.setCapability("name", "Test: Upload the newly downloaded file"); | |
caps.setCapability("browserstack.console", "verbose"); | |
caps.setCapability("browserstack.debug", "true"); | |
webDriver = new RemoteWebDriver(URLObj, caps); | |
webDriver.get(AUT_URL); | |
webDriver.manage().window().maximize(); | |
Thread.sleep(7000); | |
// CODE TO ENSURE ONLY A NON-EXE FILE IS DOWNLOADED | |
webElement = webDriver.findElement(By.xpath(".//div[@class='example']/a[1]")); | |
if(webElement.getText().contains(".exe")){ | |
webElement = webDriver.findElement(By.xpath(".//div[@class='example']/a[2]")); | |
if(webElement.getText().contains(".exe")){ | |
webElement = webDriver.findElement(By.xpath(".//div[@class='example']/a[3]")); | |
if(webElement.getText().contains(".exe")){ | |
webElement = webDriver.findElement(By.xpath(".//div[@class='example']/a[4]")); | |
if(webElement.getText().contains(".exe")){ | |
webElement = webDriver.findElement(By.xpath(".//div[@class='example']/a[5]")); | |
} | |
} | |
} | |
} | |
file_name = webElement.getText().trim(); | |
// FOR WINDOWS 10/8.1/8/7 | |
file_path = "C:\\Users\\hello\\Downloads\\" + file_name; | |
System.out.println("#" + file_path + "#"); | |
// FOR WINDOWS XP | |
// file_path = "C:\\Documents and Settings\\hello\\Downloads\\" + file_name; | |
// FOR OS X | |
// file_path = "/Users/test1/Downloads/" + file_name); | |
webElement.click(); | |
Thread.sleep(10000); | |
take_screenshot(webDriver); | |
AUT_URL = "https://the-internet.herokuapp.com/upload"; | |
webDriver.get(AUT_URL); | |
Thread.sleep(5000); | |
webDriver.findElement(By.xpath(".//input[@id='file-upload']")).sendKeys(file_path); | |
webDriver.findElement(By.xpath(".//input[@id='file-submit']")).click(); | |
Thread.sleep(4000); | |
take_screenshot(webDriver); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if(webDriver != null){ | |
webDriver.quit(); | |
} | |
} | |
} // MAIN END | |
public static void take_screenshot(WebDriver webDriver){ | |
try { | |
TakesScreenshot scrShot =((TakesScreenshot)webDriver); | |
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); | |
// File DestFile=new File("/Users/test.png"); | |
// FileUtils.copyFile(SrcFile, DestFile); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} // FUNC END | |
} // CLASS END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment