Selenium is a high quality open source test automation tool for web application testing . 是一種高品質的開放源代碼測試自動化的工具, Web應用程式的測試 Selenium runs in Internet Explorer, Mozilla and Firefox on Windows, Linux, and Macintosh, Safari on the Mac, with plans to support Safari on iPhone soon.硒運行在Internet Explorer , Mozilla和Firefox對Windows , Linux和Macintosh上, Safari瀏覽器在Mac ,與計劃支持的Safari ,擁有很快。 Selenium test scripts are portable, can be run from browsers (using Selenium IDE) or from JUNit or NGUnit (using Selenium RC) etc..硒的測試腳本是便攜式,可從本機運行瀏覽器(使用硒的IDE ) ,或從junit或ngunit (使用硒區局)等。 For example, test scripts written using Selenium IDE in Firefox on Windows can run on Firefox in Mac or Linux, without changing any code.舉例來說,測試腳本的書面使用硒的IDE在Firefox在Windows可以運行的Firefox在Mac或Linux ,沒有改變任何代碼。 Selenium tests run directly in browsers and so matches the end-user experience closely.硒的測試運行,直接在瀏覽器和使比賽的最終用戶體驗的緊密合作。

There are three variants of Selenium, which can be used in isolation or in combination to create complete automation test suite for your web applications.有三個變種的硒,可用於在隔離或相結合,以創建完整的自動化測試套件,為您的Web應用程序。

  • Selenium Core - In Selenium Core the tests scripts (written in HTML) and the Selenium Test Runner (written in Javascript) are uploaded to the same web server that hosts the application you are trying to test.硒的核心-在硒為核心的測試腳本(寫在H TML)和硒測試亞軍(書面J avaScript中)是因為上傳到相同的W eb伺服器主機的應用您正試圖考驗。 It is a simpler form of Selenium, and suitable for non-developers, but it has some inherent limitations.這是一個簡單的形式,硒,適合非開發,但也有一些固有的局限性。

  • Selenium IDE - Selenium IDE is a Firefox plugin, which includes the entire Selenium Core, allows you to record, play back, edit, and debug tests in browser.硒的IDE -硒的I DE是一個F irefox的插件,其中包括整個硒的核心,讓您的紀錄,發揮回,編輯和調試測試,在瀏覽器中。 It provides the simplest introduction to Selenium and is highly recommended for beginners.它提供了簡單的介紹了硒和強烈建議初學者。 You can save the tests / test suite created in xml or html format.您可以儲存測試/測試套件中創建的XML或HTML格式。 However to run them in an automated fashion you need Selenium Remote Control which is described next.不過,他們在運行一個自動時裝您需要硒的遠程控制,其中所描述的未來。

  • Selenium Remote Control - The Selenium Remote Control allows you to develop test cases and test suites in Java (supports JUnit & NGUnit), PHP, Ruby, Python, Perl and even .NET.硒遠程控制-硒遠程控制讓您開發測試案例和測試套件在J ava(支持j unit& n gunit) , P HP的,紅寶石,使用P ython, P erl和甚至。淨。 It is the most flexible setup but requires some development knowledge to set up and use.這是最靈活的安裝,但需要一些發展的知識,設置和使用。

  • Selenium Grid - Selenium Grid allows several Selenium Remote Control servers to be accessed in parallel by Selenium Grid server.硒網格-硒的網格,讓幾個硒遠程控制服務器來訪問,在平行硒的網格服務器。 This is extremely useful for automated load and stress testing of web applications.這是非常有用的自動負載和壓力測試的Web應用程序。



Today we will discuss on how you can easily create automatic test scripts using Selenium IDE and convert them to JUnit tests (which uses Selenium Remote Control) which can be added to your JUnit based automatic regression test suite.今天,我們將討論關於如何您可以輕鬆地創建自動測試腳本使用硒IDE和其轉換為junit測試(使用硒遠程控制) ,它可以被添加到您的junit基於自動回歸測試套件。

How to create a test plan in Selenium IDE如何創建一個測試計劃,在硒的IDE


Creating a test plan in Selenium IDE is very easy, so we will use it to create few simple tests to begin with.建立一個測試計劃在硒的IDE是很容易的,所以我們將用它來創建幾個簡單的測試,以開始。

  • Install安裝 Selenium IDE 0.8.7硒的IDE 0.8.7 , a Firefox plugin. ,一個Firefox插件。

  • After installing Selenium please restart your Firefox browser for the plugin to be activated.安裝完畢後硒請重新啟動您的Firefox瀏覽器為插件可以被激活。

  • Now you should see a new added menu item named Selenium IDE under your Firefox Tools menu.現在您應該會看到一個新的補充,菜單項命名為硒的IDE下,您的Firefox 工具菜單上。

  • Open / browse the site for which you want to prepare a test case.開放/瀏覽的網站為你要準備一個測試案例。

  • Start Selenium IDE from Firefox Tools->Selenium IDE .開始硒的IDE從Firefox的工具“ ->硒的I DE。

  • Browse some pages.瀏覽某些網頁。

  • Now click red button to stop recording.現在按一下紅色按鈕來停止錄製。


At this point you will see Selenium automatically recording your actions.在這一點上,您會看到硒自動記錄您的動作。 Carefully note the commands, target and value.仔細注意命令,目標和價值。 You can create and insert your own commands/ modify or even delete them.您可以創建並插入自己的命令/修改,甚至刪除它們。 We will show some examples below.我們將展示一些例子如下。 In the next section we will see how we can modify the generated tests to suit our needs.在下一節中我們將看到我們如何可以修改所產生的測試,以配合我們的需要。

How to create / modify / delete Selenium commands如何創建/修改/刪除硒命令


The default commands generated by Selenium when you are browsing the page as a normal user should be modified to make the test more robust and to add test cases to it.默認命令所產生的硒當您瀏覽網頁作為一個正常的使用者應加以修改,以作出更有力的測試,並加入測試案例,給它。

  • Let's replace all click commands by clickAndWait . click simply clicks the specified link and goes on to execute the next command without waiting.讓我們更換所有單擊命令clickandwait按簡單的點擊指定的鏈接,並下去,執行下一命令而不必等候。 On the other hand clickAndWait waits for the new page to loaded before executing the next command. clickAndWait should be used to make more robust test cases.在另一方面clickandwait等候新的一頁,以裝貨前執行下一命令。 clickandwait應被用來製造更強大的測試案例。

  • Insert assertTextNotPresent command after each clickAndWait command to confirm a text must not be present in the browsed page.插入asserttextnotpresent命令後,每個clickandwait指揮,以確認文本不可目前,在瀏覽網頁。

  • Use assertTextPresent command to confirm a text must be present in the browsed page.使用asserttextpresent指揮,以確認文本必須在場,在瀏覽網頁。

  • Finally to test your test plan please click green arrow button to play from the begining or to play from start point.最後,以測試您的測試計劃,請按一下綠色箭頭按鈕,將發揮從一開始,或從發揮的出發點。

  • Export the test plan as java file by Selenium IDE File->Export Test As->Java - Selenium RC (for example the file name is SeleniumSTSanityTest.java )出口測試計劃作為Java的檔案,硒的IDE 文件> “導出測試-> ”的J ava-硒區局(例如檔案名稱是s e leniumstsanitytest.java)

  • Then close your Firefox Selenium ID.然後關閉您的Firefox硒的ID 。



