步骤的响应时间(WebDriver + Jmeter)

时间:2015-06-24 11:39:50

标签: selenium junit selenium-webdriver jmeter

我有一个测试,我在哪里:

  1. 转到链接
  2. 输入登录名和密码,然后按登录按钮
  3. 按一些链接
  4. 按LogOut按钮
  5. 我在JMeter中用5个用户运行它,我应该在csv文件中保存一些数据,如:

    用户名,登录(或4步左右的smth),平均时间。

    在输出中我应该有一个文件,我可以看到,5个用户执行“登录”平均时间(例如5秒)。如何知道平均时间 - 找到所有步骤“登录”加上所有时间并除以用户数(5)?

2 个答案:

答案 0 :(得分:0)

在JMeter中实现它,如下所示:

  • 带有标签Go to the link
  • 的WebDriver采样器
  • 标签为Login
  • 的WebDriver采样器
  • 标签为Navigate
  • 的WebDriver采样器
  • 标签为Logout
  • 的WebDriver采样器

每个采样器的WebDriver代码应如下所示:

 WDS.sampleResult.sampleStart()
 // put code for login, navigate, logout, etc.
 WDS.sampleResult.sampleEnd()

WebDriver会话将保留在采样器之间,JMeter足够智能,可以测量平均响应时间,只需添加相关的监听器,即Aggregate Report

有关更多提示和技巧,请参阅The WebDriver Sampler: Your Top 10 Questions Answered指南。

答案 1 :(得分:0)

不,它没有通过我,我使用JUnit。这是我的代码:

public class LoadTestTwo extends TestCase {
    private WebDriver driver;
    public FirefoxProfile profile = new FirefoxProfile();
    public int index=0;
    private long start;
    private long end;
    boolean alreadyExists = new File("C:\\output.csv").exists(); //write estimate time to file

    public LoadTestTwo(){
        reset();
          //start = System.currentTimeMillis();  
    }

      public void end(){
          end = System.currentTimeMillis(); 
      }

      public long duration(){
          return (end-start);

      }

      public void reset(){
          start = 0;
          end = 0;
      }

    public LoadTestTwo(String testName){
        super(testName);
    }


    @Before
    public void setUp() throws Exception {
        super.setUp();  
    }


    @Test
    public void testTestLoad() throws InterruptedException, IOException, FileNotFoundException {        
        LoadTestTwo t = new LoadTestTwo();
        try {
            CsvWriter csvOutput = new CsvWriter(new FileWriter("C:\\output.csv",true),',');  
            if (!alreadyExists) {  
                csvOutput.write("Users");  
                csvOutput.write("Steps");  
                csvOutput.write("Average Time");
                csvOutput.endRecord();  
            }  

                driver = new FirefoxDriver();
                t.reset();
                start = System.currentTimeMillis();
                driver.get("somelink"); //just hided the real link
                t.end();
                csvOutput.write("LoadTest2");  
                csvOutput.write("Go to URL");  
                csvOutput.write("" + t.duration());  
                csvOutput.endRecord();  
                t.reset();
                start = System.currentTimeMillis();
                start = System.currentTimeMillis();
                driver.findElement(By.id("loginForm:authLogin")).sendKeys("User1");
                driver.findElement(By.id("loginForm:authPassword")).sendKeys("123456");
                driver.manage().timeouts().implicitlyWait(60, TimeUnit.MILLISECONDS);
                driver.findElement(By.id("loginForm:btnLogin")).click();
                t.end();
                csvOutput.write("LoadTest2");  
                csvOutput.write("Login");  
                csvOutput.write("" + t.duration());  
                csvOutput.endRecord();  
                driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
                t.reset();
                start = System.currentTimeMillis();
                driver.findElement(By.className("log")).click();
                t.end();
                driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
                csvOutput.write("LoadTest2");  
                csvOutput.write("Go to Administration");  
                csvOutput.write("" + t.duration());  
                csvOutput.endRecord(); 
                driver.manage().timeouts().implicitlyWait(7000, TimeUnit.MILLISECONDS);
                t.reset();
                start = System.currentTimeMillis();
                driver.findElement(By.xpath("//a[@class='logout']")).click();
                t.end();
                csvOutput.write("LoadTest2");  
                csvOutput.write("Logout");  
                csvOutput.write("" + t.duration());  
                csvOutput.endRecord();  

                /*FileReader fr = new FileReader(new File("C:\\output.csv"));
                BufferedReader br = new BufferedReader(fr);
                String st;
                while ((st = br.readLine()) != null){
                    System.out.println(st);
                }*/

                csvOutput.close();


        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
        driver.quit();
    }

}

这里只有一个用户,我将制作5个Jar文件并在JMeter中启动它。