51Testing软件测试论坛

标题: selenium 入门学习 (一) [打印本页]

作者: 海鸥一飞    时间: 2018-2-7 14:28
标题: selenium 入门学习 (一)
     一、创建maven工程
     在pom.xml依赖中添加selenium的jar包:
  1. <dependency>
  2.     <groupId>org.seleniumhq.selenium</groupId>
  3.     <artifactId>selenium-java</artifactId>
  4.     <version>2.53.1</version>
  5. </dependency>
复制代码
    二、创建第一个seleniumTest类:
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class SeleniumTest {
  4.     public static void main(String[] args) {
  5.         WebDriver webdriver = new ChromeDriver();
  6.         webdriver.get("https://www.baidu.com/");
  7.     }
  8. }
复制代码
     启动测试报错:
  1. Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
  2.     at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
  3.     at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
  4.     at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
  5.     at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
  6.     at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
  7.     at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
  8.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
  9.     at com.pingan.csj.SeleniumTest.main(SeleniumTest.java:8)
复制代码
    报错原因:没有找到chromeWebDriver,下载webDriver放到工程文件下(下载地址:
https://sites.google.com/a/chromium.org/chromedriver/downloads)  
[attach]110681[/attach]
     代码中增加设置WebDriver路径
  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.chrome.ChromeDriver;
  3. public class SeleniumTest {
  4.     public static void main(String[] args) {
  5.                 //配置webDriver路径
  6.         System.setProperty("webdriver.chrome.driver", "D:\\workspace-ta\\csj\\src\\main\\resources\\driver\\chromedriver.exe");
  7.         WebDriver webdriver = new ChromeDriver();
  8.         webdriver.get("https://www.baidu.com/");
  9.     }
  10. }
复制代码
      重新执行;
       启动后报错如下:
  1. Starting ChromeDriver 2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b) on port 39480
  2. Only local connections are allowed.
  3. Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created exception
  4. from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"103372.1","isDefault":true},"id":1,"name":"","origin":"://"}
  5.   (Session info: chrome=59.0.3071.115)
  6.   (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
  7. Command duration or timeout: 3.83 seconds
  8. Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
  9. System info: host: 'PAFSH-D0564', ip: '10.28.81.85', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_80'
  10. Driver info: org.openqa.selenium.chrome.ChromeDriver
  11.     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  12.     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  13.     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  14.     at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
  15.     at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
  16.     at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
  17.     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
  18.     at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
  19.     at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
  20.     at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144)
  21.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:170)
  22.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:159)
  23.     at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
  24.     at com.pingan.csj.SeleniumTest.main(SeleniumTest.java:12)
复制代码
     报错原因:webDriver和chrome版本不匹配;报错session不能创建一般都是这个原因;找到匹配版
本替换webdriver;重新执行;执行成功
      三、对代码进行部分优化
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.concurrent.TimeUnit;

  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.chrome.ChromeDriver;

  6. public class SeleniumTest {
  7.     public static void main(String[] args) throws IOException {
  8.         // 通过查找当前路径返回一个规范化路径,这样可以把工程放到不同地方执行了
  9.         String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
  10.         System.setProperty("webdriver.chrome.driver", driverPath);
  11.         WebDriver webDriver = new ChromeDriver();
  12.         webDriver.get("https://www.baidu.com/");
  13.         webDriver.close();
  14.     }
  15. }
复制代码



作者: 梦想家    时间: 2018-5-14 17:05





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2