Selenium Webdriver自動化測試,初學者可以使用selenium ide錄制腳本,然后生成java程序導入eclipse中調試運行!當然錄制出來的東西回放不一定能成功,還需要手工去修改;selenium自動化測試工具,但是特殊情況下也可以用來測試性能;先來介紹一下selenium 如何啟動遠程pc上的瀏覽器進行測試!
啟動遠程pc瀏覽器之前,需要下載selenium-server-standalone-2.40.0.jar,
1、主機端cmd下運行命令:
java -jar selenium-server-standalone-2.40.0.jar -role hub
2、遠程pc機cmd下運行命令:
java -jar selenium-server-standalone-2.40.0.jar -Dwebdriver.firefox.bin="E:Mozilla Firefoxfirefox.exe" -role webdriver -hub http://10.30.12.110:4444/grid/register -browser browserName=firefox -port 7777
(Dwebdriver.firefox.bin="E:Mozilla Firefoxfirefox.exe是遠程pc機瀏覽器安裝路徑;http://10.30.12.110:4444是主機地址和hub端口;節(jié)點端口7777不能和主機端口重復)
實例代碼如下:
import java.io.File; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.Platform; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; 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.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; import net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main; public class TestLogin implements Runnable { public static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS"); @Test public void run() { System.out.println(Thread.currentThread().getId()+sf.format(new Date())); DesiredCapabilities capability = DesiredCapabilities.firefox(); // capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); //設置用來匹配node中要使用的瀏覽器 capability.setBrowserName("firefox"); capability.setVersion("24"); capability.setPlatform(Platform.WINDOWS); WebDriver driver = null; String baseUrl = "http://XX.XX.XX.XX:9080/cas/login"; //設置本地驅動,如果你實例化Driver的時候是"WebDriver driver = new InternetExplorerDriver(capability)"這種方式,必須設置 //System.setProperty("webdriver.ie.driver","D:\IEDriverServer.exe"); try{ //本地啟動瀏覽器 // driver = new FirefoxDriver(capability); //遠程啟動瀏覽器 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability); System.out.println(Thread.currentThread().getId()+"訪問網頁開始時間:"+sf.format(new Date())); driver.get(baseUrl); //打開網頁 try { //等待頁面打開,超時設置10秒 WebElement loginAccount = new WebDriverWait(driver, 10).until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver d) { return d.findElement(By.id("loginAccount")); } }); if(null==loginAccount){ System.out.println(Thread.currentThread().getId()+" Timeout !!!"); driver.quit(); Thread.currentThread().interrupt(); }else{ System.out.println(Thread.currentThread().getId()+"訪問網頁結束時間:"+sf.format(new Date())); loginAccount.clear(); loginAccount.sendKeys("username"); WebElement loginPassword = driver.findElement(By.id("loginPassword")); loginPassword.clear(); loginPassword.sendKeys("password"); WebElement area = driver.findElement(By.cssSelector("area")); System.out.println(Thread.currentThread().getId()+"登錄開始時間:"+sf.format(new Date())); area.click(); try { //等待登錄成功,超時設置10秒 WebElement quxiao = new WebDriverWait(driver, 10).until(new ExpectedCondition<WebElement>() { public WebElement apply(WebDriver d) { return d.findElement(By.xpath(".//*[@class='x-btn-mc']/em/button[text()='取消']")); } }); if(null==quxiao){ System.out.println(Thread.currentThread().getId()+" Loign Timeout !!!"); driver.quit(); Thread.currentThread().interrupt(); }else{ System.out.println(Thread.currentThread().getId()+"登錄成功時間:"+sf.format(new Date())); System.out.println(Thread.currentThread().getId()+"點擊取消時間:"+sf.format(new Date())); quxiao.click(); } } catch (Exception e) { System.out.println(Thread.currentThread().getId()+" Loign Error !!!"); e.printStackTrace(); driver.quit(); Thread.currentThread().interrupt(); } } } catch (Exception e) { System.out.println(Thread.currentThread().getId()+" Visit Error !!!"); e.printStackTrace(); driver.quit(); Thread.currentThread().interrupt(); } }catch (Exception e) { e.printStackTrace(); driver.quit(); }finally{ if(null!=driver){ driver.quit(); } } }
如果先要做遠程多線程并發(fā)測試,將上面的代碼new出了很多實例并且啟動他們,啟動selenium server也需要多加幾個參數(shù):
1、主機端cmd下運行命令:
java -jar selenium-server-standalone-2.40.0.jar -role hub -maxSession 40 -port 4444
(maxSession 設置大連接數(shù))
2、遠程pc機cmd下運行命令:
java -jar selenium-server-standalone-2.40.0.jar -Dwebdriver.firefox.bin="E:Mozilla Firefoxfirefox.exe" -role node -hub http://127.0.0.1:4444/grid/register -maxSession 20 -browser "browserName=firefox,version=24,platform=WINDOWS,maxInstances=20" -port 5555
。╩axInstances是同時運行瀏覽器的數(shù)量)
不過在我實際使用過程中火狐瀏覽器是無法同時實現(xiàn)并發(fā)的,但是IE瀏覽器可以,所以需要把上面火狐的設置改成IE的可以了!不到萬不得已還是不建議使用這種方法進行性能測試!