selenium环境搭建

本文详细介绍如何使用Selenium进行自动化测试,包括配置JDK 1.8、Eclipse Kepler Service Release 1及Selenium 2.39.0等开发环境,并通过一个具体的示例演示如何实现网页操作自动化。

一、开发环境:

  1、JDK1.8

  2、Eclipse:Version: Kepler Service Release 1,下载地址:http://www.eclipse.org/downloads/

  3、Selenium:selenium-java-2.39.0.zip,下载地址:http://code.google.com/p/selenium/downloads/list

解压selenium-java包,这个包里面包含四部分,如下图:

  

 

二、新建一个Java Project:

  1、然后把上面解压出来的文件拷到新建的project目录下,目录结构如下图:

  

  2、添加build path,项目目录右键-->Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs

  把libs文件夹下的jar包全部添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcs

  

  3、添加完之后目录结构如下图,多了Referenced Libraries,这里就是上面那一步添加进去的jar包:

  

  4、关联webdriver的源码:

  

至此,环境工作准备就绪,下面来写一个简单的小例子。

 

 三、在src下面新建测试类,如下图:

   

代码如下,主要是打开百度,然后在搜索框输入glen,点击搜索按钮,关闭浏览器。

package TestHelloWorld;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.interactions.Actions;
import  static org.junit.Assert.*;

public class Test_zhangshen {
	public  static void main(String[] args) { 
		System.setProperty("webdriver.firefox.bin",
				"E:/Program Files/Mozilla Firefox/firefox.exe");
		WebDriver driver = new FirefoxDriver();
		driver.get(	"https://passport.jd.com/new/login.aspx?ReturnUrl=http%3A%2F%2Fhome.jd.com%2Findex.html");
		WebElement loginname = driver.findElement(By.name("loginname"));
		loginname.click();
		loginname.sendKeys(Constant.name);
		WebElement nloginpwd = driver.findElement(By.name("nloginpwd"));
		nloginpwd.click();
		nloginpwd.sendKeys(Constant.pwd);
		driver.findElement(By.id("loginsubmit")).click();
		//tagName的用法
		List<WebElement> links = driver.findElements(By.tagName("a"));
		//验证链接数量
//		assertEquals(266, links.size()); 
		//linkTest的用法
//		WebElement Linktest = driver.findElement(By.partialLinkText(Constant.my_goods));
		WebElement Linktest = driver.switchTo().frame("_MYJD_tuan").findElement(By.linkText("团购订单"));
		Linktest.click();
		driver.close();

	}
}

 然后直接右键-->Run As-->Java Application就可以看到效果了。

转载于:https://www.cnblogs.com/zhangshen/p/5455193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值