`
- 浏览:
208068 次
- 性别:
- 来自:
北京
-
先确保你的4444端口没被占用,可以用netstat -an命令查看一下.
然后确保你的jdk版本在1.5以上.
第一步:
找到你下载的selenium解压目录下的selenium-server-0.9.2目录.
在这个目录下写个批处理,内容为:
java -jar selenium-server.jar
保存为start.bat,名字随便啦.双击启动.这个是服务,在一切工作开始之前,
必须先启动这个,启动后的dos窗口不要关闭.
第二步:
在MyEclipse建个web工程,把selenium-java-client-driver-0.9.2目录下的jar包加入
到web工程的lib目录下.加入junit4支持.
第三步:
写个测试index.jsp页:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<html>
<head>
<title>test!</title>
</head>
<body>
<form action="success.jsp" method="post">
UserName:<input type="text" name="username" /><br />
<select name="select">
<option value="game">游戏</option>
<option value="program">编程</option>
</select> <br/>
<input type="submit" name="sub" value="submit"/> <br />
</form>
</body>
</html>
还有一个success.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>success</title>
</head>
<body>
ok!
</body>
</html>
写个测试类:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import static org.junit.Assert.*;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class TestPage
{
private Selenium selenium;
@Before
public void setUp()
{
//此url必须是Selenium服务器地址
String url = "http://localhost:4444";
selenium = new DefaultSelenium("localhost",SeleniumServer.getDefaultPort(),"*iexplore",url);
selenium.start();
}
@After
public void tearDown()
{
try
{
selenium.stop();
} catch (RuntimeException e)
{
System.out.println(e.getMessage());
}
}
//测试标题,文本框输入,及按钮点击
@Test
public void test1()
{
//我这里是tomcat的地址,我的tomcat端口是8888,selenium是当前工程,我让它打开首页
selenium.open("http://localhost:8888/selenium/index.jsp");
String title = selenium.getTitle();
//原来网页的标题
System.out.println(title);
selenium.type("xpath=//input[@name='username']", "zdw");
//得到输入的文本框的值
System.out.println("textvalue:" + selenium.getValue("xpath=//input[@name='username']"));
selenium.click("xpath=//input[@name='sub']");
selenium.waitForPageToLoad("4000");
assertEquals(title, "test!");
//输出新页的标题
System.out.println(selenium.getTitle());
}
//测试选择框
@Test
public void testSelect()
{
selenium.open("http://localhost:8888/selenium/index.jsp");
selenium.select("xpath=//select[@name='select']", "index=1");
//得到选择的id
System.out.println("selectid:" + selenium.getSelectedIndex("xpath=//select[@name='select']"));
//得到选择的值
System.out.println("selectvalue:" + selenium.getSelectedValue("xpath=//select[@name='select']"));
selenium.click("xpath=//input[@type='submit']");
selenium.waitForPageToLoad("3000");
}
}
注释已经很详细了,感觉selenium很好用,完全可以模拟浏览器操作.我这里用的是ie,你当然可以用firefox或其它.
源码可在我的网盘下载.
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium selenium
selenium+junit+hudson自动化测试初探
Selenium_v2.5 Selenium_v2.5 Selenium_v2.5 Selenium_v2.5Selenium_v2.5 Selenium_v2.5
selenium selenium selenium
selenium RC与selenium webdriver的区别
Selenium是ThoughtWorks公司开发的一套基于WEB应用的测试工具,直接运行在浏览器中,模拟用户的操作,主要包括三个部分selenium-IDE,selenium-core,selenium-rc 。它可以被用于单元测试,回归测试,冒烟测试,集成...
Selenium
由于Selenium Server在启动浏览器时做了手脚,所以Selenium Server会接收到所有由它启动的浏览器发送的请求。 (6).Selenium Server接收到浏览器的发送的Http请求后,自己重组Http请求,获取对应的Web页面。 (7)....
Selenium IDE 插件
selenium
Selenium 服务器(selenium-server-4.1.1.jar)
Selenium 官方中文文档
昨天群里有朋友问我selenium入门例子,我今天抽了点时间写了一段简单的代码,此代码适合刚刚学习selenium的人员参考,此代码是selenium2 RC 调用chrome driver访问百度,输入Jack_test 搜索. 代码里面含资源包:selenium...
java+selenium实现web自动化测试需要用到的jar包。不过某些情况下Selenium-server-standalone包即可,Selenium-java没有使用也可以运行
Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案例(效果类似数据驱动测试),它是Selenium IDE和Selenium RC的引擎。 2....
在c# 中应用selenium的一些写法
selenium.jar selenium资源包
1. Download the latest version of Selenium RC 2. Unzip the file save it C:\selenium-remote-control-1.0.3 3. command prompt>java -version –> Version should grater than 1.6.0_5 4. command prompt go to ...
testng生成的报告乱码,换成了selenium 的2.18的jar包
selenium 是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。selenium 是...