JMeter+Selenium转载

上一篇 / 下一篇  2017-04-15 11:12:17 / 个人分类:性能测试相关

转载
http://f.dataguru.cn/forum.php?mod=viewthread&tid=706274&highlight=jmeter
第一步:创建Selenium测试
将selenium- server-standalone-2.37.0.jar(下载地址:https://code.google.com/p/selenium/)拷贝 到%JMETER_HOME%/lib/目录下,将Selenium测试用例打包成jar文件,并拷贝到%JMETER_HOME%/lib/junit /目录下,注意:测试用例应该继承TestCase or SeleniumTestCase类,从而允许JMeter获取到该测试用例,且测试方法名称应该以test开头,示例如下:
importJava.util.concurrent.TimeUnit;
  
  
importjunit.framework.TestCase;
  
  
importorg.junit.After;
  
importorg.junit.Before;
  
importorg.junit.Test;
  
importorg.openqa.selenium.Alert;
  
importorg.openqa.selenium.By;
  
importorg.openqa.selenium.NoAlertPresentException;
  
importorg.openqa.selenium.NoSuchElementException;
  
importorg.openqa.selenium.WebDriver;
  
importorg.openqa.selenium.firefox.FirefoxDriver;
  
importorg.openqa.selenium.firefox.FirefoxProfile;
  
  
publicclassSelenium  extendsTestCase {
  
    privateWebDriver  driver;
  
    privateString  baseUrl;
  
    privatebooleanacceptNextAlert  = true;
  
    privateStringBuffer  verificationErrors = newStringBuffer();
  
  
    @Before
  
    publicvoidsetUp()  throwsException {
  
        FirefoxProfile  profile = newFirefoxProfile();
  
        driver  = newFirefoxDriver(profile);
  
        baseUrl  = "http://www.baidu.com";
  
        driver.manage().timeouts().implicitlyWait(30,  TimeUnit.SECONDS);
  
    }
  
  
    @Test
  
    publicvoidtestSelenium()  throwsException {
  
        driver.get(baseUrl  + "/");
  
        driver.findElement(By.id("kw")).clear();
  
        driver.findElement(By.id("kw")).sendKeys("JMeter");
  
        driver.findElement(By.id("su")).click();
  
        driver.findElement(By.linkText("jmeter_百度百科")).click();
  
    }
  
  
    @After
  
    publicvoidtearDown()  throwsException {
  
        driver.quit();
  
        String  verificationErrorString = verificationErrors.toString();
  
        if(!"".equals(verificationErrorString))  {
  
            fail(verificationErrorString);
  
        }
  
    }
  
  
    privatebooleanisElementPresent(By  by) {
  
        try{
  
            driver.findElement(by);
  
            returntrue;
  
        }  catch(NoSuchElementException e) {
  
            returnfalse;
  
        }
  
    }
  
  
    privatebooleanisAlertPresent()  {
  
        try{
  
            driver.switchTo().alert();
  
            returntrue;
  
        }  catch(NoAlertPresentException e) {
  
            returnfalse;
  
        }
  
    }
  
  
    privateString  closeAlertAndGetItsText() {
  
        try{
  
            Alert  alert = driver.switchTo().alert();
  
            String  alertText = alert.getText();
  
            if(acceptNextAlert)  {
  
                alert.accept();
  
            }  else{
  
                alert.dismiss();
  
            }
  
            returnalertText;
  
        }  finally{
  
            acceptNextAlert  = true;
  
        }
  
    }
  
}
  
第二步:JMeter加载Selenium测试类
在Jmeter中创建test group和JUnit sampler测试计划,并在JUnit sampler中选择测试用例的名称,选择测试方法并运行

TAG:

 

评分:0

我来说两句

Open Toolbar