First, download the necessary documents
1. Eclipse
Eclipse official website
2. JDK
JDK website
3. Selenium IDE, selenium Server, selenium Client Drivers (Java), etc.
Selenium Note: A proxy server is required to download I'm using a missus cat.
4. MAVEN installation, configuration, etc.
Second, installation
1, Eclipse decompression can be used
2, JDK installation, configuration variables, etc.
3, Selenium related installation
4. Maven
The latest version of Eclipse already comes with Maven
Third, the Operation
1. Eclipse builds a MAVEN project, after completion, directly modify the Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Selenium2Test</groupId>
<artifactId>Selenium2Test</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.25.0</version>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>0.16</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
</project>
After the Pom.xml is modified, eclipse will automatically download the required jar package.
Configure the required libraries for project engineering
Project Right click Properties->java Build Path->add Library
2. Test Firefox(Exampleforfirefox.java)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExampleForFireFox {
public static void main (String [] args) {
// If your FireFox is not installed in the default directory, it must be set in the program
// System.setProperty ("webdriver.firefox.bin", "D: \\ Program Files \\ Mozilla Firefox \\ firefox.exe");
// Create a browser instance of FireFox
WebDriver driver = new FirefoxDriver ();
// Let the browser visit Baidu
driver.get ("http://www.baidu.com");
// Can also be achieved with the following code
// driver.navigate (). to ("http://www.baidu.com");
// Get the title of the page
System.out.println ("1 Page title is:" + driver.getTitle ());
// Find the input DOM by id
WebElement element = driver.findElement (By.id ("kw"));
// Enter keywords
element.sendKeys ("zTree");
// Submit the form where input is
element.submit ();
// wait for the search page to load by judging the content of the title, with an interval of 10 seconds
(new WebDriverWait (driver, 10)). until (new ExpectedCondition <Boolean> () {
public Boolean apply (WebDriver d) {
return d.getTitle (). toLowerCase (). endsWith ("ztree");
}
});
// Display the title of the search results page
System.out.println ("2 Page title is:" + driver.getTitle ());
// Close the browser
driver.quit ();
}
If you are running normally, you can see the Firefox window open automatically, Access baidu.com, then enter keywords and query
Several problems have arisen in the construction and testing:
Question 1:
Description Resource Path location Type artifacttransferexception:failure to transfer com.google.code.findbug s:jsr305:jar:1.3.9 from Http://repo.maven.apache.org/maven2 is cached in the local repository, resolution would not be REA ttempted until the update interval of the central have elapsed or updates are forced. Original error:could not transfer artifact com.google.code.findbugs:jsr305:jar:1.3.9 from/to Central (HTTP// REPO.MAVEN.APACHE.ORG/MAVEN2): repo.maven.apache.org Pom.xml/lesson line 2 maven Dependency problem
Solution:
May not even http://repo1.maven.org/maven2 this warehouse, in the Pom.xml file add the following configuration try.
XML code:
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo2.maven.org/maven2</url>
</repository>
</repositories>
Question 2:
Ava file (. java) Right-click Run as without Java application
Workaround:
public static void Main (string[] args)
These are essential
Public
Static
void
Main
String[] arg or String arg[]
One of the elements may be missing.
Question 3:
caused by:com.thoughtworks.selenium.SeleniumException:Failed to start new browser session:java.lang.RuntimeException : Browser not supported:http://www.baidu.com (didn't you forget to add a *?)
Analysis: This error indicates that your Firefox file is not installed in the default directory, this time needs to be executed at the beginning: System.setproperty setting environment variable "Webdriver.firefox.bin" will be on your machine Firefox The correct path setting is complete.
Workaround:
If your FireFox is not installed in the default directory, you must set it in the program
System.setproperty ("Webdriver.firefox.bin", "D:\\Program Files\\mozilla firefox\\firefox.exe");
3. Test IE
Selenium is mainly for the Firefox and IE to make, so the Firefox code to modify the IE, it is quite easy, just need to simply two steps:
1) Save the Exampleforfirefox.java as a Exampleforie.java
2) put Webdriver driver = new Firefoxdriver (); Modified to Webdriver Driver = new Internetexplorerdriver ();
3) General Everyone's IE is the default path, so you do not have to set the property
Automated testing "Maven+eclipse+selenium+java environment building and testing"