Last active
July 26, 2017 04:33
-
-
Save keehyun2/18d6678d3409bb1c88f279b9f51ef47b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
... | |
<dependency> | |
<groupId>org.seleniumhq.selenium</groupId> | |
<artifactId>selenium-java</artifactId> | |
<version>3.4.0</version> | |
</dependency> | |
... |
This file contains hidden or 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
package kr.co.cyberline; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.concurrent.TimeUnit; | |
import org.openqa.selenium.Alert; | |
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.support.ui.ExpectedCondition; | |
import org.openqa.selenium.support.ui.Select; | |
import org.openqa.selenium.support.ui.WebDriverWait; | |
public class WebDirverTest { | |
static WebDriver driver; | |
static String parentWindowHandler; | |
static String subWindowHandler; | |
static DateFormat dateFormat; | |
static Date date; | |
public static void main(String[] args) throws Exception { | |
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
date = new Date(); | |
// 파이어폭스 되긴하는데 로그에 에러 생김 | |
// System.setProperty("webdriver.gecko.driver","C:\\dev\\workspace\\WebDriverTest\\src\\main\\resources\\kr\\co\\cyberline\\geckodriver.exe"); | |
// WebDriver driver = new FirefoxDriver(); | |
// 엣지 드라이버 에러 뜨는데 일단 pass | |
// System.setProperty("webdriver.edge.driver","C:\\dev\\workspace\\WebDriverTest\\src\\main\\resources\\kr\\co\\cyberline\\MicrosoftWebDriver.exe"); | |
// WebDriver driver = new EdgeDriver(); | |
System.setProperty("webdriver.chrome.driver", | |
"C:\\dev\\workspace\\WebDriverTest\\src\\main\\resources\\kr\\co\\cyberline\\chromedriver.exe"); | |
driver = new ChromeDriver(); | |
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); | |
driver.get("http://localhost:8080/kma_rnd/main/main.do"); | |
parentWindowHandler = driver.getWindowHandle(); // Store your parent window | |
// 로그인 시작 | |
WebElement element = driver.findElement(By.name("user_id")); | |
element.sendKeys("admin"); | |
element = driver.findElement(By.name("user_pwd")); | |
element.sendKeys("1"); | |
element = driver.findElement(By.cssSelector(".login_btn01 > a")); // 로그인 버튼 클릭 | |
element.click(); | |
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { | |
public Boolean apply(WebDriver d) { | |
return driver.switchTo().alert().getText().startsWith("로그인 성공"); | |
} | |
}); | |
Alert alert = driver.switchTo().alert(); | |
alert.accept(); | |
// 로그인 끝 | |
driver.findElement(By.linkText("신청")).click(); | |
// 공고등록 | |
registerPrj(); | |
// 신규 과제 등록 | |
// click(By.cssSelector("#tab01 a")); | |
// click(By.cssSelector("a[title='신규과제']")); | |
// click(By.linkText("신규작성")); | |
// click(By.cssSelector("#popPblancRfpList")); | |
// detectPopup(); | |
// driver.switchTo().window(subWindowHandler); | |
// click(By.cssSelector("a[class*='addPblanc(1)']")); | |
// | |
// driver.switchTo().window(parentWindowHandler); | |
// click(By.cssSelector("a[class*='searchTechcl']")); | |
// detectPopup(); | |
// driver.switchTo().window(subWindowHandler); | |
// click(By.cssSelector("a#btn0")); | |
// | |
// driver.switchTo().window(parentWindowHandler); | |
// click(By.cssSelector("a[class*='searchDeptTree']")); | |
// detectPopup(); | |
// driver.switchTo().window(subWindowHandler); | |
// click(By.cssSelector("a#btn0")); | |
// | |
// driver.switchTo().window(parentWindowHandler); | |
// | |
// driver.findElement(By.name("kor_pjt_nm")).sendKeys("Selenium_과제_" + dateFormat.format(date)); | |
// driver.findElement(By.name("eng_pjt_nm")).sendKeys("Selenium_과제_eng_" + dateFormat.format(date)); | |
// | |
// click(By.cssSelector("a[class*='searchDept(basicRsrchOrg)']")); | |
// detectPopup(); | |
// driver.switchTo().window(subWindowHandler); | |
// click(By.cssSelector("a#btn0")); | |
// driver.switchTo().window(parentWindowHandler); | |
// | |
// driver.findElement(By.name("tot_rsch_start_dt")).click(); | |
// selectDate(); | |
// driver.findElement(By.name("tot_rsch_end_dt")).click(); | |
// selectDate(); | |
// driver.findElement(By.name("tsyr_rsch_start_dt")).click(); | |
// selectDate(); | |
// driver.findElement(By.name("tsyr_rsch_end_dt")).click(); | |
// selectDate(); | |
} | |
/** | |
* 공고등록 | |
*/ | |
public static void registerPrj() throws Exception{ | |
click(By.xpath(".//*[@id='MNU_0000000000000827']/a")); // 과제공고 | |
click(By.linkText("신규작성")); | |
driver.findElement(By.name("pblanc_title")).sendKeys("Selenium_Webdriver_Test_" + dateFormat.format(date)); | |
click(By.xpath("//*[@id='frmPrjPblancCreate']/table/tbody/tr[3]/td/a")); // 사업명 팝업 | |
detectPopup(); | |
driver.switchTo().window(subWindowHandler); // switch to popup window | |
click(By.cssSelector("#btn0>strong")); | |
driver.switchTo().window(parentWindowHandler); // switch back to parent window - 팝업이 닫혓으면 반드시 드라이버를 parent 로 스위치 해주어야함. | |
click(By.xpath("//*[@id='frmPrjPblancCreate']/table/tbody/tr[4]/td[1]/a/img")); | |
detectPopup(); | |
driver.switchTo().window(subWindowHandler); | |
click(By.cssSelector("#btn0>strong")); | |
driver.switchTo().window(parentWindowHandler); | |
driver.findElement(By.name("pblanc_content")).sendKeys("내용_테스트_" + dateFormat.format(date)); | |
driver.findElement(By.name("pblanc_acc_open_dt")).click(); | |
selectDate(); | |
new Select(driver.findElement(By.name("pblanc_acc_open_hour"))).selectByIndex(2); | |
driver.findElement(By.name("pblanc_acc_clos_dt")).click(); | |
selectDate(); | |
new Select(driver.findElement(By.name("pblanc_acc_clos_hour"))).selectByIndex(23); | |
click(By.linkText("정보저장")); | |
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { | |
public Boolean apply(WebDriver d) { | |
return driver.switchTo().alert().getText().startsWith("저장"); | |
} | |
}); | |
driver.switchTo().alert().accept(); | |
} | |
/** | |
* ajax load 할때 | |
* blockUI 가 클릭 할 부분을 막아서 wait 처리함. css fade in 효과 때문 인것 같음... 여튼 클릭할때 에러 발생함... | |
* @param by | |
* @throws InterruptedException | |
*/ | |
public static void click(By by) throws InterruptedException{ | |
try { | |
driver.findElement(by).click(); | |
} catch (Exception e) { | |
//e.printStackTrace(); | |
synchronized (driver) { driver.wait(1000); } | |
click(by); | |
} | |
} | |
public static void click(WebElement we) throws InterruptedException{ | |
try { | |
we.click(); | |
} catch (Exception e) { | |
//e.printStackTrace(); | |
synchronized (driver) { driver.wait(1000); } | |
click(we); | |
} | |
} | |
/** | |
* 새로 생긴 팝업을 찾는다. 팝업이 여러개 떠있으면 에러 생길수 있음. | |
* @throws InterruptedException | |
*/ | |
public static void detectPopup() throws InterruptedException{ | |
Set<String> handles = driver.getWindowHandles(); // get all window handles | |
Iterator<String> iterator = handles.iterator(); | |
while (iterator.hasNext()){ | |
subWindowHandler = iterator.next(); | |
} | |
} | |
/** | |
* date 를 오늘 날짜로 select 한다. | |
* @throws InterruptedException | |
*/ | |
public static void selectDate() throws InterruptedException{ | |
WebElement dateWidget = driver.findElement(By.id("ui-datepicker-div")); | |
List<WebElement> columns=dateWidget.findElements(By.tagName("td")); | |
for (WebElement cell : columns) | |
{ | |
if (cell.getText().equals(new SimpleDateFormat("dd").format(date))) | |
{ | |
click(cell); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment