SeleniumThe Ultimate Automated test Environment setup (i.)selenium+eclipse+junit+testngFirst Step InstallationJDK
JDk1.7.
: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
Punch ' next ',OK. Configure environment variables when installation is complete:
Java_home = E:\Java\Java\jdk1.7.0_15
PATH =%java_home%\bin
CLASSPATH =.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar
After configuring the environment variables, theCMD command Line input:java-version, returns the following results, indicating the installation was successful:
Step two DownloadEclipse
: http://www.eclipse.org/download/
The latest Eclipse standard 4.3, 198 MB, downloaded are not installed, extracted directly after the use.
Third Step DownloadSelenium IDE,SELENIUMRC,Iedriverserver,seleniumclient Drivers
: http://www.seleniumhq.org/download/
1 . Selenium IDE:selenium-ide-2.2.0.xpi is used to record scripts on Firefox .
2, Selenium RC:selenium-server-standalone-2.33.0.jar analog server side, not less.
3, iedriverserver:driverserver_win32_2.33.0.zip IE driver,Firfox and the chorm is not driven.
4, Selenium client Drivers:selenium-java-2.33.0.zip analog Selenium clients.
Here, I will download all the files, all stored under E:\eclipse\selenium , easy to manage:
Fourth Step downloadFirefox
: http://www.firefox.com.cn/download/
Download get file:firefox-latest.exe
Fifth Step InstallationIDE,Firebug,Xpath Checker,Xpath Finder
after you install Firefox , open Firefoxand drag and drop the selenium-ide-2.2.0xpi downloaded from the previous Firefox, when it pops up, install it.
Firebug xpath checker , xpath finder open browser, select Tools ―― add-ons, open the Add-Ons Manager page, Search firebug , xpath
Load the query into the firebug,XPath checker,andXPath finder , and take effect after restarting the browser:
Seleniumide,Firebug and XPath usage, can Baidu Selenium Private cuisine (Beginner's introductory tutorial). pdf, which has a good description.
Sixth step StartSELENIUMRC
ways to start SELENIUMRC :
CMD command line into the selenium-server-standalone-2[1].33.0.jar Storage directory, enter the following command
Java-jar Selenium-server-standalone-2[1].12.0.jar
For convenience, the startup command can be written with a bat to execute,run_selenium.bat, as follows:
@echo off
CD E:\eclipse\selenium
E:
Java-jar Selenium-server-standalone-2.33.0.jar
Seventh StepEclipseExecutionSeleniumof theJavaExample
-----7.1
Open Eclipse, create a new project File-new-java Project
-----7.2
Enter project name:selenum,next
-----7.3
Next, window into Java Settings, select Libraries, click addlibrary.
a Jar package (E:\eclipse\plugins\org.junit_4.11.0.v2XXXX)that references Junit4 .
Then click Add External Jars. ,
Referring to Selenium Related packages (E:\eclipse\selenium), the final Libraries is as follows :
When you are finished, theJava view is as follows:
-----7.4
Right-click src,new->package Create a new package selenium_test,
Then right-click the package selenium_test,new->class, Create a new class class Case1.java, the final effect is as follows:
-----7.5
Here we use IE Browser to execute an instance, modify Case1.java, the code is as follows:
Package selenium_test;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.openqa.selenium.ie.InternetExplorerDriver;
Import org.openqa.selenium.remote.DesiredCapabilities;
public class Case1 {
public static void Main (string[] args) {
System.setproperty ("Webdriver.ie.driver",
"E:\\eclipse\\selenium\\iedriverserver.exe");// Note The file storage path IEDriverServer.exe here
Desiredcapabilities iecapabilities = desiredcapabilities
. InternetExplorer ();
Iecapabilities
. setcapability (
Internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains,
true);
Webdriver Driver = new Internetexplorerdriver (iecapabilities);
Driver.get ("http://www.google.com.hk");
webelement element = Driver.findelement (By.name ("Q"));
Element.sendkeys ("Hello selenium!");
Element.submit ();
try {
Thread.Sleep (3000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("page title is:" + driver.gettitle ());
Driver.quit ();
}
}
-----7.6
Run run_selenium.batand start the selenium RC server.
Then right-click case1.java,run As->java application, to perform the following successful results:
Here we run the script through Junit , and the script needs to be modified because junit 's Java file has its own format.
Eighth stepEclipsethroughJunitExecutionSeleniumof theJavaExample
-----8.1
Right-click selenium_test,new->junit Test case to create a new Case2.java.
Complete the following:
-----8.2
Modify the Case2.java code as follows:
Package selenium_test;
Import org.junit.*;
Import org.openqa.selenium.*;
Import Org.openqa.selenium.firefox.FirefoxDriver;
public class Case2 {
Webdriver driver;
@Before
public void SetUp () throws Exception {
Driver = new Firefoxdriver ();
}
@Test
public void Test_case2 () throws Exception {
Driver.get ("http://www.google.com.hk");
webelement element = Driver.findelement (By.name ("Q"));
Element.sendkeys ("Hello selenium!");
Element.submit ();
}
@After
public void TearDown () throws Exception {
System.out.println ("page title is:" + driver.gettitle ());
Driver.quit ();
}
}
-----8.3
Run run_selenium.batand start the selenium RC server ( If it is not turned off after the front RC starts, you do not need to start more than one ).
Right-click case2.java,run as->junit Test, and perform the following successful results:
Nineth StepEclipsethroughTestNGExecutionSeleniumof theJavaExample
-----9.1
Installing TestNG
in the Eclipse , click Help, Install new software , in Add Enter http://beust.com/eclipse in the column and you will see TestNG below. Select Click Install, press Next until the installation is complete , Online installation can be a bit slow.
After installing restart Eclipse , select java->testng in window->show view->other , it will appear TestNG option has been made.
-----9.2
Right-click Package selenium_test,new->other->testng to create a new TestNG test class Case3.java.
Complete the following:
Modify the contents of the Case3.java script as follows:
Package selenium_test;
Import Org.testng.annotations.Test;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import Org.testng.annotations.BeforeMethod;
Import Org.testng.annotations.AfterMethod;
Import Org.openqa.selenium.firefox.FirefoxDriver;
public class Case3 {
Webdriver driver;
@BeforeMethod
public void Beforemethod () {
}
@AfterMethod
public void Aftermethod () {
System.out.println ("page title is:" + driver.gettitle ());
Driver.quit ();
}
@Test
public void Test_case3 () {
Driver = new Firefoxdriver ();
Driver.get ("http://www.google.com.hk");
webelement element = Driver.findelement (By.name ("Q"));
Element.sendkeys ("Hello selenium!");
Element.submit ();
}
}
-----9.3
Run run_selenium.batand start the selenium RC server.
Right-click Case3.java,run as->testng Testto perform the following successful results:
when you finish executing, you will generate a Test-output folders, folders below the index.html is the test report, as follows:
the above is in Eclipse under How to build Selenium test environment, including direct execution . Java , through Junit Execution . Java , through TestNG Execution . Java .
Selenium Ultimate Automated test environment Construction (i) selenium+eclipse+junit+testng