Junit4 如何實現並發測試用例,及 Selenium Grid2 與 Junit4 結合進行並發測試。


這里用一個 sample 展現如何用 Junit 並發測試。

第一個文件:TestBingFa.java

package com.xbosoft.junit4;

import org.junit.Test;

public class TestBingFa {

    @Test
    public void testMethod() {
        System.out.println("test success!");
    }
}

第二個文件:

package com.example.tests;

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;

public class PerformTestBingFa {

    public static void main(String[] args) {
        new Thread() {
            public void run() {
                // JUnitCore.runClasses(new Class[] { TestExample.class }); //這個是說要直接運行某一整個 class 中的所有方法。
                 new JUnitCore().run(Request.method(TestBingFa.class,"testMethod")); //這個是指定 run 那個 class 中的哪個方法
 } }.start(); new Thread() { public void run() { // JUnitCore.runClasses(new Class[] { TestExample.class }); //這個是說要直接運行某一整個 class 中的所有方法。 new JUnitCore().run(Request.method(TestBingFa.class,"testMethod")); //這個是指定 run 那個 class 中的哪個方法  } }.start(); // for (int i = 0; i< 10; i++) // { // new Thread() { // public void run() { // // JUnitCore.runClasses(new Class[] { TestExample.class }); //(1) // new JUnitCore().run(Request.method(TestGrid2.class,"testBenbriaSele01")); //(2) // } // }.start(); // }  } }

這樣就可以了。

於是我們就可以想到,用 Selenium Grid2 進行測試的使用,是不是也可以用這種發法進行?答案淡然是肯定的。

我們假設有兩台機器。

機器一是 gird hub

機器二是 grid console

他們都安裝了相同版本的 Firefox。都是 Windows

根據 Grid2 的特性,如果兩個Windows 系統有同樣 Firfox,如果都注冊到了 hub 上,如果run 兩遍同樣的腳本,他會默認第一次在一個機器上跑,第二次再另外一個機器上跑。
我們就用這個場景看一下下面是怎樣同時開兩個線程,然后讓一個腳本同時在兩個機器的 Firefox 21 上跑的。

文件1: TestGrid.java

它是一個 Selenium WebDriver 腳本。 用 Grid 實現了可以再 RemoteWebDriver 上跑。

過程就是反復4次登陸一個站點 - 輸入用戶>密碼>點擊Submit 來登錄> 點擊 logout 退出。 重復4次。 為了看得清楚,還加了sleep時間。

package com.example.tests;

import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;

public class TestGrid {
    private WebDriver driver;
    private String baseUrl;

    @Before
    public void setUp() throws Exception {
        
        //Use Firefox
        DesiredCapabilities capability = DesiredCapabilities.firefox();
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        
        
        baseUrl = "http://10.1.3.12/";
//        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        Capabilities actualCapabilities = ((RemoteWebDriver)driver).getCapabilities(); 
        System.out.println(actualCapabilities); 
    }

    @Test
    public void testBenbriaSele01() throws Exception {
        int sleepTime = 3000;
        
        System.out.println("before driver get()");
        driver.get(baseUrl);
        System.out.println("After driver get()");
        //First time
        driver.findElement(By.id("username")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("password")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(sleepTime);
        driver.findElement(By.id("logout")).click();
        
        //Second Time
        Thread.sleep(sleepTime);
        driver.findElement(By.id("username")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("password")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(sleepTime);
        driver.findElement(By.id("logout")).click();
        
        //Third Time
        Thread.sleep(sleepTime);
        driver.findElement(By.id("username")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("password")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(sleepTime);
        driver.findElement(By.id("logout")).click();
        Thread.sleep(sleepTime);
        
        //Forth Time
        Thread.sleep(sleepTime);
        driver.findElement(By.id("username")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("password")).sendKeys("admin");
        Thread.sleep(sleepTime);
        driver.findElement(By.id("login_submit")).click();
        Thread.sleep(sleepTime);
        driver.findElement(By.id("logout")).click();
        Thread.sleep(sleepTime);
    }

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

}

第二個文件: 就是創建了兩個線程,然后調用 TestGrid 中的 testBenbriaSele01 方法,並同時在兩個瀏覽器中執行。

package com.example.tests;

import org.junit.runner.JUnitCore;
import org.junit.runner.Request;

public class PerformTestBingFa {

    public static void main(String[] args) {
        new Thread() {
            public void run() {
                // JUnitCore.runClasses(new Class[] { TestExample.class }); //(1)
                 new JUnitCore().run(Request.method(TestGrid.class,"testBenbriaSele01")); //(2)
            }
        }.start();
        new Thread() {
            public void run() {
                // JUnitCore.runClasses(new Class[] { TestExample.class }); //(1)
                 new JUnitCore().run(Request.method(TestGrid.class,"testBenbriaSele01")); //(2)
            }
        }.start();

        
//        for (int i = 0; i< 10; i++)
//        {
//            new Thread() {
//                public void run() {
//                    // JUnitCore.runClasses(new Class[] { TestExample.class }); //(1)
//                     new JUnitCore().run(Request.method(TestGrid2.class,"testBenbriaSele01")); //(2)
//                }
//            }.start();
//        }
    }
}

在 Junit 4 中,當然也提供了最新的處於試驗階段的並發方法。如下:

package com.xbosoft.blackline;

import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;

public class PerformTestBingFa2 {

       @Test  
       public void test() {      
          Class[] cls={TestAll.class,TestGrid2.class };
          
          //Parallel among classes  
          JUnitCore.runClasses(ParallelComputer.classes(), cls);  

//          //Parallel among methods in a class  
//          JUnitCore.runClasses(ParallelComputer.methods(), cls);  

//          //Parallel all methods in all classes  
//          JUnitCore.runClasses(new ParallelComputer(true, true), cls);     
       
       }
}

 

That's all.

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM