Automation QA Testing Course Content

File Upload Download and DatePicker Programs

FileUpload using ROBOT class
========================================================
package testngprograms;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;

public class FileUploadTest {
private WebDriver driver=null;
private WebDriverWait wait=null;
private String fpath="D:\\JavaPrograms\\fileprograms_april2018\\fileoutput.doc";
  @Test
  public void uploadFile() throws InterruptedException {
  //open the url
  driver.get("https://www.monsterindia.com/");
  wait.until(ExpectedConditions.titleContains("Job Vacancy - Latest Job Openings - Job Search Online - Monster India"));
  
  //click on upload button
  driver.findElement(By.xpath("//*[contains(@class,'resume-btn btn-orange')]")).click();
 
  Thread.sleep(3000);
  
  //identify the browse button
  WebElement upload_btn=driver.findElement(By.xpath("//form[@name='parsingResponseForm']//div[1]/button"));
 
  Utility.safeJavaScriptClick(driver, upload_btn);
  driver.findElement(By.xpath("//span[@class='browse-text'][contains(.,'Choose file')]")).click();
  Thread.sleep(5000);
  Utility.uploadFileWithRobot(fpath);
  
  Thread.sleep(5000);
  wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[contains(@class,'file-uploading')]")));
  wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(@class,'successUploadFile')]")));
  if(driver.findElement(By.xpath("//*[contains(@class,'successUploadFile')]")).isDisplayed()) {
  System.out.println("File uploaded successfully");  
  }else {
  System.out.println("File is not uploaded successfully");
  }
  Thread.sleep(5000);
  }
  @BeforeClass
  public void beforeClass() {
  System.out.println("I am in BeforeClass block");
  //set the chrome driver.exe path
  System.setProperty("webdriver.chrome.driver","D:\\webdriverjars\\executables\\chromedriver_win32\\chromedriver.exe");
Reporter.log("creating chromedriver object");
// interface refobj=new implementingclass();
driver = new ChromeDriver();
//implicitwait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//maximize the window
driver.manage().window().maximize();
//create an object for WebDriverWait class
wait=new WebDriverWait(driver,30);
  }

  @AfterClass
  public void afterClass() {
  System.out.println("I am in AfterClass block");
//close the browser
driver.close();
  }

}
========================================================
downlaod file using Firefox
package testngprograms;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;

public class DownloadFileUsingFirefoxeTest {
private String fpath="D:\\webdriverjars";
private WebDriver driver=null;
private WebDriverWait wait=null;
  @Test
  public void seleniumJarDownloadTest() throws InterruptedException {
  //open the url
  Reporter.log("open the seleniumhq.org website");
  driver.get("https://selenium.dev/downloads/");
  Reporter.log("verifying the download page title");
  wait.until(ExpectedConditions.titleContains("Downloads"));
  
  WebElement downloadlink=driver.findElement(By.xpath("//*[text()='Java']/following::a"));
 
  downloadlink.click();
  Thread.sleep(5000);
  }
  @BeforeClass
  public void beforeClass() {
  System.out.println("I am in BeforeClass block");
  //set the chrome driver.exe path
  System.setProperty("webdriver.gecko.driver", "D:\\BrowserExefiles\\geckodriver-v0.26.0-win64\\geckodriver.exe");
  FirefoxOptions opt=Utility.downloadFileUsingFirefox(fpath);
  //interface refvariable=new implementingclass();
driver=new FirefoxDriver(opt);
//implicitwait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//maximize the window
driver.manage().window().maximize();
//create an object for WebDriverWait class
wait=new WebDriverWait(driver,30);
  }

  @AfterClass
  public void afterClass() {
  System.out.println("I am in AfterClass block");
//close the browser
  driver.close();
  }


}
=================================================================
Download files using Chrome

package testngprograms;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;

public class DownloadFileUsingChromeTest {
private String fpath="D:\\webdriverjars";
private WebDriver driver=null;
private WebDriverWait wait=null;
  @Test
  public void seleniumJarDownloadTest() throws InterruptedException {
  //open the url
  driver.get("https://selenium.dev/downloads/");
  wait.until(ExpectedConditions.titleContains("Downloads"));
// 
  //downloading selenium standalone server
  WebElement downloadlink=driver.findElement(By.xpath("//html/body/div[1]/div[2]/p[1]/a"));
  
  //click on download link
// WebElement downloadlink=driver.findElement(By.xpath("//*[text()='Javadoc']/preceding::a[2]"));
 
  downloadlink.click();
  Thread.sleep(10000);
  }
  @BeforeClass
  public void beforeClass() {
  System.out.println("I am in BeforeClass block");
  //set the chrome driver.exe path
  System.setProperty("webdriver.chrome.driver","D:\\webdriverjars\\executables\\chromedriver_win32\\chromedriver.exe");
Reporter.log("creating chromedriver object");
// interface refobj=new implementingclass();
ChromeOptions opt=Utility.downloadFileUsingChrome(fpath);
  //interface refvariable=new implementingclass();
driver=new ChromeDriver(opt);
//implicitwait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//maximize the window
driver.manage().window().maximize();
//create an object for WebDriverWait class
wait=new WebDriverWait(driver,30);
  }

