Download file using autoIt and firefoxProfiles(javaScript)
Autoit:
1) Download autoIT and extract
2) In 'SciTE Script Editor' write the below code and save(save with .au3 format) in specific location.
WinWait("[CLASS:#MozillaDialogClass]","",8)
Send("{ALTDOWN}s{ALTUP}")
Sleep(5000)
Send("{ENTER}")
3) Compile the script, and it will generate .exe application.
Eclipse:
public class Download_AutiIt_pass {
WebDriver d=new FirefoxDriver();
@Test
public void downloadAutoIT() throws IOException
{
d.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
d.findElement(By.xpath("//a[text()='Download Text File']")).click();
Runtime.getRuntime().exec("D:\\Automation_workspace\\AutoIT\\download_keyboard_autoIT.exe");
//give the path of .exe(after compile :step 3)
}
}
output:
It will downloads the file
Download file using firefoxProfiles:
selenium webdriver do not have any feature to handle this save file dialogue.
But yes, Selenium webdriver has one more very good feature by which you do not need to handle that dialogue
and you can download any file very easily. We can do It using webdriver's Inbuilt class FirefoxProfile and Its
different methods
Code:
package testcases;
import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;
public class Download_File_pass {
WebDriver d=new FirefoxDriver();
@Test
public void downloadAutoIT() throws IOException, AWTException
{
FirefoxProfile pro=new FirefoxProfile();
pro.setPreference("browser.download.folderList",2);
pro.setPreference("browser.download.manager.showWhenStarting",false);
pro.setPreference("browser.download.dir","D:\\Automation_workspace\\AutoIT\\downloaded_files");
pro.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
WebDriver d=new FirefoxDriver(pro);
d.get("http://www.ox.ac.uk/admissions/graduate/applying-to-oxford/after-you-apply/accepting-your-offer");
d.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
d.manage().window().maximize();
d.findElement(By.xpath("//*[@id='block-ds-extras-oxweb-ds-page-content-right']/div/div/div/div[2]/a")).click();
WebElement element = d.findElement(By.xpath("//*[@id='block-menu-block-9']/h2/a"));
((JavascriptExecutor) d).executeScript("arguments[0].scrollIntoView(true);", element);
d.findElement(By.xpath("//a[text()='Cancellation form']")).click();
}
}
output:
it will download the file
+++++++Other way+++++
package interview;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class Auto_it {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FirefoxProfile profile=new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\selenium_NEW_PRACTICE");// give path where u want to download file
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/x-pdf,text/csv,application/zip");
WebDriver driver=new FirefoxDriver(profile);
driver.get("http://seleniumhq.org/download/");
driver.manage().timeouts().implicitlyWait(9000,TimeUnit.SECONDS);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/p[6]/a")).click();
System.out.println("hiiii");
driver.close();
}
}
+++++Using robot keys++++++++++
driver21.findElement(By.xpath("//*[@id='csvbutton']")).click();
Thread.sleep(20000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_ENTER);
System.out.println("File is downloaded");
Autoit:
1) Download autoIT and extract
2) In 'SciTE Script Editor' write the below code and save(save with .au3 format) in specific location.
WinWait("[CLASS:#MozillaDialogClass]","",8)
Send("{ALTDOWN}s{ALTUP}")
Sleep(5000)
Send("{ENTER}")
3) Compile the script, and it will generate .exe application.
Eclipse:
public class Download_AutiIt_pass {
WebDriver d=new FirefoxDriver();
@Test
public void downloadAutoIT() throws IOException
{
d.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
d.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
d.findElement(By.xpath("//a[text()='Download Text File']")).click();
Runtime.getRuntime().exec("D:\\Automation_workspace\\AutoIT\\download_keyboard_autoIT.exe");
//give the path of .exe(after compile :step 3)
}
}
output:
It will downloads the file
Download file using firefoxProfiles:
selenium webdriver do not have any feature to handle this save file dialogue.
But yes, Selenium webdriver has one more very good feature by which you do not need to handle that dialogue
and you can download any file very easily. We can do It using webdriver's Inbuilt class FirefoxProfile and Its
different methods
Code:
package testcases;
import java.awt.AWTException;
import java.awt.Robot;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;
public class Download_File_pass {
WebDriver d=new FirefoxDriver();
@Test
public void downloadAutoIT() throws IOException, AWTException
{
FirefoxProfile pro=new FirefoxProfile();
pro.setPreference("browser.download.folderList",2);
pro.setPreference("browser.download.manager.showWhenStarting",false);
pro.setPreference("browser.download.dir","D:\\Automation_workspace\\AutoIT\\downloaded_files");
pro.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
WebDriver d=new FirefoxDriver(pro);
d.get("http://www.ox.ac.uk/admissions/graduate/applying-to-oxford/after-you-apply/accepting-your-offer");
d.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
d.manage().window().maximize();
d.findElement(By.xpath("//*[@id='block-ds-extras-oxweb-ds-page-content-right']/div/div/div/div[2]/a")).click();
WebElement element = d.findElement(By.xpath("//*[@id='block-menu-block-9']/h2/a"));
((JavascriptExecutor) d).executeScript("arguments[0].scrollIntoView(true);", element);
d.findElement(By.xpath("//a[text()='Cancellation form']")).click();
}
}
output:
it will download the file
+++++++Other way+++++
package interview;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class Auto_it {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FirefoxProfile profile=new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "D:\\selenium_NEW_PRACTICE");// give path where u want to download file
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/x-pdf,text/csv,application/zip");
WebDriver driver=new FirefoxDriver(profile);
driver.get("http://seleniumhq.org/download/");
driver.manage().timeouts().implicitlyWait(9000,TimeUnit.SECONDS);
driver.findElement(By.xpath("html/body/div[1]/div[2]/div[2]/p[6]/a")).click();
System.out.println("hiiii");
driver.close();
}
}
+++++Using robot keys++++++++++
driver21.findElement(By.xpath("//*[@id='csvbutton']")).click();
Thread.sleep(20000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_ENTER);
System.out.println("File is downloaded");
No comments:
Post a Comment