Java-Selenium启动环境配置
来源:互联网 发布:农村淘宝亏了好几万 编辑:程序博客网 时间:2024/04/28 14:57
Selenium可以与FireFox , Chrome, IE,Opera, Safari,等常用的浏览器搭配,每种浏览器的启动方式雷同,共同要注意的问题是:
1、浏览器版本尽可能不要太新,但也不能太低。
2、Chrome和IE浏览器要安装相应 driver插件(http://docs.seleniumhq.org/download/)
FireFox(不需要下载驱动,原生支持)
Opera, Safari 本人没用过,具体不清楚
下面通过代码来演示如何打开(基于JAVA,基于Python的程序请参考我的另外一系列文章Python-Selenium2 Web自动化测试)
因为本人主要用FireFox为主,所以Firefox遇到的问题基本都解决了,其他浏览器没遇到太多问题,所以只能给出简单的示范。若需要请留言,我再去看看。
一、开发环境:
1、JDK1.8
2、Eclipse:下载地址:http://www.eclipse.org/downloads/
3、Selenium:selenium-java-2.40.0.zip,下载地址:http://download.csdn.net/detail/jasonwoolf/9164383
二、Eclipse环境配置:
1、新建Java Project
2、将下载好的selenium-java-2.40.0.zip 解压缩到java project的目录,和src,bin 这2个文件夹同级的目录
3、右键project 然后Build Path--> config build path-->Java Build Path-->Libraries-->Add JARs
把libs文件夹下的jar包全部添加上,再添加selenium-java-2.39.0和selenium-java-2.39.0-srcs
三、简单测试程序
1、FireFox
1.1、安装在默认路径下:
import org.openqa.selenium.*;public class TestSelenium throws InterruptedException{ public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement element = driver.findElement(By.name("wd")); element .sendKeys("Jason Dean"); WebElement btn = driver.findElement(By.id("su")); btn.click(); Thread.sleep(1000); driver.close(); }}
1.2、不是安装在默认路径下(但是这样打开的FireFox是没有任何插件的):
<pre name="code" class="java">import org.openqa.selenium.*;public class TestSelenium throws InterruptedException{ public static void main(String[] args) { //如果没有默认安装在C盘,需要制定其路径 System.setProperty("webdriver.firefox.bin", "D:/Mozilla firefox/firefox.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement element = driver.findElement(By.name("wd")); element .sendKeys("Jason Dean"); WebElement btn = driver.findElement(By.id("su")); btn.click(); Thread.sleep(1000); driver.close(); }}
1.3 、加载插件
<pre name="code" class="java">import org.openqa.selenium.*;public class TestSelenium throws InterruptedException{ public static void main(String[] args) throws InterruptedException{ File file = new File("files/firebug-2.0.7-fx.xpi"); FirefoxProfile profile = new FirefoxProfile(); try { profile.addExtension(file); } catch (IOException e) { e.printStackTrace(); } profile.setPreference("extensions.firebug.currentVersion", "2.0.7"); profile.setPreference("extensions.firebug.allPagesActivation", "on"); WebDriver driver = new FirefoxDriver(); driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement element = driver.findElement(By.name("wd")); element.sendKeys("Jason Dean"); WebElement btn = driver.findElement(By.id("su")); btn.click(); Thread.sleep(1000); driver.close(); }}
1.4、 启动本机器的firefox配置(在firefox地址栏中输入about:config,可以查看firefox的参数):
import org.openqa.selenium.*;public class TestSelenium throws InterruptedException{ public static void main(String[] args) throws InterruptedException{ WebDriver driver = new FirefoxDriver(); ProfilesIni pi = new ProfilesIni(); FirefoxProfile profile = pi.getProfile("default"); driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement element = driver.findElement(By.name("wd")); element.sendKeys("Jason Dean"); WebElement btn = driver.findElement(By.id("su")); btn.click(); Thread.sleep(1000); driver.close(); }}
2、 Chrome 浏览器
import org.openqa.selenium.*;public class TestSelenium throws InterruptedException{ public static void main(String[] args) throws InterruptedException{ System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement txtbox = driver.findElement(By.name("wd")); txtbox.sendKeys("Jason Dean"); WebElement btn = driver.findElement(By.id("su")); btn.click(); Thread.sleep(10000); driver.close(); }}
3、IE
import org.openqa.selenium.*;public class TestSelenium throws InterruptedException{ public static void main(String[] args) throws InterruptedException{ System.setProperty("webdriver.ie.driver", "files\\IEDriverServer.exe"); WebDriver driver = new InternetExplorerDriver(); driver.get("http://www.baidu.com/"); driver.manage().window().maximize(); WebElement txtbox = driver.findElement(By.name("wd")); txtbox.sendKeys("Jason Dean"); WebElement btn = driver.findElement(By.id("su")); btn.click(); Thread.sleep(10000); driver.close(); }}
- Java-Selenium启动环境配置
- selenium +webdriver+java 环境配置
- Java Selenium 环境搭建、启动IE
- 120330 selenium RC 基于java 环境配置
- Selenium 环境配置
- selenium RC 环境配置
- selenium RC 环境配置
- selenium RC 环境配置
- selenium RC 环境配置
- Selenium环境配置
- selenium RC 环境配置
- selenium RC 环境配置
- selenium+python 环境配置
- selenium RC 环境配置
- Python Selenium环境配置
- 【Selenium】idea环境配置
- selenium开发环境配置
- selenium环境配置
- 程序连接不上sqlserver2008数据库
- 寻找第K大元素的八大算法、源码及拓展
- 【小知识点总结】android判断横竖屏
- Cocos2d-x中自定义XML的读取和写入
- 剑指offer—按之字形顺序打印二叉树
- Java-Selenium启动环境配置
- 卡榫函数实现API
- UVa 893 - Y3K Problem
- 南阳oj A+BproblemII Java 解决 题目103
- break 和continue以及return的区别
- 剑指offer—把二叉树打印成多行
- Leetcode87: Find Minimum in Rotated Sorted Array
- c#遍历checkBox控件,并用MessageBox显示出来
- [LeetCode-234] Palindrome Linked List(回文链表、链表中间节点查找)