전에 잠깐 눈으로만 알고 있었는데, 한번 셈플링 해보았다.

자세한것은 모르지만 Selenium 과 WebDriver 의 장점을 살려서 Selenium 2 로 탄생했단다.


우선 편리한 레코딩을 위해서 Firefox와 Firefox Plugin으로 Selenium IDE를 설치한다.

http://docs.seleniumhq.org/download/


아래 IDE로 자동 레코딩된 소스를 조금 수정해서 돌려보았다.


Firefox 이외의 브라우저에서는 alert 클릭이 부분이 정상적으로 되지 않았다...


추 후에 연구해봐야 할듯...(단순하게 WebDriver만 바꾸는게 아닌건가...)


또한 IDE로 레코딩 할때에 frameset 기반의 홈페이지는 레코딩이 잘 되지 않았고, javascript로 레이어로 펼쳐서 클릭하는 메뉴의 경우에도 Firefox를 제외하고는 정상적으로 되지 않았다.


[LoginTest.java]

import static org.junit.Assert.fail;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class LoginTest {
	private WebDriver driver;
	private String baseUrl;
	private boolean acceptNextAlert = false;
	private StringBuffer verificationErrors = new StringBuffer();
	private int browser = 1;
	
	@Test
	public void loginTest() throws Exception {
		baseUrl = "http://localhost:8080/";
		driver.get(baseUrl + "/login");
		driver.findElement(By.name("j_username")).clear();
		driver.findElement(By.name("j_username")).sendKeys("1");
		driver.findElement(By.name("j_password")).clear();
		driver.findElement(By.name("j_password")).sendKeys("2");
		driver.findElement(By.name("submit")).click();
		driver.findElement(By.cssSelector("button")).click();
		Assert.assertEquals("이구용", closeAlertAndGetItsText());
	}
	
	@Before
	public void setUp() throws Exception {
		DesiredCapabilities caps = null;
		File f = null;
		
		if( browser == 0 ){
			driver = new HtmlUnitDriver(true);
		}else if( browser == 1 ){
			driver = new FirefoxDriver();
		}else if( browser == 2 ){
			f = new File("E:\\webdriver\\IEDriverServer32\\IEDriverServer.exe");
			System.setProperty("webdriver.ie.driver", f.getAbsolutePath());
			
			caps = DesiredCapabilities.internetExplorer();
			caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
			driver = new InternetExplorerDriver(caps);
		}else if( browser == 3 ){
			f = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
			System.setProperty("webdriver.chrome.driver", f.getAbsolutePath());
			
			driver = new ChromeDriver();
		}
		
		
		driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
	}
	
	@After
	public void tearDown() throws Exception {
		System.out.println("@After");
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private String closeAlertAndGetItsText() {
		try {
			Alert alert = driver.switchTo().alert();
			String alertText = alert.getText();
			if (acceptNextAlert) {
				alert.accept();
			} else {
				alert.dismiss();
			}
			return alertText;
		} finally {
			acceptNextAlert = true;
		}
	}
}



[build.gradle]

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.5
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
    //testCompile 'org.seleniumhq.selenium:selenium-java:2.28.0'
	//testCompile 'org.seleniumhq.selenium:selenium-firefox-driver:2.39.0'
	//testCompile 'org.seleniumhq.selenium:selenium-server:2.39.0'
	//testCompile 'org.seleniumhq.selenium:selenium-server-standalone:2.39.0'
	testCompile 'org.seleniumhq.selenium:selenium-java:2.39.0'
	testCompile 'org.seleniumhq.selenium:selenium-firefox-driver:2.39.0'
	testCompile 'org.seleniumhq.selenium:selenium-chrome-driver:2.39.0'
	testCompile 'org.seleniumhq.selenium:selenium-IE-driver:2.39.0'
	testCompile 'org.seleniumhq.selenium:selenium-HtmlUnit-driver:2.39.0'
}



+ Recent posts