python andselenium

17
Python And Selenium [email protected]

Upload: dae-myung-kang

Post on 06-Dec-2014

1.710 views

Category:

Documents


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Python andselenium

Python And Selenium

[email protected]

Page 2: Python andselenium

Selenium

셀레늄.브라우저 자동화 도구.브라우저에 대한 UI 테스트시 사용된다.http://www.seleniumhq.org/download/

Page 3: Python andselenium
Page 4: Python andselenium

Selenium 설치

테스트 환경: Linux Ubuntu, Firefox

java -jar selenium-server-standalone-2.35.0.jarpip install -U selenium

Page 5: Python andselenium

샘플 #1from selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysimport time

browser = webdriver.Firefox() # Get local session of firefoxbrowser.get("http://www.yahoo.com") # Load pageassert "Yahoo!" in browser.titleelem = browser.find_element_by_name("p") # Find the query boxelem.send_keys("seleniumhq" + Keys.RETURN)time.sleep(0.2) # Let the page load, will be added to the APItry: browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]").click()except NoSuchElementException: assert 0, "can't find seleniumhq"browser.close()

Page 6: Python andselenium

시연 #1

Page 7: Python andselenium

나쁜 짓에도 할 수 있을듯...

추석 표 예매?또는 영화 표 예매?

Page 8: Python andselenium

기차표를 예매한다면 어떤 것들이 필요할까?

1. 브라우저 실행/특정 페이지 이동2. 폼 전달/특정 태그 선택 후 클릭3. 경고창4. select/option 태그 값 변경5. 종료

Page 9: Python andselenium

브라우저 실행/특정 페이지 이동

1. driver = webdriver.Firefox()2. driver.get("http://www.korail.com/2012/index.jsp")

Page 10: Python andselenium

특정 버튼 찾아서 클릭

driver.find_element_by_link_text("로그인").click()

Page 11: Python andselenium

Form 채우기

driver.find_element_by_name('txtMember').send_keys(id)driver.find_element_by_name('txtPwd').send_keys(pw)

Page 12: Python andselenium

경고창

alert = driver.switch_to_alert()time.sleep(2)alert.accept()

적절한 딜레이가 필요

Page 13: Python andselenium

select/option tag select = driver.find_element_by_name("txtPsgFlg_1") options = select.find_elements_by_tag_name("option") for option in options: if num == option.get_attribute("value"): option.click()

Page 14: Python andselenium

브라우저 종료driver.close()

Page 15: Python andselenium

시연2

Page 16: Python andselenium

Tip

1. Selenium IDE를 사용하면 정보를 찾기 쉽다.2. 적절한 딜레이를 줘야 정상적으로 동작한다.3. 예외 처리를 잘 해둬야 한다.

Page 17: Python andselenium

결론

1. 이번 기차표 예매는 큰 도움은 안됬음. - 서버가 뻗어서 일단 예상대로 동작 안함.2. 페이지 소스가 자주 바뀌면 이에 따라 계속 수정이 필요함.3. 그럼에도 불구하고, 배우고 쓸만한 가치가 있음. 웹 UI 테스트 자동화등 - 실제 서비스 테스트 사례도 많음.