How to run above test plan (automatically generated java file from Selenium IDE) in command line? 如何運行上述測試計劃(自動生成的Java文件從硒的IDE )在命令行呢?

  • Download下載 Selenium RC硒區局 .

  • Unzip it under the same directory where SeleniumSTSanityTest.java (exported test plan as java file from Selenium ID) was saved.將它解壓縮根據同一目錄下seleniumstsanitytest.java (出口測試計劃作為Java的文件從硒Id )已保存。

  • Install junit.安裝junit 。

  • Go to directory where you unzip selenium-remote-control-1.0-beta-1-dist.zip file.到所在的目錄,你解壓縮硒-遠程控制- 1.0 - β - 1 - dist.zip檔案。

  • Open a terminal and do the steps below-打開一個終端,做下面的步驟-

    • cd selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1裁談會selenium-remote-control-1.0-beta-1/selenium-server-1.0-beta-1

    • java -jar selenium-server.jar (to run the server in interactive mode execute java -jar selenium-server.jar -interactive) Java的罐子硒server.jar (運行服務器在互動模式,執行Java -罐子硒server.jar互動)

    • If you get an error like Error: com.thoughtworks.selenium.SeleniumException: ERROR Server Exception: sessionId should not be null; has this session been started yet? then ensure that the browser is in the PATH before running the server.如果你得到一個錯誤一樣, 錯誤: com.thoughtworks.selenium.seleniumexception :錯誤的服務器例外: sessionid不應該無效;已經本次會議已經開始了沒?然後確保瀏覽器是在道路之前,運行服務器。 For example, you want to run the test in Firefox.舉例來說,您要運行的測試在Firefox 。 Then you should do next two steps.那麼你應該做的下兩個步驟。

    • locate firefox-bin (for example it returns /usr/lib/firefox-1.5.0.12/firefox-bin)找到的Firefox斌(例如它的回報/ usr/lib/firefox-1.5.0.12/firefox-bin )

    • export PATH=$PATH:/usr/lib/firefox-1.5.0.12/firefox-bin;出口的路徑= $路徑: / usr/lib/firefox-1.5.0.12/firefox-bin ;
      Note: There is an alternative way to fix above error (browser is not in path).注:有另一種修正上述錯誤(瀏覽器是不是在路徑) 。 Simply replace chrome with browser PATH in SeleniumSTSanityTest.java file.簡單地取代與瀏覽器的路徑seleniumstsanitytest.java檔案。 For example:例如:
      line
      setUp("http://blog.taragana.com", "*chrome"); 安裝( “ http://blog.taragana.com ” , “ *鉻” ) ;
      becomes成為
      setUp("http://blog.taragana.com", "*firefox /usr/lib/firefox-1.5.0.12/firefox-bin"); 安裝( “ http://blog.taragana.com ” , “ *火狐/ usr/lib/firefox-1.5.0.12/firefox-bin ” ) ;
      in SeleniumSTSanityTest.java .seleniumstsanitytest.java
      To run the test in opera browser replace chrome with opera .運行該測試在Opera瀏覽器取代歌劇


    Now the selenium server is running and you have to run the Java client located in selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1.現在,硒服務器正在運行,你還有運行Java客戶端設在selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1 。

  • Open another terminal.打開另一個終端。

    • export CLASSPATH=.:selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1/selenium-java-client-driver.jar:/usr/share/java/junit.jar出口類=. : selenium-remote-control-1.0-beta-1/selenium-java-client-driver-1.0-beta-1/selenium-java-client-driver.jar :在/ usr / share /華/ junit 。瓦罐

    • javac SeleniumSTSanityTest.java javac seleniumstsanitytest.java

    • java SeleniumSTSanityTest Java的seleniumstsanitytest




The automatically generated java file SeleniumSTSanityTest.java is likely to have some defects.自動生成的Java文件seleniumstsanitytest.java可能有一些缺陷。 Fix it by comparing with the example below:修復它通過比較與下面的例子:




import com.thoughtworks.selenium.*; 進口com.thoughtworks.selenium .* ;
import junit.framework.*; 進口junit.framework .* ;
import java.util.regex.Pattern; 進口java.util.regex.pattern ;

public class SeleniumSTSanityTest extends SeleneseTestCase {公共類seleniumstsanitytest延伸selenesetestcase (
public void setUp() throws Exception {公眾無效安裝( )拋出例外(
setUp("http://blog.taragana.com", "*chrome"); // to run the test in opera replace chrome with opera安裝( “ http://blog.taragana.com的” , “ *鉻” ) ; / /運行測試在Opera取代鉻與歌劇
}
public void testSimpleThoughts() throws Exception {公眾無效testsimplethoughts ( )拋出例外(
selenium.open(""); selenium.open ("");
assertFalse(selenium.isTextPresent("WordPress database error: [")); assertfalse ( selenium.istextpresent ( “在WordPress數據庫錯誤: ["));
assertTrue(selenium.isTextPresent("2003-2008")); asserttrue ( selenium.istextpresent ( “ 2003-2008年” ) ) ;
selenium.open("/index.php/category/programming/java"); selenium.open ( “ /的index.php /類別/編程/ Java的” ) ;
selenium.waitForPageToLoad("30000"); selenium.waitforpagetoload ( “ 30000 ” ) ;
assertFalse(selenium.isTextPresent("WordPress database error: [")); assertfalse ( selenium.istextpresent ( “在WordPress數據庫錯誤: ["));
assertTrue(selenium.isTextPresent("2003-2008")); asserttrue ( selenium.istextpresent ( “ 2003-2008年” ) ) ;
selenium.click("//img[@alt='Übersetzen Sie zum Deutsch/German']"); selenium.click ( “ / /國際管理集團[ @按Alt = ' übersetzen女士zum說明deutsch english /德語']");
selenium.waitForPageToLoad("30000"); selenium.waitforpagetoload ( “ 30000 ” ) ;
assertFalse(selenium.isTextPresent("WordPress database error: [")); assertfalse ( selenium.istextpresent ( “在WordPress數據庫錯誤: ["));
assertTrue(selenium.isTextPresent("2003-")); asserttrue ( selenium.istextpresent ( “ 2003年-"));
selenium.click("//img[@alt='Přeložit do Čech/Czech']"); selenium.click ( “ / /國際管理集團[ @按Alt = ' přeložit做čech /捷克']");
selenium.waitForPageToLoad("60000"); selenium.waitforpagetoload ( “ 60000 ” ) ;
assertFalse(selenium.isTextPresent("WordPress database error: [")); assertfalse ( selenium.istextpresent ( “在WordPress數據庫錯誤: ["));
assertTrue(selenium.isTextPresent("2003")); asserttrue ( selenium.istextpresent ( “ 2003 ” ) ) ;
}

public static Test suite() {公共靜態測試套件( ) (
return new TestSuite(SeleniumSTSanityTest.class);返回新testsuite ( seleniumstsanitytest.class ) ;
}

public static void main(String args[]) {公共靜態無效的主要(字符串args [ ] ) (
junit.textui.TestRunner.run(suite()); junit.textui.testrunner.run (套房( ) ) ;
}
}



Let me know if you have any comments / suggestions on how we can improve this tutorial.讓我知道,如果您有任何意見/建議就如何改善我們的補習。