Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
2
Indexable
Never
package selenium;

import java.util.List;
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.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Topic_11_UploadFile {
	WebDriver driver;

	// Get relative path
	String rootFolder = System.getProperty("user.dir");
	String fileName01 = "image1.jpg";
	String fileName02 = "image2.jpg";
	String fileName03 = "image3.png";

	String fileNamePath01 = rootFolder + "\\uploadFiles\\" + fileName01;
	String fileNamePath02 = rootFolder + "\\uploadFiles\\" + fileName02;
	String fileNamePath03 = rootFolder + "\\uploadFiles\\" + fileName03;

	String[] files = { fileNamePath01, fileNamePath02, fileNamePath03 };

	@BeforeClass
	public void beforeClass() {
		System.setProperty("webdriver.chrome.driver", ".\\lib\\chromedriver.exe");
		driver = new ChromeDriver();
		
//		System.setProperty("webdriver.gecko.driver", ".\\lib\\geckodriver.exe");
//		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		System.out.println("rootFolder: " + rootFolder);
		System.out.println("filepath: " + fileNamePath01);

	}

	public void TC_01_SendKeys_Upload_Queue() {
		driver.get("https://blueimp.github.io/jQuery-File-Upload/");

		// Cach1: for (0 - 1 -2)
//		for (int i = 0; i < files.length; i++) {
//			WebElement uploadFile = driver.findElement(By.xpath("//input[@type='file']"));
//			uploadFile.sendKeys(files[0]);
//			uploadFile.sendKeys(files[1]);
//			uploadFile.sendKeys(files[2]);
//		}

		// Cach2:for-each
		for (String file : files) {
			WebElement uploadFile = driver.findElement(By.xpath("//input[@type='file']"));
			uploadFile.sendKeys(file);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		driver.findElement(By.xpath("//span[contains(text(),'Start upload')]")).click();

		Assert.assertTrue(driver.findElement(By.xpath("//a[text()='" + fileName01 + "']")).isDisplayed());
		Assert.assertTrue(driver.findElement(By.xpath("//a[text()='" + fileName02 + "']")).isDisplayed());
		Assert.assertTrue(driver.findElement(By.xpath("//a[text()='" + fileName03 + "']")).isDisplayed());
	}

	@Test
	public void TC_02_SendKeys_Upload_Multiple() throws Exception {
		driver.get("https://blueimp.github.io/jQuery-File-Upload/");

		WebElement uploadFile = driver.findElement(By.xpath("//input[@type='file']"));
		uploadFile.sendKeys(fileNamePath01 + "\n" + fileNamePath02 + "\n" + fileNamePath03);
		Thread.sleep(1000);

//		driver.findElement(By.xpath("//span[contains(text(),'Start upload')]")).click();
		
		List <WebElement> startButtons = driver.findElements(By.xpath("//table//button[@class='btn btn-primary start']"));
		
		for (WebElement startButton: startButtons) {
			startButton.click();
			Thread.sleep(1000);
		}

		Assert.assertTrue(driver.findElement(By.xpath("//a[text()='" + fileName01 + "']")).isDisplayed());
		Assert.assertTrue(driver.findElement(By.xpath("//a[text()='" + fileName02 + "']")).isDisplayed());
		Assert.assertTrue(driver.findElement(By.xpath("//a[text()='" + fileName03 + "']")).isDisplayed());
		Thread.sleep(1000);

	}

	@AfterClass
	public void afterClass() {
		driver.quit();
	}

}