ALM/Test Automation

Selenium 테스트 메모

Terry Cho 2013. 12. 24. 00:11

Firefox selenium IDE를 이용하여 Record 가능. 간단하게 IDE내에서 Command 추가등도 가능

아래는 네이버에서 "조대협" 으로 검색하여, 검색 결과에 "조대협의 블로그" 문자열이 나오면 성공하는 테스트 케이스 




작성 완료후 Export하면

Java/JUnit 3,4 , Test NG

Ruby,Python,C# 등으로 TG Export 가능


아래는 JUnit4로 Export한 소스 코드

package com.example.tests;


import com.thoughtworks.selenium.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import static org.junit.Assert.*;

import java.util.regex.Pattern;


public class selenium_TC_naver {

private Selenium selenium;


@Before

public void setUp() throws Exception {

selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.naver.com/");

selenium.start();

}


@Test

public void testSelenium_TC_naver() throws Exception {

selenium.open("/");

selenium.click("id=query");

selenium.type("id=query", "조대협");

selenium.click("id=search_btn");

selenium.waitForPageToLoad("30000");

assertTrue(selenium.isTextPresent("조대협의 블로그"));

}


@After

public void tearDown() throws Exception {

selenium.stop();

}

}

다음은 Junit 4/Web Driver용으로 Export한 소스
package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class SeleniumTCNaverWebdriver {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.naver.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testSeleniumTCNaverWebdriver() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("query")).click();
    driver.findElement(By.id("query")).clear();
    driver.findElement(By.id("query")).sendKeys("조대협");
    driver.findElement(By.id("search_btn")).click();
    // Warning: assertTextPresent may require manual changes
    assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*조대협의 블로그[\\s\\S]*$"));
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  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;
    }
  }
}


그리드형

'ALM > Test Automation' 카테고리의 다른 글

Selenium Test Suite 수행  (0) 2013.12.29
Selenium WebDriver와 RC 차이  (0) 2013.12.24
테스트 팀의 조직 구조  (1) 2012.08.21
JUnit Max  (1) 2009.05.06
Software Testing Proces  (0) 2009.04.09