selenium入门(环境搭建)

本文主要当作学习笔记记录selenium的环境搭建过程。代码如下:

package com.zzq.test.selenium;

import java.io.File;
import java.net.URI;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class MyWebDriver {

	/**
	 * 测试火狐浏览器:由于WebDriver默认实现了FireFoxDriver,所以无需下载FireFoxDriver就可以进行模拟操作
	 */
	public static void testFireFoxDriver() throws Exception {
		// 指定火狐浏览器的安装路径
		System.setProperty("webdriver.firefox.bin",
				"D:/Program Files/Mozilla Firefox/firefox.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.baidu.com");
		System.out.println("网页标题:" + driver.getTitle());
		WebElement element = driver.findElement(By.name("wd"));
		element.sendKeys("webdriver");
		element.submit();
		System.out.println("网页标题:" + driver.getTitle());
		driver.quit();
	}

	/**
	 * chrome需要自行下载chromedriver.exe。下载地址:http://chromedriver.storage.googleapis.com/index.html
	 * 
	 * @throws Exception
	 */
	public static void testChromeDriver() throws Exception {
		System.setProperty("webdriver.chrome.driver",
				"C:/Users/samsung/AppData/Local/Google/Chrome/Application/chrome.exe");
		URI uri = MyWebDriver.class.getClassLoader()
				.getResource("chromedriver.exe").toURI();
		File file = new File(uri);
		System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
		WebDriver driver = new ChromeDriver();
		driver.get("http://www.baidu.com");
		System.out.println("网页标题:" + driver.getTitle());
		WebElement element = driver.findElement(By.name("wd"));
		element.sendKeys("webdriver");
		element.submit();
		System.out.println("网页标题:" + driver.getTitle());
		driver.quit();
	}
	
	/**
	 * 测试IE浏览器,driver下载地址:http://selenium-release.storage.googleapis.com/index.html?path=2.46/
	 */
	public static void testIE()throws Exception{
		URI uri = MyWebDriver.class.getClassLoader().getResource("IEDriverServer.exe").toURI();
		File file = new File(uri);
		System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
		DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
		capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
		WebDriver driver = new InternetExplorerDriver(capabilities);
		driver.get("http://www.baidu.com");
		System.out.println("网页标题:" + driver.getTitle());
		WebElement element = driver.findElement(By.name("wd"));
		element.sendKeys("webdriver");
		element.submit();
		System.out.println("网页标题:" + driver.getTitle());
		driver.quit();
	}
	
	/**
	 * htmlUnit:无需驱动程序,直接实例化即可
	 */
	public static void testHtmlUnitDriver(){
		WebDriver driver = new HtmlUnitDriver();
		driver.get("http://www.baidu.com");
		System.out.println("网页标题:" + driver.getTitle());
		WebElement element = driver.findElement(By.name("wd"));
		element.sendKeys("webdriver");
		element.submit();
		System.out.println("网页标题:" + driver.getTitle());
		driver.quit();
	}

	public static void main(String[] args) throws Exception {
		// testFireFoxDriver();
		// testChromeDriver();
		//testIE();
		testHtmlUnitDriver();
	}

}

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>com.zzq.selenium</groupId>
	<artifactId>selenium</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-java</artifactId>
			<version>2.45.0</version>
		</dependency>
		<dependency>
			<groupId>org.seleniumhq.selenium</groupId>
			<artifactId>selenium-server</artifactId>
			<version>2.45.0</version>
		</dependency>
	</dependencies>
</project>

完整代码下载:代码下载(驱动程序根据链接下载)

你可能感兴趣的:(selenium入门(环境搭建))