Selenium Webdriver自動(dòng)化測(cè)試,初學(xué)者可以使用selenium ide錄制腳本,然后生成java程序?qū)雃clipse中調(diào)試運(yùn)行!當(dāng)然錄制出來(lái)的東西回放不一定能成功,還需要手工去修改;selenium自動(dòng)化測(cè)試工具,但是特殊情況下也可以用來(lái)測(cè)試性能;先來(lái)介紹一下selenium 如何啟動(dòng)遠(yuǎn)程pc上的瀏覽器進(jìn)行測(cè)試!
啟動(dòng)遠(yuǎn)程pc瀏覽器之前,需要下載selenium-server-standalone-2.40.0.jar,
1、主機(jī)端cmd下運(yùn)行命令:
java -jar selenium-server-standalone-2.40.0.jar -role hub
2、遠(yuǎn)程pc機(jī)cmd下運(yùn)行命令:
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
。―webdriver.firefox.bin="E:Mozilla Firefoxfirefox.exe是遠(yuǎn)程pc機(jī)瀏覽器安裝路徑;http://10.30.12.110:4444是主機(jī)地址和hub端口;節(jié)點(diǎn)端口7777不能和主機(jī)端口重復(fù))
實(shí)例代碼如下:
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); //設(shè)置用來(lái)匹配node中要使用的瀏覽器 capability.setBrowserName("firefox"); capability.setVersion("24"); capability.setPlatform(Platform.WINDOWS); WebDriver driver = null; String baseUrl = "http://XX.XX.XX.XX:9080/cas/login"; //設(shè)置本地驅(qū)動(dòng),如果你實(shí)例化Driver的時(shí)候是"WebDriver driver = new InternetExplorerDriver(capability)"這種方式,必須設(shè)置 //System.setProperty("webdriver.ie.driver","D:\IEDriverServer.exe"); try{ //本地啟動(dòng)瀏覽器 // driver = new FirefoxDriver(capability); //遠(yuǎn)程啟動(dòng)瀏覽器 driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capability); System.out.println(Thread.currentThread().getId()+"訪(fǎng)問(wèn)網(wǎng)頁(yè)開(kāi)始時(shí)間:"+sf.format(new Date())); driver.get(baseUrl); //打開(kāi)網(wǎng)頁(yè) try { //等待頁(yè)面打開(kāi),超時(shí)設(shè)置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()+"訪(fǎng)問(wèn)網(wǎng)頁(yè)結(jié)束時(shí)間:"+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()+"登錄開(kāi)始時(shí)間:"+sf.format(new Date())); area.click(); try { //等待登錄成功,超時(shí)設(shè)置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()+"登錄成功時(shí)間:"+sf.format(new Date())); System.out.println(Thread.currentThread().getId()+"點(diǎn)擊取消時(shí)間:"+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(); } } }
如果先要做遠(yuǎn)程多線(xiàn)程并發(fā)測(cè)試,將上面的代碼new出了很多實(shí)例并且啟動(dòng)他們,啟動(dòng)selenium server也需要多加幾個(gè)參數(shù):
1、主機(jī)端cmd下運(yùn)行命令:
java -jar selenium-server-standalone-2.40.0.jar -role hub -maxSession 40 -port 4444
(maxSession 設(shè)置大連接數(shù))
2、遠(yuǎn)程pc機(jī)cmd下運(yùn)行命令:
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í)運(yùn)行瀏覽器的數(shù)量)
不過(guò)在我實(shí)際使用過(guò)程中火狐瀏覽器是無(wú)法同時(shí)實(shí)現(xiàn)并發(fā)的,但是IE瀏覽器可以,所以需要把上面火狐的設(shè)置改成IE的可以了!不到萬(wàn)不得已還是不建議使用這種方法進(jìn)行性能測(cè)試!