JDK eclipse selenium 安装以及环境变量的配置

原文地址https://www.cnblogs.com/zmhsoup/p/5249663.html

目录

第一步  安装JDK

第二步 下载Eclipse

第三步 在Eclipse中安装TestNG

第四步 下载Selenium IDE、SeleniumRC、IEDriverServer

第五步 下载Firefox、安装Selenium IDE、firebug、Xpath checker、Xpath finder插件

第六步 启动SeleniumRC

第七步 Eclipse执行Selenium的Java实例

第八步 TestNG执行Selenium的Java实例

下载地址

第一步  安装JDK


下载地址:http://www.oracle.com/technetwork/java/javase/downloads/

1、下载步骤:

2、配置环境变量:

  JAVA_HOME = E:JavaJavajdk1.7.0_15

  PATH = %JAVA_HOME%in;%JAVA_HOME%jrein

  CLASSPATH = .;%JAVA_HOME%lib;%JAVA_HOME%lib ools.jar

  注:上面这行的“.”不能忽略掉了。

3、验证是否安装成功:

第二步 下载Eclipse


下载地址:http://www.eclipse.org/downloads/

第三步 在Eclipse中安装TestNG


第1种方法:直接安装 Help->Install New Software

  

最后重启eclipse.

 第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.

验证方法:file-->new-->other-->TestNg

第四步 下载Selenium IDE、SeleniumRC、IEDriverServer


下载地址:http://www.seleniumhq.org/download/

  1. Selenium IDE:selenium-ide-2.5.0.xpi 用来在Firefox上录制脚本。 

  2.Selenium RC:selenium-server-standalone-2.40.0.jar 模拟服务器端,selenium 1.0执行脚本时需要单独启动该jar包, selenium webdriver无需单独启动。

  3.IEDriverServer:IEDriverServer_Win32_2.40.0.zip IE驱动

这里,将下载得到的所有文件,全存放在d:xxxselenium下面,方便管理:

第五步 下载Firefox、安装Selenium IDE、firebug、Xpath checker、Xpath finder插件


下载地址:http://www.firefox.com.cn/download/

安装完Firefox后,打开Firefox:

1、安装Selenium IDE:

把前面下载的selenium-ide-2.5.0xpi拖放到Firefox,弹出下图后,安装即可。

 

2、安装firebug:工具-->附加组件,搜索firebug、Xpath,安装,重启火狐浏览器。

验证安装成功:

第六步 启动SeleniumRC


selenium 1.0需要启动单独rc,webdriver则不需要启动。(具体原因可自行百度或者参见我其他学习笔记)

启动seleniumRC的方法:
cmd命令行进入selenium-server-standalone-2.40.0.jar存放目录(或者进入selenium-server-standalone-2.40.0.jar存放目录然后在文档路径里输入cmd),然后输入如下命令
java -jar selenium-server-standalone-2.40.0.jar

为了方便,可以写一个批处理文件来执行,Run_selenium.bat,内容如下:

@echo off
cd E:eclipseselenium
E:
java -jar selenium-server-standalone-2.40.0.jar

第七步 Eclipse执行Selenium的Java实例


1.新建java工程:File-->new-->other-->Java Project

输入工程名,完成之后弹出选择视图模式的确认框,可以选NO。

2.引入Selenium相关的包:

在MyTest上右键,Properties-->Java Build Path-->Libraries-->Add External Jars

3.新建package和class:

在src上右键,new->package(名称为:Selenium_Java)

在Selenium_Java上右键,new->class(名称为:runasjavaapplication.java):

4.用selenium webdriver写代码如下:

   可以打开不同的浏览器,用以开展兼容性测试。

   注:用ie浏览器打开时会有个报错:

   

  解决办法是讲注释掉的30-34行的代码取消注释,注释掉36行的代码即可(代码中29行应为:System.setProperty("webdriver.chrome.driver", file_chrome.getAbsolutePath());   46行最好写成:my_dr.get("https://www.baidu.com");)。

复制代码
 1 package Selenium_Java;
 2 
 3 import java.io.File;
 4 
 5 import org.openqa.selenium.By;
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.chrome.ChromeDriver;
 8 import org.openqa.selenium.firefox.FirefoxDriver;
 9 import org.openqa.selenium.ie.InternetExplorerDriver;
10 import org.openqa.selenium.remote.CapabilityType;
11 import org.openqa.selenium.remote.DesiredCapabilities;
12 import org.testng.Assert;
13 
14 
15 /**
16  * @author : zmh
17  * @version :1.0
18  * @date :2016年03月06日 下午3:00:22
19  */
20 public class runasjavaapplication {
21 
22     public static void main(String[] args) throws InterruptedException {
23         
24         //-----------------------------打开火狐浏览器------------------------------------------------
25         //WebDriver my_dr = new FirefoxDriver();// 打开火狐浏览器  原生支持的浏览器,但是不支持火狐高级的版本
26         
27         //-----------------------------打开Chrome浏览器---------------------------------------------
28         File file_chrome = new File("C:/Program Files/Google/Chrome/Application/chromedriver.exe");
29         System.setProperty("webdriver.ie.driver", file_chrome.getAbsolutePath());
30         //WebDriver my_dr = new ChromeDriver();// 打开chrome浏览器
31 
32         //-----------------------------打开IE浏览器--------------------------------------------------
33         File file_ie = new File("C:\Program Files\Internet Explorer\IEDriverServer.exe");
34         System.setProperty("webdriver.ie.driver", file_ie.getAbsolutePath());
35         
36         //为 Internet Explorer 设置安全性功能,否则会遇到一个安全问题提示:"Protected Mode must be set to the same value (enabled or disabled) for all zones"
37         //DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
38         //caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);  
39         //caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
40         //WebDriver my_dr = new InternetExplorerDriver(caps);// 打开ie浏览器
41         
42         WebDriver my_dr = new InternetExplorerDriver();// 打开ie浏览器
43 
44         //---------------------------------------------------------------------------------------
45         //打开百度
46         my_dr.get("www.baidu.com");
47 
48         Thread.sleep(1000);
49         //定位到百度的输入框
50         my_dr.findElement(By.id("kw")).sendKeys("G7物流地图");
51         
52         Thread.sleep(1000);
53         //点击搜索
54         my_dr.findElement(By.id("su")).click();
55         
56         Thread.sleep(1000);
57         //打印页面标题
58         System.out.println(my_dr.getTitle());
59         //验证页面标题是否符合预期
60         Assert.assertEquals(my_dr.getTitle(), "G7物流地图_百度搜索");
61         
62         Thread.sleep(1000);
63         // 关闭所有webdriver进程,退出
64         my_dr.quit();
65     }
66 }
复制代码

