Selenium 使用要點記錄<一>

1. Selenium 究竟是個啥高級貨?java

Selenium是針對Web application的一個開源的自動化測試框架,其實你們能夠去Selenium官網去仔細看看哈,web

你們能夠順帶去提升哈本身的英語水平。固然selenium雖然開發出來的目的是爲了方便你們自動化測試,可是它的功能不只侷限於這些,只要Selenium適用您的場景,它就能發揮本身的強大功能。好比上次,坐我旁邊的小夥子說:我要研究一個爬蟲算法,去淘寶上把關於某個寶貝的全部評論都抓下來,而後再對評論進行分析balabalabala...通過2個晚上的研究,他仍是發現本身寫算法啊,正則啊去分析爬到的內容取出評論果真有點疼,有點憂傷啊。而後他發現本絲正坐在位置上傻比的看着屏幕,瀏覽器不停的本身作操做,而後很好奇的問我,selenium能不能去taobao頁面上抓點評論啥的。而後我木訥的說:騷年,不要急。容我3min來試一把。算法

String baseUrl = "http://detail.tmall.com/item.htm?spm=a2106.m874.1000384.1.CrEIEH&id=37505213894&source=dou&scm=1029.newlist-0.1.51108009&ppath=&sku=&ug=";
		String ffpath = "F:/ff22/firefox.exe";
		File pathToBinary = new File(ffpath);
		FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
		FirefoxProfile ffProfile = new FirefoxProfile();
		WebDriver driver = new FirefoxDriver(ffBinary,ffProfile);
	
		logger.info("Start to open " + baseUrl);

		driver.get(baseUrl);
		driver.manage().window().maximize();

		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.findElement(By.xpath("//a[@href='#J_Reviews']")).click();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

		List<WebElement> elements = driver.findElements(By.className("tm-rate-fulltxt"));
		for (WebElement element : elements) {
			
			logger.info(element.getText());
		}

反正我是隨便幫他折騰下而後評論就能很方便的取到了,而後這位騷年就表示淡淡的憂傷去了。apache

因此說只要你有了資源,你能去善於利用資源,你就能發現本身有更多的能力,哈哈。瀏覽器

2. Selenium的一些歷史知識,以及我碰到的需求app

其實Selenium有2個比較大的版本,最開的Selenium裏邊使用的是Remote control,若是使用remote control的話就須要額外的去起一個stand alone的server來支持Selenium去啓動瀏覽器。可能考慮到對js支持以及項目的重構吧,後來基本棄用了remote control開始使用WebDriver,固然Selenium org爲這2各類大的版本提供了兼容性的作法,假設有些自動化測試的項目由於某些特殊的功能WebDriver不能提供滿意的效果,不妨能夠試試Remote Control的Selenium接口,WebDriver和Remote Control Selenium兼容以下:框架

  1. WebDriver driver = new FirefoxDriver();
    driver.get(url);
    WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, driver.getCurrentUrl());

Selenium 如今基本上能支持如今各公司主要使用的瀏覽器,具體的支持兼容狀況你們轉link (Selenium 瀏覽器兼容狀況)飛過去瞅瞅哈,本絲就偷個懶再也不整理。jsp

扯完了selenium的歷史小問題,如今吐槽下我碰到的需求問題。maven

第一次弄selenium的時候是用的remote control給一個flex作的項目作一些簡單的功能性測試,基本上是在每次release上線之後測試每一個重要的經常使用元素(如鏈接,輸入框,button)都可以正常工做(其實最重要的是給tester們聲稱report,只有report纔是他們關注的)。其實碰到的主要問題仍是元素的定位和二級menu的選擇問題,可是剛作這些功能的時候是各類問題糾結啊,如今想來WebDriver是使用稍微方便。因爲那個時候對remote control的接觸不是很深刻,功能的對比你們只能去google了。測試

可能真的是緣分的引導,如今項目裏邊down time時間又讓我接着去負責一個項目裏邊的自動化測試項目。我琢磨:嘿,還真是信了這個邪。第二個自動化測試腳本的項目其實也很簡單,跟第一個項目的功能也很相似:按照提供的操做步驟去完成對web application頁面的操做,若失敗則用java mail給team lead group發mail提醒。其實我這工做也不算難,可是咱們須要自動化的那些個步驟真心的是長啊,搞的原本玉樹臨風的我每次下班回家都感受被秋風颳了一遍又一遍的感受。

後邊也會整理在這過程過碰到的一些問題和解決方法。

3. Selenium的簡答使用

其實吧,原本這不是掃盲帖的,可是可能有些同窗看了也想玩一把的話我就寫個簡單的例子。

首先貼上使用selenium WebDriver須要依賴的包,直接奉上maven依賴了:

        <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<selenium.version>2.40.0</selenium.version>
		<selenium.driver.artifactid>selenium-java</selenium.driver.artifactid>
	</properties>

	<dependencies>
		<!-- Selenium dependency -->
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>${selenium.driver.artifactid}</artifactId>
			<version>${selenium.version}</version>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-server</artifactId>
			<version>${selenium.version}</version>
		</dependency>
		<!-- log4j dependency -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- slf4j-log4j -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.5</version>
		</dependency>

		<!-- commons-lang dependency -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.3</version>
		</dependency>
    </dependencies>

測試代碼:

        public void testSelenium() {
		FirefoxProfile ffProfile = new FirefoxProfile();
		WebDriver driver = new FirefoxDriver(ffProfile);
                String baseUrl = "http://www.baidu.com";
		driver.get(baseUrl);
		WebElement search_input = driver.findElement(By.id("kw1"));
		WebElement search_button = driver.findElement(By.id("su1"));
		search_input.sendKeys("饞嘴熊愛吃蜜");
		search_button.click();
		try {
			Thread.sleep(10000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			driver.quit();
		}
	}

這就是簡單的使用啦,我這邊使用的是firefox,若是您在測試的時候錯誤的話能夠去查看:

1) selenium版本和瀏覽器版本不兼容

能夠用以下方式從新指定瀏覽器安裝路徑:

a. 

                String ffpath = "F:/ff22/firefox.exe";
		File pathToBinary = new File(ffpath);
		FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
		FirefoxProfile ffProfile = new FirefoxProfile();
		WebDriver driver = new FirefoxDriver(ffBinary,ffProfile);

b.

System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");

不一樣的瀏覽器去生成WebDriver都有很小的差異,之前Chrome好像還須要額外的導入一個driver包,因爲最近都是使用firefox你們能夠自行Google。

最最簡單的介紹到此結束。原本想直接給記錄本身碰到的問題的,結果花了這麼久才寫了這麼點,只能留後慢慢記啦。

本絲第一次寫blog,但願各路大神指正哈。

相關文章
相關標籤/搜索