Selenium WebDriver在Chrome、firefox、IE浏览器上运行测试

Chrome浏览器

 

Chrome浏览器使用名为ChromeDriver.exe 的可执行文件实现WebDriver协议。此可执行文件在系统上启动服务器,而该服务器又负责在Selenium中运行测试脚本。

Google Chrome浏览器中自动执行以下测试方案

 将系统属性“webdriver.chrome.driver” 设置为 ChromeDriver.exe 文件的路径并实例化ChromeDriver类。

如下示例代码

// System Property for Chrome Driver   
System.setProperty("webdriver.chrome.driver", "D:\\software\\webdriver\\chromedriver.exe");

// Instantiate a ChromeDriver class.       
WebDriver driver=new ChromeDriver();

编写完整的代码,每个代码块嵌入了注释,以便清楚地解释这些步骤。

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Third {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // System Property for Chrome Driver
        System.setProperty("webdriver.chrome.driver", "D:\\software\\webdriver\\chromedriver.exe");
        // Instantiate a ChromeDriver class.
        WebDriver driver = new ChromeDriver();
        // Launch Website
        driver.navigate().to("http://www.yiibai.com/");
        // Maximize the browser
        driver.manage().window().maximize();
        // Scroll down the webpage by 5000 pixels
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("scrollBy(0, 100)");
        // Click on the Search button        	driver.findElement(By.linkText("Access教程")).click();
    }
}

 

谷歌浏览器与chromedriver版本对应关系

chromedriver各版本下载地址: http://npm.taobao.org/mirrors/chromedriver

 

Selenium WebDriver-在Firefox浏览器上运行

Gecko一词指的是由Mozilla基金会开发的Gecko浏览器引擎,它用作为Mozilla浏览器的一部分。
Gecko Driver是Selenium和Firefox浏览器中测试之间的链接。 它充当W3C WebDriver兼容客户端(Eclipse,Netbeans等)之间的代理,以与基于Gecko的浏览器(Mozilla Firefox)进行交互。

Marionette(下一代FirefoxDriver)默认从Selenium 3开启。Selenium使用W3C Webdriver协议向GeckoDriver发送请求,GeckoDriver将它们转换为名为Marionette的协议。 即使使用的是旧版本的Firefox浏览器,Selenium 3也希望通过webdriver.gecko.driver设置驱动程序可执行文件的路径。

注意:Selenium 3已升级为现在使用Marionette驱动程序启动Firefox驱动程序,而不是之前支持的默认初始化。

 

在编写测试脚本之前,先来了解如何在Selenium中初始化GeckoDriver。 有三种方法可以初始化GeckoDriver:

1. 使用DesiredCapabilities
首先,需要为Gecko Driver 设置系统属性。

System.setProperty("webdriver.gecko.driver","D:\\software\\webdriver\\geckodriver.exe" );

下面是使用DesiredCapabilities类设置gecko驱动程序的代码。

DesiredCapabilities capabilities = DesiredCapabilities.firefox(); 

capabilities.setCapability("marionette",true);

以下是完整的示例代码 -

System.setProperty("webdriver.gecko.driver","D:\\software\\webdriver\\geckodriver.exe" ); 

DesiredCapabilities capabilities = DesiredCapabilities.firefox(); 

capabilities.setCapability("marionette",true); 

WebDriver driver= new FirefoxDriver(capabilities);

2. 使用 marionette 属性:

Gecko Driver也可以使用marionette属性进行初始化。

System.setProperty("webdriver.firefox.marionette","D:\\software\\webdriver\\geckodriver.exe

此方法不需要 DesiredCapabilities 的代码。

3. 使用Firefox选项:

Firefox 47或更高版本将 marionette 驱动程序作为遗留系统。 因此,可以使用Firefox选项调用 marionette 驱动程序,如下所示。

FirefoxOptions options = new FirefoxOptions(); 

options.setLegacy(true);

编写测试代码,为每个代码块嵌入了注释,以便清楚地解释这些步骤。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class Second {
    public static void main(String[] args) {
        // System Property for Gecko Driver
        System.setProperty("webdriver.gecko.driver", "D:\\software\\WebDriver\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
        WebDriver driver = (WebDriver) new FirefoxDriver();
        // Launch Website
        driver.navigate().to("http://www.yiibai.com/");
        // Click on the Custom Search text box and send value        	driver.findElement(By.name("kw")).sendKeys("java教程");        	driver.findElement(By.id("submit")).click();
        // Click on the Search button
        driver.findElement(By.className("article-list-item-txt")).click();
    }
}

Firefox浏览器和GeckoDriver版本对应关系

geckodriver 各版本下载地址:http://npm.taobao.org/mirrors/geckodriver

Selenium WebDriver-在IE浏览器上运行

Internet Explorer使用Internet Explorer驱动程序服务器实现WebDriver协议。 Internet Explorer驱动程序服务器是Selenium和Internet Explorer浏览器中的测试之间的链接。

下面是一个示例代码

// System Property for IEDriver   
System.setProperty("webdriver.ie.driver", "D:\\IE Driver Server\\IEDriverServer.exe");  
// Instantiate a IEDriver class.
WebDriver driver=new InternetExplorerDriver();

现在是时候编码了,为每个代码块嵌入了注释,以便清楚地解释这些步骤。

import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.ie.InternetExplorerDriver; 
public class Fourth {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // System Property for IEDriver           	System.setProperty("webdriver.ie.driver", "D:\\software\\webdriver\\IEDriverServer.exe");  
           // Instantiate a IEDriver class.    
        WebDriver driver=new InternetExplorerDriver();  
           // Launch Website          	driver.navigate().to("http://www.yiibai.com/");  
           //Maximize the browser            	driver.manage().window().maximize();  
           // Click on the search text box and send value          	driver.findElement(By.id("kw")).sendKeys("java教程");  
           // Click on the search button          	driver.findElement(By.name("submit")).click();  
    }
}