셀레늄 #
셀레늄(Selenium)은 Ajax 어플리케이션의 GUI 테스트를 자동화해주는 프레임워크 입니다. 셀레늄은 크게 셀레늄 코어(Selenium Core)와 셀레늄 개발환경(Selenium IDE)로 나뉩니다. 원칙대로라면 셀레늄 코어에 대해 먼저 설명해야 겠지만, 편의를 위해 개발환경에 대해서 먼저 설명토록 하겠습니다.
셀레늄 코어 #
셀레늄 코어(Selenium Core)는 자바스크립트 기반의 프레임워크이며, 셀레늄 테스트를 실행하는 역할을 합니다.
Locator #
Locator는 어떤 객체를 찾기 위한 용도로 사용되는 문자열입니다. 사용법은 다음과 같습니다.
locatorType=argument
locatorType의 종류와 사용법은 다음과 같습니다.
- identifier=id
객체의 id 값(Attribute)으로 객체를 찾습니다. 찾지 못할 경우, name=id로 찾습니다.(기본)
- id=id
객체의 id 값으로 객체를 찾습니다.
- name=name
객체의 name 값으로 객체를 찾습니다.
- username
- name=username The name may optionally be followed by one or more element-filters, separated from the name by whitespace. If the filterType is not specified, value is assumed.
- name=flavour value=chocolate
- dom=javascriptExpression
javascriptExpression으로 객체를 찾습니다. 이것은 HTML DOM을 자바스크립트로 찾아볼 수 있도록 지원합니다. Note that you must not return a value in this string; simply make it the last expression in the block.
- dom=document.forms['myForm'].myDropdown
- dom=document.images56
- dom=function foo() { return document.links[1]; }; foo();
- xpath=xpathExpression
XPath로 객체를 찾습니다. (???)
- xpath=//img@alt='The image alt text'
- xpath=//table@id='table1'//tr[4]/td[2]
- xpath=//a[contains(@href,'#id1')]
- xpath=//a[contains(@href,'#id1')]/@class
- xpath=(//table@class='stylee')//thtext()='theHeaderText'/../td
- xpath=//input@name='name2' and @value='yes'
- xpath=//*text()="right"
- link=textPattern
링크(anchor) 객체중에 testPattern이 매칭되는 것을 찾습니다.
- link=The link text
- css=cssSelectorSyntax
CSS 셀렉터를 통해 객체를 찾습니다. Please refer to CSS2 selectors, CSS3 selectors for more information. You can also check the TestCssLocators test in the selenium test suite for an example of usage, which is included in the downloaded selenium core package.
- css=ahref="#id3"
- css=span#firstChild + span Currently the css selector locator supports all css1, css2 and css3 selectors except namespace in css3, some pseudo classes(:nth-of-type, :nth-last-of-type, :first-of-type, :last-of-type, :only-of-type, :visited, :hover, :active, :focus, :indeterminate) and pseudo elements(::first-line, ::first-letter, ::selection, ::before, ::after).
- dom : 만약 로케이터가 "document."으로 시작한다면
- xpath : 만약 로케이터가 "//"로 시작한다면
- identifier : 그렇지 않은 모든 경우에
Assertion #
셀레늄은 많은 Assertion 방법을 제공합니다. AssertVisible과 같이, GUI에 관계된 Assert도 있습니다. 자세한 것은 레퍼런스를 참고해주세요.
셀레늄 온 레일스 #
셀레늄 온 레일스(Selenium on Rails) - 루비 온 레일스의 셀레늄 테스트
설치 #
설치는 프로젝트 단위로 이루어짐.
- script/plugin install http://svn.openqa.org/svn/selenium-on-rails/selenium-on-rails
- 윈도우 사용자라면, gem install win32-open3
사용 #
- script/generate selenium xxx 를 실행하여 xxx 라는 셀레늄 테스트 생성
- test/selenium/xxx.sel 파일을 수정하여 셀레늄 테스트 작성
- script/server -e test 를 실행하여 테스트 모드로 서버 실행
http://localhost:3000/selenium 로 들어가서 테스트 진행
셀레늄 개발환경 #
셀레늄 개발환경(Selenium IDE)은 파이어폭스 웹브라우저의 플러그인으로, 셀레늄 테스트를 쉽게 작성하고 실행할 수 있도록 합니다.
Selenium IDE
파이어폭스 플러그인 설치 - 파이어폭스 웹브라우저에서 해당 링크를 열고 설치를 누르면 자동으로 설치하게 됩니다. 파이어폭스 플러그인을 설치하면, 셀레늄 코어도 자동으로 설치됩니다. 또한 셀레늄 개발환경으로 작성한 테스트는 특별히 셀레늄 코어를 연결해주지 않아도 자동으로 연결해주고, 실행해줍니다.
Hello Selenium #
셀레늄 개발환경으로 간단한 테스트를 작성해봅시다. 먼저 테스트를 적용할 웹페이지부터 준비합시다. 아래 내용을 작성한 후 파일로 저장합니다.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
window.onload = function( e ) {
var hello = document.getElementById( "Hello" );
hello.onclick = function( e ) {
var result = document.getElementById( "Result" );
result.innerHTML = "Hello Selenium";
};
}
</script>
<title>Hello Selenium</title>
</head>
<body>
<button id="Hello">Hello</button>
<div id="Result"></div>
</body>
</html>
위의 파일이 c:\hello\hello.html 이라고 가정하면, 테스트 작성은 다음과 같이 진행합니다.
- 파이어폭스 웹브라우저를 열고 테스트 할 페이지를 엽니다.
- 이제 도구 > Selenium IDE를 눌러 셀레늄 개발환경을 실행합니다.
- 맨 위 Base URL에 file:///C:/hello/을 입력합니다. 이 위치는 위에서 hello.html을 작성한 위치이며, 자신의 파일 위치를 입력하시면 됩니다.
- 그 아래 Table 탭안의 명령 창에서, 빈 공간중 가장 위를 클릭하면 새로운 커맨드가 하나 생깁니다. 이런식으로 커맨드를 추가해가면서 아래의 내용들을 입력합니다.
- Command에 open을 입력합니다.
- Target에 Hello.html을 입력합니다. 파일의 이름이나 위치등은 자신이 정한 대로 바꾸어주시면 됩니다.
- 이제 바로 아래의 빈 공간을 클릭해서 새 커맨드를 만듭니다.
- Command에 click을 입력하고, Target에 id=Hello을 입력합니다.
- 새로 커맨드를 만들고, Command에 verifyText, Target에 id=Result, Value에 Hello Selenium을 입력합니다.
위의 테스트 작성 중 9번째 작성한 내용에서, Value의 값을 abcd로 바꿔서 다시 테스트를 실행해봅시다. 마지막 줄에 빨간불이 들어오는 것을 확인해봅시다.
명령의 작성방법은 다음과 같습니다.
- Table 탭에서 빈공간중 맨 위를 클릭
- 중단의 Command 항목에서 명령어 종류를 선택
- Target 항목에서 명령을 받을 타겟을 선택 (셀레늄 코어의 Locator 참고)
- Value 항목에서 명령에 필요한 인자값들을 작성