  @AfterClass
  public void afterClass() {
  System.out.println("I am in AfterClass block");
//close the browser
  driver.close();
  }


}
======================================================
Handling DatePicker
---------------------------------------------------------------------------------------------
package testngprograms;

import org.testng.annotations.Test;
import java.util.List;
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.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class JqueryDatePickerTest {
WebDriver driver = null;
WebDriverWait wait = null;

@Test
public void dateSelectionTest() throws InterruptedException {

driver.get("http://jqueryui.com/datepicker/");
WebElement frameElement = driver.findElement(By.className("demo-frame"));
driver.switchTo().frame(frameElement);
// click to open the date time picker calendar.
By dtp = By.xpath(".//*[@id='datepicker']");
wait.until(ExpectedConditions.presenceOfElementLocated(dtp));
driver.findElement(dtp).click();

// Provide the day of the month to select the date.
HandleJQueryDateTimePicker("22");
}

// Function to select the day of the month in the date picker.
public void HandleJQueryDateTimePicker(String day) throws InterruptedException {

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("ui-datepicker-div")));
WebElement table = driver.findElement(By.className("ui-datepicker-calendar"));
System.out.println("JQuery Calendar Dates");

List<WebElement> tableRows = table.findElements(By.xpath("//tr"));
for (WebElement row : tableRows) {
List<WebElement> cells = row.findElements(By.xpath("td"));

for (WebElement cell : cells) {
if (cell.getText().equals(day)) {
driver.findElement(By.linkText(day)).click();
}
}
}

// Switch back to the default screen again and scroll up by using
// the negative y-coordinates.
driver.switchTo().defaultContent();
((JavascriptExecutor) driver).executeScript("scroll(0, -250);");

// Intentional pause for 2 seconds.
Thread.sleep(2000);
}

@BeforeTest
public void beforeTest() {

/*
* //set the geckodriver.exe path System.setProperty("webdriver.gecko.driver",
* "D:\\BrowserExefiles\\geckodriver-v0.26.0-win64\\geckodriver.exe"); //launch
* the browser //interface refvar=new implmentingclass(); driver=new
* FirefoxDriver();
*/
Reporter.log("set CHROME_DRIVER_SILENT_OUTPUT_PROPERTY to avoid TImeOut log in console");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");

Reporter.log("set the chromedriver.exe file");
System.setProperty("webdriver.chrome.driver",
"D:\\webdriverjars\\executables\\chromedriver_win32\\chromedriver.exe");
// interface refobj=new implementingclass();
driver = new ChromeDriver();

// maximize the window
driver.manage().window().maximize();
// add implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// create Object for WebDriverWait class
wait = new WebDriverWait(driver, 30);

}

@AfterTest
public void afterTest() {
//driver.close();
}

}
=====================================================================
FileDownload :
-----------------------------------------------------
package testngprograms;

import org.testng.annotations.Test;

import basicprograms.WebDriverUtils;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

import java.io.File;
import java.time.Duration;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;

public class DownloadFileTest {
WebDriver driver = null;
WebDriverWait wait = null;
String fpath = "C:\\Users\\rames\\OneDrive\\Documents";
  @Test
  public void downloadFileUsingChrome() throws InterruptedException {
  Reporter.log("open the url:https://www.selenium.dev/downloads/", true);
driver.get("https://www.selenium.dev/downloads/");
Reporter.log("Click on Selenium server version link", true);
driver.findElement(By.xpath("//div[@class='card-body']/p[1]/a")).click();
Thread.sleep(10000);
File f=new File(fpath);
File[] flist=f.listFiles();
for(File fl:flist) {
if(fl.getName().contains("selenium-server")) {
System.out.println("downloaded file name is: "+fl.getName());
Assert.assertTrue(fl.getName().contains("selenium-server"));
Thread.sleep(3000);
//delete the file
boolean b=fl.delete();
Thread.sleep(3000);
Assert.assertFalse(fl.getName().contains("selenium-server"));
}
}
  }
  @Parameters({ "browser" })
@BeforeClass(alwaysRun = true)
public void beforeClass(String browser) {
if (browser.equalsIgnoreCase("chrome")) {
ChromeOptions opt = new ChromeOptions();
opt=WebDriverUtils.downloadFileUsingChrome(fpath);
opt.setAcceptInsecureCerts(true);
driver = new ChromeDriver(opt);
Reporter.log("chromebrowser is launched", true);
} else if (browser.equalsIgnoreCase("firefox")) {

FirefoxOptions opt = new FirefoxOptions();
opt.setAcceptInsecureCerts(true);
// opt.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
// interface refvar=new implementedclass();
opt=WebDriverUtils.downloadFileUsingFirefox(fpath);
driver = new FirefoxDriver(opt);
Reporter.log("firefox browser is launched", true);
} else if (browser.equalsIgnoreCase("edge")) {
EdgeOptions opt = new EdgeOptions();
opt.setAcceptInsecureCerts(true);
driver = new EdgeDriver(opt);
Reporter.log("edge browser is launched", true);
}
Reporter.log("maximize the window", true);
driver.manage().window().maximize();
Reporter.log("add implicitwait", true);
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(10000));
Reporter.log("add explicitwait object", true);
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
Reporter.log("creating Object for WebDriverUtils class", true);
}

@AfterClass
public void afterClass() {
System.out.println("I am in AfterClass block");
// close the browser
driver.close();
}


}


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.