5.用selenium1.0写代码如下:

复制代码
 1 package Selenium_Java;
 2 
 3 import com.thoughtworks.selenium.DefaultSelenium;
 4 
 5 /**
 6  * @author : zmh
 7  * @version :1.0
 8  * @date :2016年3月6日 下午12:28:31
 9  */
10 public class runasjavaapplication_selenium1 {
11     public static void main(String[] args) {
12         //创建一个selenium对象,调用DefaultSelenium的构造器,传入参数,参数分别是:host:机器的ip地址、port:端口号、浏览器类型,url:要测试网站的链接
13         DefaultSelenium selenium = new DefaultSelenium("localhost", 4444,
14                 "*iexplore C:\Program Files\Internet Explorer\iexplore.exe",
15                 "http://www.baidu.com/");
16 
17         selenium.start();
18         selenium.open("http://www.baidu.com");
19         selenium.type("id=kw1", "G7物流地图");
20         selenium.click("id=su1");
21         System.out.println("Page title is: " + selenium.getTitle());
22         selenium.stop();
23     }
24 }
复制代码

上面提到的需要打开selenium rc还记得吗?打开之后就可以正常运行了。

第八步 TestNG执行Selenium的Java实例


 1.新建testng类:在工程上右键,new->other->TestNG(名称为:runastestng.java)

创建完成后如下:

2.写代码:

复制代码
 1 package Selenium_TestNG;
 2 
 3 import java.io.File;
 4 
 5 import org.openqa.selenium.By;
 6 import org.openqa.selenium.WebDriver;
 7 import org.openqa.selenium.firefox.FirefoxDriver;
 8 import org.openqa.selenium.ie.InternetExplorerDriver;
 9 import org.openqa.selenium.remote.CapabilityType;
10 import org.openqa.selenium.remote.DesiredCapabilities;
11 import org.testng.Assert;
12 import org.testng.annotations.Test;
13 import org.testng.annotations.BeforeMethod;
14 import org.testng.annotations.AfterMethod;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.AfterClass;
17 
18 /**
19  * @author : zmh
20  * @version :1.0
21  * @date :2016年3月6日 下午12:28:31
22  */
23 
24 public class runastestng {
25     
26     WebDriver mydr;// 申明全局变量。。。。。
27     
28   @Test
29   public void testng001() throws InterruptedException {
30     //-----------------------------打开IE浏览器--------------------------------------------------
31       File file_ie = new File("C:\Program Files\Internet Explorer\IEDriverServer.exe");
32       System.setProperty("webdriver.ie.driver", file_ie.getAbsolutePath());
33         
34       //为 Internet Explorer 设置安全性功能,否则会遇到一个安全问题提示:"Protected Mode must be set to the same value (enabled or disabled) for all zones"
35       DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
36       caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);  
37       caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
38       WebDriver my_dr = new InternetExplorerDriver(caps);// 打开ie浏览器
39       //打开百度
40       my_dr.get("www.baidu.com");
41 
42       Thread.sleep(1000);
43       //定位到百度的输入框
44       my_dr.findElement(By.id("kw")).sendKeys("G7物流地图");
45       Thread.sleep(1000);
46       //点击搜索
47       my_dr.findElement(By.id("su")).click();
48         
49       Thread.sleep(1000);
50       //打印页面标题
51       System.out.println(my_dr.getTitle());
52       //验证页面标题是否符合预期
53       Assert.assertEquals(my_dr.getTitle(), "G7物流地图_百度搜索");
54       Thread.sleep(5000);
55             
56   }
57   @BeforeMethod
58   public void beforeMethod() {
59       //switchTo相关可以写在这里
60   }
61 
62   @AfterMethod
63   public void afterMethod() {
64     // 切换到主窗口、模擬刷新頁面
65   }
66 
67   @BeforeClass
68   public void beforeClass() {
69     //登陆相关可以写在这里
70   }
71 
72   @AfterClass
73   public void afterClass() {
74      //浏览器关闭可以写在这里
75   }
76 
77 }
复制代码

运行如下:

3.查看运行报告:

当然,我们也可以继承IReporter类,使用监听器,达到美化报告的目的,这个以后再另外的笔记中补充。

注:搭建环境涉及到的安装包可以在这里下载,此链接永久有效(缺少包或者分享链接失效的可以私信我去添加和修改):

链接: https://pan.baidu.com/s/1kCvVq-KpCY-c9aOYoukjSQ 密码: hwui

原文地址https://www.cnblogs.com/zmhsoup/p/5249663.html

原文地址:https://www.cnblogs.com/111testing/p/9114029.html