第一步 安装JDK
第二步 下载Eclipse
第三步 在Eclipse中安装TestNG
第1种方法:直接安装 Help->Install New Software
name:MyTestNG
location:http://beust.com/eclipse
一路next~~
第2种方法:离线安装
1.下载附件(eclipse-testng离线包.zip),并解压;
2.将解压后的文件..\eclipse-testng离线包\features\目录下的文件夹org.testng.eclipse_6.8.6.20130607_0745放到eclipse–》features目录下;
3.将解压后的文件..\eclipse-testng离线包\org.testng.eclipse_6.8.6.20130607_0745文件夹放到eclipse–》plugins目录下;
4.重启eclipse.
查看一下是否见效:
第四步 下载Selenium IDE、SeleniumRC、IEDriverServer
http://download.csdn.net/detail/wangdan199112/9861374
http://download.csdn.net/detail/wangdan199112/9861379
http://download.csdn.net/detail/wangdan199112/9861382
http://download.csdn.net/detail/wangdan199112/9861384
第五步 下载Firefox、安装Selenium IDE、firebug、Xpath checker、Xpath finder插件
工具–>附加组件,搜索Selenium IDE、firebug、Xpath,安装,重启火狐浏览器。
第六步 启动SeleniumRC
win+r–>cmd–>你的jar包路径
D:\study\tools>java -jar selenium-server-standalone-3.4.0.jar
第七步 Eclipse执行Selenium的Java实例
1.新建java工程:File–>new–>other–>Java Project
项目名:MyTest.java
2.引入Selenium相关的包:
在MyTest上右键,Properties–>Java Build Path–>Libraries–>Add External Jars
selenium-server-standalone-3.4.0.jar
testng-6.8.21.jar
3.新建package和class:
在src上右键,new->package(名称为:Selenium_Java)
在Selenium_Java上右键,new->class(名称为:runasjavaapplication.java)
4.用selenium webdriver写代码如下:
可以打开不同的浏览器,用以开展兼容性测试。
package Selenium_Java;
import java.io.File;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
//import org.testng.Assert;
/**
* @author : zmh
* @version :1.0
* @date :2016年03月06日 下午3:00:22
*/
public class runasjavaapplication {
public static void main(String[] args) throws InterruptedException {
//-----------------------------打开火狐浏览器------------------------------------------------
//WebDriver my_dr = new FirefoxDriver();// 打开火狐浏览器 原生支持的浏览器,但是不支持火狐高级的版本
//-----------------------------打开Chrome浏览器---------------------------------------------
File file_chrome = new File("D:/study/tools/chromedriver/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file_chrome.getAbsolutePath());
//WebDriver my_dr = new ChromeDriver();// 打开chrome浏览器
//-----------------------------打开IE浏览器--------------------------------------------------
File file_ie = new File("D:\\study\\tools\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file_ie.getAbsolutePath());
//为 Internet Explorer 设置安全性功能,否则会遇到一个安全问题提示:"Protected Mode must be set to the same value (enabled or disabled) for all zones"
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver my_dr = new InternetExplorerDriver(caps);// 打开ie浏览器
//WebDriver my_dr = new InternetExplorerDriver();// 打开ie浏览器
//---------------------------------------------------------------------------------------
//打开百度
my_dr.get("www.baidu.com");
Thread.sleep(1000);
//定位到百度的输入框
my_dr.findElement(By.id("kw")).sendKeys("G7物流地图");
Thread.sleep(1000);
//点击搜索
my_dr.findElement(By.id("su")).click();
Thread.sleep(1000);
//打印页面标题
System.out.println(my_dr.getTitle());
//验证页面标题是否符合预期
Assert.assertEquals(my_dr.getTitle(), "G7物流地图_百度搜索");
Thread.sleep(1000);
// 关闭所有webdriver进程,退出
my_dr.quit();
}
}
第八步 TestNG执行Selenium的Java实例
1.新建testng类:在工程上右键,new->other->TestNG(名称为:runastestng.java)
2.写代码:
package Selenium_TestNG;
import java.io.File;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class runastestng {
WebDriver mydr;// 申明全局变量。。。。。
@Test
public void testng001() throws InterruptedException {
//-----------------------------打开IE浏览器--------------------------------------------------
File file_ie = new File("D:\\study\\tools\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file_ie.getAbsolutePath());
//为 Internet Explorer 设置安全性功能,否则会遇到一个安全问题提示:"Protected Mode must be set to the same value (enabled or disabled) for all zones"
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver my_dr = new InternetExplorerDriver(caps);// 打开ie浏览器
//打开百度
my_dr.get("www.baidu.com");
Thread.sleep(1000);
//定位到百度的输入框
my_dr.findElement(By.id("kw")).sendKeys("王丹");
Thread.sleep(1000);
//点击搜索
my_dr.findElement(By.id("su")).click();
Thread.sleep(1000);
//打印页面标题
System.out.println(my_dr.getTitle());
//验证页面标题是否符合预期
Assert.assertEquals(my_dr.getTitle(), "王丹_百度搜索");
Thread.sleep(5000);
}
@Test
public void f() {
}
@BeforeMethod
public void beforeMethod() {
}
@AfterMethod
public void afterMethod() {
}
@BeforeClass
public void beforeClass() {
}
@AfterClass
public void afterClass() {
}
}
3.查看运行报告: