之前的自动化测试脚本一直使用的是firefox浏览器,因为简单方便,不需要额外去安装driver驱动,而且我对firefox的firebug插件也情有独钟,定位元素时使用起来相当方便,但是后期有的项目对chrome浏览器的兼容性最好,使用firefox浏览器会出现元素定位不准确的现象,所以还是需要使用到chrome浏览器进行自动化测试。
1.下载与chrome版本相匹配的chromedriver版本,以下贴出部分版本较新的chrome和chromedriver的版本映射表,我使用的chrome版本为v58,所以下载v2.29的chromedriver版本。下载地址就不贴了,CSDN上资源很丰富。
chromedriver版本 | 支持的Chrome版本 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
2.修改脚本
使用firefox时,测试脚本为:
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class test {
WebElement webElement = null;
WebDriver driver = new FirefoxDriver();
@BeforeTest
public void open() {
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
@Test(priority = 1)
public void searchCSDN() throws InterruptedException {
webElement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
webElement.sendKeys("csdn");
webElement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
webElement.click();
Thread.sleep(3000);
}
}
首先,我将下载的chromedriver.zip文件解压到F:/test文件夹下
selenium使用chrome,网上主要有两种方案:
第一种,将chromedriver.exe路径F:/test/chromedriver.exe添加到path环境变量中,但是运行脚本,报错---失败
第二种,通过webdriver.chrome.driver系统属性实现,修改脚本如下:
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class test {
WebElement webElement = null;
WebDriver driver = new ChromeDriver();
@BeforeTest
public void open() {
// 告诉系统chromedriver.exe的位置
System.setProperty("webdriver.chrome.driver","F:\\test\\chromedriver.exe");
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
@Test(priority = 1)
public void searchCSDN() throws InterruptedException {
webElement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
webElement.sendKeys("csdn");
webElement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
webElement.click();
Thread.sleep(3000);
}
}
运行脚本,仍然报错,提示Cannot instantiate class helloworld.test
百思不得其解啊,后面琢磨了好久,发现原因是下载下来的chromedriver.exe并不是随便放在哪个路径都可以,而是要放在chrome的安装目录下,如将chromedriver.exe放在D:\Program Files (x86)\Google\Chrome\Application下,这个时候,脚本中即使不添加
System.setProperty("webdriver.chrome.driver","D:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
都可以成功运行脚本。
So,最终正确运行的脚本如下:
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class test {
WebElement webElement = null;
WebDriver driver = new ChromeDriver();
@BeforeTest
public void open() {
// 告诉系统chromedriver.exe的位置,可加可不加
System.setProperty("webdriver.chrome.driver","D:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
}
@Test(priority = 1)
public void searchCSDN() throws InterruptedException {
webElement = driver.findElement(By.xpath("//input[@id = \"kw\"]"));
webElement.sendKeys("csdn");
webElement = driver.findElement(By.xpath("//*[@id=\"su\"]"));
webElement.click();
Thread.sleep(3000);
}
}