Selenium3.0 自动化测试-简介

原博文地址:点击打开链接

早在2013年的时候,Selenium官方宣布,Selenium新的版本会在圣诞节的时候发布。但是,他们并没有说哪一个圣诞节发布。

  转眼的三年过去了,目前已经发布到Selenium3.0 beta4版本,这将会是Selenium3.0正式版本前的最后一个测试版本。

  尽管我对Selenium3.0比较失望(本以为它会集成移动端的自动化测试)。但是,它还是做了一些变动。

 

Selenium3.0的变化                 

  最大的变化应该是去掉了Selenium RC 了,这是必然的结果。Selenium RC Selenium1.0的产物,Selenium2.0WebDriver为主,经过这么多年有发展,Selenium RC已经很少有人在用了。Selenium3.0版本去掉是个必然的结果。

  • You’ll need to be running Java 8 to use the Java pieces of Selenium. This is the oldest version of Java officially supported by Oracle, so hopefully you’re using it already!

  Selenium3.0只支持Java8版本以上,所以,如果你是用Java+Selenium开发自动化测试,那么Java JDK需要升级到Java8了,对于其它编程来说可以忽略这点,除非你要使用Selenium Grid

  Selenium3.0中的Firefox驱动独立了,在Selenium3.0之前,只要在不同编程语言下安装好Selenium就可以驱动Firefox浏览器运行自动化测试脚本。这是因为不同语言下的Selenium库中移动包含了Firefox浏览驱动。

  然而,现在Firefox浏览器驱动与Selenium库分离,单独提供下载。

  下载地址:https://github.com/mozilla/geckodriver/releases

  不过,geckodriver驱动要求Friefox浏览器必须48版本以上。

  • Support for Safari is provided on macOS (Sierra or later) via Apple’s own safaridriver.

  Safari是苹果公司的浏览器,然后,它也早就实现了多平台的支持,同样可以在Windows下运行,然而,它的驱动比较有意思,是集成到Selenium Server中的。也就是说你想让自动化测试脚本在Safari浏览器上运行,必须使用Selenium Server

  • Support for Edge is provided by MS through their webdriver server.
  • Only versions 9 or above of IE are supported. Earlier versions may work, but are no longer supported as MS no longer supports them.

 

如何使用浏览器驱动                                       

   读者可以单独创建一个目录,如:D:/drivers/ ,把不同浏览器的驱动都放到该目录。geckodriver.exeFirefox)、chromedriver.exeChrome)、MicrosoftWebDriver.exeEdge)、IEDriverServer.exeIE)、operadriver.exeOpera)等。

  然后,将D:/drivers/添加到系统环境变最path下面即可。

 

Python安装Selenium3.0                                                   

 通过pip安装,3.0.0b3为当前最新版本。

>pip install selenium==3.0.0b3

Selenium3.0API没有任何改变,跑个简单的例子验证一下。 

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

driver.find_element_by_id("kw").send_keys("Selenium2")
driver.find_element_by_id("su").click()

driver.quit()

 

Java安装Selenium3.0                                                        

  下载Selenium Server 3.0.0-beta4为当前最新版本:http://www.seleniumhq.org/download/

 

   打开Eclipse,导入:如下图:

 

  同样通过一个简单的例子来验证Selenium3.0工作正常。 


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import  java.io.File;


public class Build_Test {
    public static void main(String[] args) {
        //System.setProperty("webdriver.firefox.bin", "C:\\Users\\guoji\\Desktop\\阅览器驱动\\geckodriver-v0.11.1-win64\\fgeckodriver.exe");\\配置IE阅览器驱动
        File file = new File("C:\\Users\\guoji\\Desktop\\阅览器驱动\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", file.getAbsolutePath()); 
       // WebDriver driver = new FirefoxDriver();
        WebDriver driver = new InternetExplorerDriver();\\IE
        driver.get("http://www.bjguahao.gov.cn/index.htm");
        driver.findElement(By.id("kw")).sendKeys("selenium java");
        driver.findElement(By.id("su")).click();
        driver.quit();
    }
}
出现的问题:

1Exception in thread"main" org.openqa.selenium.WebDriverException: Cannot find firefoxbinary in PATH. Make sure firefox is installed. OS appears to be: XP

Build info:version: '2.16.1', revision: '15405', time: '2012-01-05 12:30:12'
解决办法:

我们只要在WebDriver driver = new FirefoxDriver(); 前面指定我们浏览器的具体信息即可:

System.setProperty( "webdriver.firefox.bin" , "E:/Program Files/MozillaFirefox/firefox.exe" );

WebDriver driver =new FirefoxDriver();

 

2Exception in thread "main"java.lang.IllegalStateException: The path to the driver executable must be setby the webdriver.chrome.driver system property; for more information, seehttp://code.google.com/p/selenium/wiki/ChromeDriver.The latest version can be downloaded fromhttp://code.google.com/p/chromedriver/downloads/list
 at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
 atorg.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
 atorg.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:75)
 atorg.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
 at com.example.tests.Selenium2ForChrome.main(Selenium2ForChrome.java:18)

出现这个错误的原因是因为谷歌浏览器和selenium不是原生的,需要在谷哥里面装插件,插件下载地址是http://code.google.com/p/chromedriver/downloads/list

暂时还没解决好。

 

3The path to the driver executable must be set by thewebdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver.The latest version can be downloaded from http://code.google.com/p/selenium/downloads/list 

该问题的错误原因和上面的一样,用IEdriver的时候也需要装插件,去http://code.google.com/p/selenium/downloads/list 下载对应的插件版本,然后修改代码如下:

[java] view plain copy

1.  File file = new File("C:/Selenium/iexploredriver.exe");  

2.  System.setProperty("webdriver.ie.driver", file.getAbsolutePath());  

3.  WebDriver driver = new InternetExplorerDriver();  

参考来源:http://stackoverflow.com/questions/10995314/driver-executable-must-be-set-by-the-webdriver-ie-driver-system-property

 

4Exception in thread "main"org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launchingInternet Explorer. Browser zoom level was set to 119%. It should be set to 100%(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5.98 seconds

错误原因:IE浏览器的比例调大了,按ctrl+0,可以恢复原来的大小,即可。PS:这种错误真是。。。让人无语。

 

5My97DatePicker控件输入日期问题

之前用的是seleniumIDE自己录制的代码,结果回放的时候总是说元素找不到,整的我很头疼,后来发现一个简单的办法,就是直接把值输入日期控件的输入框当中来,

[java] view plain copy

1.  driver.findElement(By.id("bookDay")).clear();  

2.          driver.findElement(By.id("bookDay")).sendKeys("2013-06-17");  


不过我觉得这个方法不好,还在寻找其他办法。

在网上找了下,有下面这个方法,问题是我看不懂。。。

http://lyh875.blog.163.com/blog/static/21428005820133192552198/

[java] view plain copy

1.  selenium.selectFrame("relative=up");  

2.    //点击日期文本框  

3.    selenium.click("days");  

4.    //必须增加Thread.sleep(),不增加会报错,提示找不到下一条命令中的元素  

5.    //另,尝试使用waitForPageToLoad代替,会超时出错;  

6.    Thread.sleep(5000);  

7.    //当前为4月,向前移两个月  

8.    selenium.click("//div[@id='dpTitle']/div[2]");  

9.    selenium.click("//div[@id='dpTitle']/div[2]");  

10.   //点击2009-02-02  

11.   selenium.click("//td[@οnclick='day_Click(2009,2,2);']");  

 

6、Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function

解决办法:

lib文件夹下的所有包都加到类库里


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值