Selenium_IE11_FireFox调用实例

1)使用Selenium调用IE11

    1、前期准备参考博客:http://blog.csdn.net/jichuang123/article/details/53008581

         备注:按照上述博客操作完成后一定要关机再开机,配置的注册表信息才能生效,重启电脑启不到对应的效果。

    2、编写Selenium代码

package com.testng.webdriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterMethod;

public class TestIe {
    public WebDriver driver;
    String baseUrl = "http://www.sogou.com/";
      @Test
      public void testSearch() {
          driver.get(baseUrl);
          driver.findElement(By.id("query")).sendKeys("光荣之路自动化测试");
          driver.findElement(By.id("stb")).click();
      }
      @BeforeMethod
      public void beforeMethod() {
          //设置IE浏览器默认存储位置 
          driver = new InternetExplorerDriver();
          System.setProperty("webdriver.ie.driver", "C:\Program Files (x86)\Internet Explorer\IEDriverServer.exe");
          //设置浏览器为全屏模式
          driver.manage().window().maximize();
      }

      @AfterMethod
      public void afterMethod() {
          //退出浏览器
          driver.quit();
      }
}

2)使用Selenium调用FireFox

     因为Firefox自身已经集成了插件,所以不需要单独下载插件,直接使用如下代码就OK;

  

package com.testng.webdriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestFireFox {
    public WebDriver driver;
    String baseUrl = "http://www.sogou.com/";
      @Test
      public void testSearch() {
          driver.get(baseUrl);
          driver.findElement(By.id("query")).sendKeys("光荣之路自动化测试");
          driver.findElement(By.id("stb")).click();
      }
      @BeforeMethod
      public void beforeMethod() {
          //创建火狐 
          driver = new FirefoxDriver();
          //设置火狐浏览器默认存储位置 
          System.setProperty("webdriver.firefox.bin", "C:\Program Files (x86)\Mozilla Firefox\firefox.exe");
          //设置浏览器为全屏模式
          driver.manage().window().maximize();
      }

      @AfterMethod
      public void afterMethod() {
          //退出浏览器
          driver.quit();
      }
}

 Chrome浏览器如何调用,待整理好补充。

原文地址:https://www.cnblogs.com/xmmc/p/7491974.html