selenium 测试脚本

package test;

 

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 hello {

  private WebDriver driver;

  private String baseUrl;

  private String linkurl="http://www.hc-fund.com/portal/admin/user/page";

  private boolean acceptNextAlert = true;

  private StringBuffer verificationErrors = new StringBuffer();

 

  @Before

  public void setUp() throws Exception {

    driver = new FirefoxDriver();

    baseUrl = "http://www.hc-fund.com/portal/login";

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  }

  

  @Test

  public void testCase1() throws Exception {

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("xx");//1 case;

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("xx");

    driver.findElement(By.id("submit_btn")).click();

    driver.get(linkurl);

    driver.findElement(By.linkText("admin")).click();

    //Thread.sleep(1000);

    driver.findElement(By.linkText("退出")).click();

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("submit_btn")).click();//2case;

    Thread.sleep(1000);

    try {

      assertEquals("登录名和密码不能为空", driver.findElement(By.cssSelector("div.alert-error")).getText());

    } catch (Error e) {

      verificationErrors.append(e.toString());

    }

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("'and  '1 or 1 = 1");//3 case;

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("");

    driver.findElement(By.id("submit_btn")).click();

    try {

      assertEquals("登录名和密码不能为空", driver.findElement(By.cssSelector("div.alert-error")).getText());

    } catch (Error e) {

      verificationErrors.append(e.toString());

    }

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("1111111111111111111111111111111111111");//4 case;

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("1111111111111111111111111111111111111");

    driver.findElement(By.id("submit_btn")).click();

    try {

      assertEquals("登录失败,请重试.", driver.findElement(By.cssSelector("div.alert-error")).getText());

    } catch (Error e) {

      verificationErrors.append(e.toString());

    }

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("'admin or '1 = 1");//5 case;

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("'admin or '1 = 1");

    driver.findElement(By.id("submit_btn")).click();

    try {

      assertEquals("登录失败,请重试.", driver.findElement(By.cssSelector("div.alert-error")).getText());

    } catch (Error e) {

      verificationErrors.append(e.toString());

    }

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("yes"); //6 case;

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("yes");

    driver.findElement(By.id("submit_btn")).click();

    try {

      assertEquals("登录失败,请重试.", driver.findElement(By.cssSelector("div.alert-error")).getText());

    } catch (Error e) {

      verificationErrors.append(e.toString());

    }

    driver.get(baseUrl + "/portal/login");

    driver.findElement(By.id("username")).clear();

    driver.findElement(By.id("username")).sendKeys("数学"); //7 case;

    driver.findElement(By.id("password")).clear();

    driver.findElement(By.id("password")).sendKeys("数学");

    driver.findElement(By.id("submit_btn")).click();

    try {

      assertEquals("登录失败,请重试.", driver.findElement(By.cssSelector("div.alert-error")).getText());

    } catch (Error e) {

      verificationErrors.append(e.toString());

    }

  }

 

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

    }

  }

}

 //登录脚本

posted @ 2014-11-27 12:02  echo1006  阅读(273)  评论(0编辑  收藏  举报