??? 交流討論:歡迎加入我們一起學(xué)習(xí)!
?? 資源分享:耗時(shí)200+小時(shí)精選的「軟件測(cè)試」資料包
???教程推薦:火遍全網(wǎng)的《軟件測(cè)試》教程??
??歡迎點(diǎn)贊 ?? 收藏 ?留言 ?? 如有錯(cuò)誤敬請(qǐng)指正!
Selenium?名字的來源
在這里,我還想說一下關(guān)于?Selenium?名字的來源,很有意思的?:?>?:?Selenium?的中文名為?“?硒?”?,是一種化學(xué)元素的名字,它?對(duì)?汞?(?Mercury?)有天然的解毒作用,實(shí)驗(yàn)表明汞暴露水平越高,硒對(duì)汞毒性的拮抗作用越明顯,所以說硒是汞的克星。大家應(yīng)該知道?Mercury?測(cè)試工具系?列吧(?QTP?,?QC?,?LR?,?WR...?),他們功能強(qiáng)大,但卻價(jià)格不菲,大家對(duì)此又愛又恨!故?thoughtworks?特意把他們的?Web?開源測(cè)試工具命?名為?Selenium?,以此幫助大家脫離汞毒。
產(chǎn)品類別
Selenium?IDE |
一個(gè)用于構(gòu)造測(cè)試腳本的原型工具。它是一個(gè)Firefox插件,并且提供了一個(gè)易于使用的開發(fā)自動(dòng)化測(cè)試的接口。Selenium?IDE有一個(gè)錄制功能,可以記錄用戶執(zhí)行的動(dòng)作,然后可以導(dǎo)出它們作可重用的腳本 |
Remote?Control |
Selenium?RC是最重要的Seleniumx項(xiàng)目,在WebDriver/Selenium合并產(chǎn)生Selenium?2 |
WebDriver |
Selenium?2是該項(xiàng)目的未來方向,和對(duì)Selenium工具包的最新的增加物。 |
Grid |
如果你必須運(yùn)行你的測(cè)試集在多個(gè)環(huán)境,你可以有不同的遠(yuǎn)程機(jī)器的支持和運(yùn)行你的測(cè)試在同一時(shí)間在不同的遠(yuǎn)程機(jī)器上。在任何一種情形下,Selenium都將充分利用并行處理,極大地改善運(yùn)行你的測(cè)試所花費(fèi)的時(shí)間。 |
瀏覽器支持
官方文檔?http://docs.seleniumhq.org/docs/01_introducing_selenium.jsp#supported-browsers-and-platforms
實(shí)戰(zhàn)操作
準(zhǔn)備
IE?Chrome的Driver安裝和準(zhǔn)備
https://code.google.com/p/selenium/wiki/ChromeDriver
https://code.google.com/p/selenium/wiki/InternetExplorerDriver
RemoteControl的不同瀏覽器Java代碼
package?base;
import?org.openqa.selenium.*;
import?org.openqa.selenium.ie.*;
import?org.openqa.selenium.remote.*;
import?static?org.testng.Assert.*;
import?org.testng.annotations.*;
import?com.thoughtworks.selenium.Selenium;
import?java.io.*;
import?java.net.*;
public?class?BaseRC?{
protected?Selenium?selenium;
private?WebDriver?driver?=?null;
private?StringBuffer?verificationErrors?=?new?StringBuffer();
@Parameters({?"platform",?"browser",?"version",?"url"?})
@BeforeTest(alwaysRun?=?true)
public?void?setup(String?platform,?String?browser,?String?version,
String?url)?throws?MalformedURLException,?IOException?{
DesiredCapabilities?caps?=?null;
//?Browsers
if?(browser.equalsIgnoreCase("Internet?Explorer"))?{
System.setProperty("webdriver.ie.driver",
"c:\\test\\IEDriverServer.exe");
caps?=?DesiredCapabilities.internetExplorer();
//?IE安全設(shè)置
caps.setCapability( InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
//?browser?zoom?level?must?be?set?to?100%
}?else?if?(browser.equalsIgnoreCase("Firefox"))?{
System.setProperty("webdriver.firefox.bin",
"C:\\test\\Firefox4\\firefox.exe");
caps?=?DesiredCapabilities.firefox();
}?else?if?(browser.equalsIgnoreCase("chrome"))?{
System.setProperty("webdriver.chrome.driver",
"c:\\test\\chromedriver.exe");
caps?=?DesiredCapabilities.chrome();
caps.setCapability(
"chrome.binary",
"C:\\test\\Chrome31\\chrome.exe");
}?else?if?(browser.equalsIgnoreCase("iPad"))
caps?=?DesiredCapabilities.ipad();
else?if?(browser.equalsIgnoreCase("Android"))
caps?=?DesiredCapabilities.android();
//?Platforms
if?(platform.equalsIgnoreCase("Windows"))
caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);
else?if?(platform.equalsIgnoreCase("MAC"))
caps.setPlatform(org.openqa.selenium.Platform.MAC);
else?if?(platform.equalsIgnoreCase("Andorid"))
caps.setPlatform(org.openqa.selenium.Platform.ANDROID);
//?Version
caps.setVersion(version);
driver?=?new?RemoteWebDriver(new?URL(
"http://localhost:4444/wd/hub"),?caps);
selenium?=?new?WebDriverBackedSelenium(driver,?url);
}
@AfterTest
public?void?afterTest()?{
//?Close?the?browser
driver.quit();
selenium.stop();
String?verificationErrorString?=?verificationErrors.toString();
if?(!"".equals(verificationErrorString))?{
fail(verificationErrorString);
}
}
}
WebDriver的不同瀏覽器Java代碼
package?base;
import?org.openqa.selenium.*;
import?org.openqa.selenium.firefox.*;
import?org.openqa.selenium.ie.*;
import?org.openqa.selenium.chrome.*;
import?org.openqa.selenium.remote.*;
import?org.openqa.selenium.support.ui.ExpectedCondition;
import?org.openqa.selenium.support.ui.WebDriverWait;
import?static?org.testng.Assert.*;
import?org.testng.annotations.*;
import?java.io.*;
import?java.net.*;
public?class?BaseWebDriver?{
protected?WebDriver?driver?=?null;
private?StringBuffer?verificationErrors?=?new?StringBuffer();
@Parameters({?"platform",?"browser",?"version",?"url"?})
@BeforeTest(alwaysRun?=?true)
public?void?setup(String?platform,?String?browser,?String?version,
String?url)?throws?MalformedURLException,?IOException?{
DesiredCapabilities?caps?=?null;
//?Browsers
if?(browser.equalsIgnoreCase("Internet?Explorer"))?{
System.setProperty("webdriver.ie.driver",
"c:\\test\\IEDriverServer.exe");
caps?=?DesiredCapabilities.internetExplorer();
//?IE安全設(shè)置
caps.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
//?browser?zoom?level?must?be?set?to?100%
}?else?if?(browser.equalsIgnoreCase("Firefox"))?{
System.setProperty("webdriver.firefox.bin",
"C:\\test\\Firefox4\\firefox.exe");
caps?=?DesiredCapabilities.firefox();
}?else?if?(browser.equalsIgnoreCase("chrome"))?{
System.setProperty("webdriver.chrome.driver",
"c:\\test\\chromedriver.exe");
caps?=?DesiredCapabilities.chrome();
caps.setCapability(
"chrome.binary",
"C:\\test\\Chrome31\\chrome.exe");
}?else?if?(browser.equalsIgnoreCase("iPad"))
caps?=?DesiredCapabilities.ipad();
else?if?(browser.equalsIgnoreCase("Android"))
caps?=?DesiredCapabilities.android();
//?Platforms
if?(platform.equalsIgnoreCase("Windows"))
caps.setPlatform(org.openqa.selenium.Platform.WINDOWS);
else?if?(platform.equalsIgnoreCase("MAC"))
caps.setPlatform(org.openqa.selenium.Platform.MAC);
else?if?(platform.equalsIgnoreCase("Andorid"))
caps.setPlatform(org.openqa.selenium.Platform.ANDROID);
//?Version
caps.setVersion(version);
driver?=?new?RemoteWebDriver(new?URL(
"http://localhost:4444/wd/hub"),?caps);
driver.get(url);
//
WebDriverWait?wait?=?new?WebDriverWait(driver,?10);
wait.until(new?ExpectedCondition<WebElement>()?{
@Override
public?WebElement?apply(WebDriver?d)?{
return?d.findElement(By.id("login"));
}
});
}
/* @Test(description?=?"TestDemo")
public?void?testDemo()?throws?InterruptedException?{
//?Sleep?until?the?div?we?want?is?visible?or?5?seconds?is?over
long?end?=?System.currentTimeMillis()?+?5000;
while?(System.currentTimeMillis()?<?end)?{
WebElement?resultsDiv?=?driver.findElement(By.id("container"));
//?If?results?have?been?returned,?the?results?are?displayed?in?a
//?drop?down.
if?(resultsDiv.isDisplayed())?{
break;
}
}
}*/
@AfterTest
public?void?afterTest()?{
//?Close?the?browser
driver.quit();
//driver.close();
String?verificationErrorString?=?verificationErrors.toString();
if?(!"".equals(verificationErrorString))?{
fail(verificationErrorString);
}
}
}
Grid下的不同瀏覽器運(yùn)行腳本
總控運(yùn)行
rem??http://localhost:4444/grid/console?可以查看hub總控的信息
java?-jar?selenium-server-standalone-2.35.0.jar?-role?hub?-port?4444?-nodeTimeout?600
各種瀏覽器運(yùn)行的腳本
參數(shù)設(shè)置相同的部分[IP?RC/WD運(yùn)行模式]
@echo?off
set?a=0
for?%%a?in?(%*)?do?set?/a?a+=1
echo?"%a%?argc"
Rem?可變的設(shè)置
set?PORT=8902?
if?%a%==1?(
if?"%1%"==""?(
?set?IP="localhost"
)?else?(
?set?IP=%1%
)
set?MODE="webdriver"
)?else?(
if?"%1%"==""?(
?set?IP="localhost"
)?else?(
?set?IP=%1%
)
if?"%2%"=="rc"?(
?set?MODE="node"
?set?PORT=9902
)?else?(
?set?MODE="webdriver"
)
)
echo?%IP%?%MODE%
不同瀏覽器的運(yùn)行參數(shù)
java?-Dwebdriver.chrome.driver="c:\test\chromedriver.exe"?-jar?selenium-server-standalone-2.35.0.jar?-role?%MODE%?-hubHost?%IP%?-port?%PORT%?-timeout?20000?-browser?"browserName=chrome,version=31,maxInstances=2,platform=WINDOWS,chrome.binary=C:\test\Chrome31\chrome.exe"
rem?java?-jar?selenium-server-standalone-2.35.0.jar?-h?可以查看幫助參數(shù)
rem?!!!?-browser參數(shù)中,逗號(hào)之間不要有空格
java?-jar?selenium-server-standalone-2.35.0.jar?-role?%MODE%?-hubHost?%IP%?-port?%PORT%?-timeout?20000?-browser?"browserName=firefox,version=4,maxInstances=1,platform=WINDOWS,firefox_binary=C:\test\Firefox4\firefox.exe"
java?-Dwebdriver.ie.driver="c:\test\IEDriverServer.exe"?-jar?selenium-server-standalone-2.35.0.jar?-role?%MODE%?-hubHost?%IP%?-port?%PORT%?-timeout?20000?-browser?"browserName=internet?explorer,version=8,maxInstances=1,platform=WINDOWS"
最后我邀請(qǐng)你進(jìn)入我們的【軟件測(cè)試學(xué)習(xí)交流群:785128166】, 大家可以一起探討交流軟件測(cè)試,共同學(xué)習(xí)軟件測(cè)試技術(shù)、面試等軟件測(cè)試方方面面,還會(huì)有免費(fèi)直播課,收獲更多測(cè)試技巧,我們一起進(jìn)階Python自動(dòng)化測(cè)試/測(cè)試開發(fā),走向高薪之路
作為一個(gè)軟件測(cè)試的過來人,我想盡自己最大的努力,幫助每一個(gè)伙伴都能順利找到工作。所以我整理了下面這份資源,現(xiàn)在免費(fèi)分享給大家,有需要的小伙伴可以關(guān)注【公眾號(hào):程序員二黑】自提!
文章來源:http://www.zghlxwxcb.cn/news/detail-838478.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-838478.html
到了這里,關(guān)于Web自動(dòng)化測(cè)試 Selenium 1/3的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!