不同浏览器下,创建Java + Selenium工程

来源:互联网 发布:室内设计网络培训 编辑:程序博客网 时间:2024/06/05 07:02

不同浏览器下,创建Selenium工程

Selenium-WebDriver 依赖浏览器内建的自动化支持,直接驱动浏览器。Selenium-RC(elenium1.0)则是在启动浏览器时,注入javascript函数实现。

Selenium2.0 = Selenium1.0 + WebDriver(也就是说Selenium2.0合并了这两个项目)

selenium的一个介绍文档非常详细,这里附上地址: 
http://qa.blog.163.com/blog/static/19014700220122231779/

下面分别介绍了Firefox,InternetExplorer和Chrome的简单测试的搭建。

使用maven配置测试环境

使用maven来管理selenium的java工程是最方便的。在Maven的中央仓库中包含了所有的jar包,地址如下:http://repo1.maven.org/maven2/org/seleniumhq/selenium/

如果需要使用Selenium RC 、Remote WebDriver 或者 Grid 2,请引用jar包org.seleniumhq.selenium:selenium-server-standalone。如果只需要本地运行,则可以只添加selenium-java的jar包。

 <dependency>       <groupId>org.seleniumhq.selenium</groupId>       <artifactId>selenium-java</artifactId>       <version>2.41.0</version>   </dependency>  

Firefox

Firefox中的FirefoxDriver以xpi的形式提供,在启动FirefoxDriver时自动加载。该.xpi已经在selenium-firefox-driver-xxx.jar中的org.openqa.selenium.firefox下提供了。不需要我们额外处理。只要引用了selenium的jar包自然就包含进来了。

具体见https://code.google.com/p/selenium/wiki/FirefoxDriver

如下参数为ff可能需要设置的:

Property                            What it meanswebdriver.accept.untrusted.certs     ?webdriver.assume.untrusted.issuer    ?webdriver.development               (不建议使用)产品处于开发状态。webdriver.enable.native.events       ?webdriver.firefox.bin               用来控制ff的bin代码位置webdriver.firefox.port              ?webdriver.firefox.profile           火狐配置文件的位置。默认创建匿名配置文件。webdriver.firefox.useExisting       (不建议使用)在存在浏览器实例时,使用当前实例。webdriver.log.file                  javascript控制台日志的导出位置。webdriver.firefox.logfile           stdout/stderr日志的导出位置。webdriver.reap_profile              如果临时文件和配置不应该删除,设为true

FF浏览器的默认位置(如果没有安装到默认位置,需要设置webdriver.firefox.bin)

OS        Expected Location of FirefoxLinux    firefox (found using "which")Mac      /Applications/Firefox.app/Contents/MacOS/firefox-binWindows  %PROGRAMFILES%\Mozilla Firefox\firefox.exe

Demo如下:

public static void main(String[] args) {    String path = "http://www.baidu.com/";    WebDriver driver = new FirefoxDriver();    driver.get(path);    WebElement inputbox = driver.findElement(By.id("kw1"));    WebElement submitbox = driver.findElement(By.id("su1"));    inputbox.sendKeys("selenium");    submitbox.click();    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {        public Boolean apply(WebDriver d) {            return d.getTitle().toLowerCase().contains("selenium");        }    });    driver.quit();}

InternetExplorer

IE的测试需要InternetExplorerDriver。并且InternetExplorerDriver在XP,Vista和win7下,对IE6-10兼容,并且提供了32位和64位的版本。

IEDriver的说明具体见:https://code.google.com/p/selenium/wiki/InternetExplorerDriver。

InternetExplorerDriver可能需要设置的系统参数如下:

Property                        What it meanswebdriver.ie.driver             IE driver的位置webdriver.ie.driver.host        IE driver监听的IP地址webdriver.ie.driver.loglevel    log的级别:FATAL,ERROR,WARN,INFO,DEBUG,TRACE.                                 默认FATAL.webdriver.ie.driver.logfile     log文件的完整路径和名称webdriver.ie.driver.silent      禁止输出

Selenium启动IE进行页面测试的系统需求: 
1,为了能够启动IE,需要在工程中添加IEDriverServer.exe。 
我在工程下建立了lib文件夹,专门用于放置第三方的引用。

IEDriverServer的下载路径,http://selenium-release.storage.googleapis.com/index.html

2,在IE7以上,需要保证在“Internet Options”中都不要选择“Enable Protected Mode”(保护模式)。 建议使用代码控制,如下:

    //java.lang.IllegalStateException:     //The path to the driver executable must be set     //by the webdriver.ie.driver system property    System.setProperty("webdriver.ie.driver","lib\\IEDriverServer.exe");    //On IE 7 or higher on Windows Vista or Windows 7,     //you must set the Protected Mode settings for each zone to be the same value.    DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();    ieCapabilities.setCapability(         InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,         true);    WebDriver driver = new InternetExplorerDriver(ieCapabilities);    String path = "http://www.baidu.com/";    driver.get(path);    WebElement inputbox = driver.findElement(By.id("kw1"));    WebElement submitbox = driver.findElement(By.id("su1"));    inputbox.sendKeys("selenium");    submitbox.click();

Chrome

chromedriver是一个独立的server,由Chromium项目组协助开发的。ChromeDriver由三个部件组成:chrome浏览器本身,支持ChromeDriver的语言和支持ChromeDriver和Chrome之间通信的工程。同样,我将下载的chromedriver.exe放到了lib下面。

下载地址: http://chromedriver.storage.googleapis.com/index.html

需要注意的是,偶尔会出现之前调试好的测试用例无法执行。因为chrome会自动更新,很有可能是chrome已经更新,与已有的chromedriver不兼容。

Chrome浏览器的默认位置

OS      Expected Location of ChromeLinux       /usr/bin/google-chrome1Mac         /Applications/Google\ Chrome.app/Contents/MacOS/Google\ ChromeWindows XP  %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exeWindows7    C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

demo代码如下:

public static void main(String[] args) {    //The path to the driver executable must be set by     //the webdriver.chrome.driver system property    System.setProperty("webdriver.chrome.driver","lib\\chromedriver.exe");    String path = "http://www.baidu.com/";    WebDriver driver = new ChromeDriver();    driver.get(path);    WebElement inputbox = driver.findElement(By.id("kw1"));    WebElement submitbox = driver.findElement(By.id("su1"));    inputbox.sendKeys("selenium");    submitbox.click();    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {        public Boolean apply(WebDriver d) {            return d.getTitle().toLowerCase().contains("selenium");        }    });    driver.quit();}

我们可以使用一些命令行指令来配置浏览器,类似Firefox。如最大化浏览器:

 DesiredCapabilities capabilities = DesiredCapabilities.chrome();  capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));  WebDriver driver = new ChromeDriver(capabilities);     

Safari

SafariDriver是一个Safari浏览器的扩展。和Firefox类似,在selenium-safari-driver-xxx.jar包中org.openqa.selenium.safari路径下提供了Safaridriver.safariextz扩展程序。

文档说明地址: https://code.google.com/p/selenium/wiki/SafariDriver

可能需要的设置

webdriver.safari.driver      定义SafariDriver扩展程序的位置webdriver.safari.noinstall   设置为true则不安装jar包中提供的SafariDriver扩展程序

使用该浏览器,只需要将driver改成SafariDriver即可:

WebDriver driver = new SafariDriver();

0 0