51Testing软件测试论坛

标题: selenium java自动化测试 [打印本页]

作者: 小小糖    时间: 2018-3-14 13:55
标题: selenium java自动化测试
本篇文章初步介绍使用Java+Selenium进行web的自动化测试, 仅仅是起步!!!!

1. 环境准备

  1.1 Java环境
安装, 配置jdk
  1.2 Java IDE
因为Selenium支持Java/C#/Ruby/Python/Perl等,这里选择用Java。到Eclipse官方网站下载Eclipse,
解压
  1.3 Selenium
到Selenium官方网站http://seleniumhq.org/download/下载Selenium IDE,Selenium Server, Selen
ium Client Driver
      以上三者之间的关系是:
      Selenium IDE可以录制脚本,执行脚本,修改脚本,将录制的脚本导出成其他语言(比如Java
)---本篇不讨论
      Selenium Server是和Selenium Client结合使用的,Server用来启动浏览器,接受Client端的请求
,执行test;
      Client端则是向Server端发送请求。


2. 在Eclipse中编辑并运行test
    2.1 新建一个Java project: File-New-Java Project
    2.2 在上一步建好的项目名称上点鼠标右键, 点击Build Path-Add External Archives...,将下载的
selenium-java-2.21.0.jar(client)和selenium-server-standalone-  2.21.0.jar(server)加进来
    2.3 启动selenium server.进入cmd,运行 "java  -jar selenium-server-standalone-2.21.0.jar" --保
证能找到改jar包。信息如下:
  1. [java] view plaincopy
  2. 信息: Launching a standalone server  
  3. 22:16:21.484 INFO - Java: Sun Microsystems Inc. 17.1-b03  
  4. 22:16:21.500 INFO - OS: Windows XP 5.1 x86  
  5. 22:16:21.515 INFO - v2.21.0, with Core v2.21.0. Built from revision 16552  
  6. 22:16:21.859 INFO - RemoteWebDriver instances should connect to: http://127.0.0.   
  7. 1:4444/wd/hub  
  8. 22:16:21.875 INFO - Version Jetty/5.1.x  
  9. 22:16:21.875 INFO - Started HttpContext[/selenium-server/driver,/selenium-server  
  10. /driver]  
  11. 22:16:21.875 INFO - Started HttpContext[/selenium-server,/selenium-server]  
  12. 22:16:21.875 INFO - Started HttpContext[/,/]  
  13. 22:16:22.062 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@5d173  
  14. 22:16:22.062 INFO - Started HttpContext[/wd,/wd]  
  15. 22:16:22.093 INFO - Started SocketListener on 0.0.0.0:4444  
  16. 22:16:22.093 INFO - Started org.openqa.jetty.jetty.Server@32fb4f  
  17. [java] view plain copy
  18. 信息: Launching a standalone server  
  19. 22:16:21.484 INFO - Java: Sun Microsystems Inc. 17.1-b03  
  20. 22:16:21.500 INFO - OS: Windows XP 5.1 x86  
  21. 22:16:21.515 INFO - v2.21.0, with Core v2.21.0. Built from revision 16552  
  22. 22:16:21.859 INFO - RemoteWebDriver instances should connect to: http://127.0.0.  
  23. 1:4444/wd/hub  
  24. 22:16:21.875 INFO - Version Jetty/5.1.x  
  25. 22:16:21.875 INFO - Started HttpContext[/selenium-server/driver,/selenium-server  
  26. /driver]  
  27. 22:16:21.875 INFO - Started HttpContext[/selenium-server,/selenium-server]  
  28. 22:16:21.875 INFO - Started HttpContext[/,/]  
  29. 22:16:22.062 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@5d173  
  30. 22:16:22.062 INFO - Started HttpContext[/wd,/wd]  
  31. 22:16:22.093 INFO - Started SocketListener on 0.0.0.0:4444  
  32. 22:16:22.093 INFO - Started org.openqa.jetty.jetty.Server@32fb4f  
复制代码




2.4 代码测试

  1. [java] view plaincopyprint?
  2. import com.thoughtworks.selenium.DefaultSelenium;  
  3.    
  4. public class SeleniumDemo01  
  5. {  
  6.   public static void main(String[] args)  
  7.   {  
  8.       String host = "localhost";  
  9.       int port = 4444;  
  10.       String url = "http://www.baidu.com/";  
  11.       String browserType = "*firefox";  
  12.    
  13.       String keyWordsLocator = "document.getElementById('kw')";     
  14.       String search = "document.getElementById('su')";  
  15.       DefaultSelenium selenium = new DefaultSelenium(host,port,browserType,url);  
  16.       selenium.start();  
  17.       selenium.open(url);  
  18.       selenium.type(keyWordsLocator,"java selenium");  
  19.       selenium.click(search);  
  20.       selenium.waitForPageToLoad("50000");  
  21.       selenium.stop();  
  22.       
  23.   }  
  24. }  
  25. 运行结果:  
  26.     1.firefox浏览器期待  
  27.     2.打开百度主页  
  28.     3.在搜索框中键入了"java selenium"  
  29.     4.点击"百度一下"按钮  
  30.     5.等等页面加载50s  
  31.     6.关闭浏览器  
复制代码



作者: 梦想家    时间: 2018-5-14 16:10





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