Monday, January 8, 2018

File Upload Simplified


package com.examples;

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;

public class File_Upload_01 {

public static void main(String[] args) {
System.setProperty("Webdriver.chrome.driver", "chromedriver");
WebDriver wd = new ChromeDriver();
int i = 2;
if (i == 0) {
wd.get("http://cgi-lib.berkeley.edu/ex/fup.html");
wd.findElement(By.xpath("html/body/form/input[1]")).sendKeys("/operadriver");
wd.findElement(By.xpath("html/body/form/input[2]")).sendKeys("Test");
wd.findElement(By.xpath("html/body/form/input[3]")).click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(wd.getTitle());
}
if (i == 1) {
wd.get("https://encodable.com/uploaddemo/");
wd.findElement(By.xpath(".//*[@id='uploadname1']")).sendKeys("/operadriver");
wd.findElement(By.xpath(".//*[@id='newsubdir1']")).sendKeys("Test");
wd.findElement(By.xpath(".//*[@id='formfield-email_address']")).sendKeys("Test@test.com");
wd.findElement(By.xpath(".//*[@id='formfield-first_name']")).sendKeys("Test");

wd.findElement(By.xpath(".//*[@id='uploadbutton']")).click();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(wd.getTitle());
}
if (i == 2) {
wd.get("https://www.websupergoo.com/file-upload-1.htm");
wd.findElement(By.xpath(".//*[@id='page-content']/article/section/form/fieldset/input[1]"))
    .sendKeys("/operadriver");
// wd.findElement(By.xpath(".//*[@id='newsubdir1']")).sendKeys("Test");
// wd.findElement(By.xpath(".//*[@id='formfield-email_address']")).sendKeys("Test@test.com");
// wd.findElement(By.xpath(".//*[@id='formfield-first_name']")).sendKeys("Test");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// .click();
/*
* Actions ac = new Actions(wd); ac.click(wd.findElement(By.cssSelector(
* ".container>form>fieldset>input[type='submit']")));
*/
WebElement element = wd.findElement(By.xpath(".//*[@id='page-content']/article/section/form/fieldset/input[2]"));

JavascriptExecutor executor = (JavascriptExecutor) wd;
executor.executeScript("arguments[0].click();", element);
wd.switchTo().alert().dismiss();
System.out.println(wd.getTitle());
}
// wd.quit();

}

}

No comments:

Post a Comment