练习1:使用selenium+firefox测试ecshop登录过程

一、WebDriver
  1、启动浏览器
    (1)启动Firefox浏览器
      a.启动默认路径下的浏览器
        WebDriver driver = new FirefoxDriver();
      b.启动非默认路径下的浏览器
        System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
        WebDriver driver = new FirefoxDriver();
    (2)启动Chrome浏览器
      安装chrome浏览器,双击安装
        a.启动默认路径下的chrome浏览器
          //设置系统属性,指定Chrome浏览器的driver
          //System.setProperty("webdriver.chrome.driver",   "C:\\chromedriver.exe");
          //创建Chrome浏览器对象
          //WebDriver driver = new ChromeDriver();
        b.启动非默认路径下的chrome
          System.setProperty("webdriver.chrome.driver",
          "C:\\chromedriver.exe");
          //指定浏览器的路径
          System.setProperty("webdriver.chrome.bin","C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe");
          WebDriver driver = new ChromeDriver();

      练习2:使用Chrome浏览器测试Ecshop搜索功能
        a.选择"手机类型"
        b.输入"9"
        c.点击"搜索"按钮

    (3)启动IE浏览器
      a.先从selenium官网下载IE 浏览器的Driver,注意driver和浏览器的兼容性
      b.放在指定的盘符下,如C盘根目录
      c.使用IE浏览器时,需要设置IE->工具->Internet 选项->高级->勾选"允许活动内容在我的计算机上运行"复选框
      d.在代码中指定driver的路径
        //设置系统属性,指定IE的driver
        //C:\IEDriverServer.exe
        System.setProperty("webdriver.ie.driver", "C:\\IEDriverServer.exe");
        //创建IE浏览器的对象
        WebDriver driver = new InternetExplorerDriver();

      练习3:使用IE浏览器测试Ecshop搜索功能
        a.选择"3G手机"
        b.输入"9"
        c.点击"搜索"按钮
        d.存储搜索结果统计个数在count中
        e.输出count的值

        注意:
          a.如果在运行代码时,控制台提示:IllegalStateException:
        原因:未指定浏览器的driver
        解决:System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

  2、WebDriver结合Junit使用
    (1)解读Junit4 Test Case类(测试类)
      a.@Test:注释为当前方法是测试方法,里面编写测试步骤
      b.@BeforeClass//初始化整个类,只在类加载前执行一次
      c.@AfterClass//释放资源,在类执行完后执行一次
      d.@Before//初始化测试方法(测试用例),在每个测试方法执行前被调用一次
      e.@After//结束测试方法,在每个测试方法执行后被调用一次
    (2)结合目的:
      a.方便实现预期结果的检查
      b.方便查看测试结果
      c.实现测试用例批量执行
      d.管理测试用例
    (3)配置工程,让项目支持Junit4
      a.在工程上右键,选择Build Path->Add Libraries->双击Junit->选择Junit4->Finish

      练习4:使用IDE录制Ecshop登录后退出业务
        a.输入用户名后,存储用户名在uname中
        b.点击"立即登陆"后,断言"退出"链接是否出现,断言页面上方是否显示uname

    (4)将Selenium IDE录制的代码转换为Junit Test Case代码
      a.根据测试用例,使用IDE录制测试步骤
      d.设置IDE运行转换为Junit4的代码:Options->Options->勾选"Enable experimental features"(启用实验性功能)->确定,只需要设置一次
      c.转换:勾选Options->Format->Java / Junit4 /WebDriver选项,如果弹出提示,点击确定
      d.在Source视图中Ctrl+A全选代码,Ctrl+C拷贝代码
      e.在Myeclipse中创建一个Junit Test Case类,把里面的代码都删除,Ctrl+V粘贴从IDE中拷贝的代码
      f.修改调整代码:
        --修改包名与现在包名一致
        --修改类名与现在类名一致
        --检查URL地址,如果是绝对路径,要删除打开被测系统代码路径前的BaseURL变量
        --从IDE中粘贴的代码默认是使用火狐浏览器,如果火狐浏览器的安装路径不是默认路径,需要在@Before方法中添加指定浏览器路径的代码System.setProperty();
      g.运行:右击->Run As->Junit Test
      h.查看结果:在Junit视图结果中查看
        绿色:整个测试代码执行正确
        红色:Error,表示代码编写的有误
        蓝色:Failure,表示程序测试出bug
      i.如果要使用IDE重新录制代码,需要把Format设置HTML

      练习5:录制Ecshop搜索功能
        a.选择"手机类型"
        b.输入"9"
        c.点击"搜索"按钮
        d.验证第一个商品是否为"诺基亚N85"
        e.点击第一个商品
        f.验证"加入购物车"是否出现
        g.把代码转换为Junit代码并执行通过

Junit代码

//谷歌浏览器
package com.day04.www; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; public class TestChrome_check {
public static void main(String[] args) {
//设置系统属性,指定chrome的driver
System.setProperty("webdriver.chrome.driver",
"C:\\chromedriver.exe");
//设置系统属性,指定chrome的安装路径
System.setProperty("webdriver.chrome.bin",
"C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe");
//创建chrome对象
WebDriver driver = new ChromeDriver(); driver.get("file:///D:/Selenium/day01/example/check.html");
driver.findElement(By.id("m2")).click();
driver.findElement(By.id("m3")).click();
driver.findElement(By.id("s2")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭浏览器
driver.close();
}
} //火狐浏览器
package com.day04.www; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class TestFirefox_ecshopLogin {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.bin",
"C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl;
baseUrl="http://localhost/";
driver.get(baseUrl + "/ws/ecshop/upload/index.php");
driver.findElement(By.cssSelector("#ECS_MEMBERZONE > a > img")).click();
driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys("testing");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("123456");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("退出")).click();
driver.quit();
}
} //IE浏览器
package com.day04.www; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select; public class TestIE_ecshopSearch {
//先写main方法
public static void main(String[] args) throws InterruptedException {
//设置系统属性,指定IE的driver
System.setProperty("webdriver.ie.driver",
"C:\\IEDriverServer.exe");
//创建IE浏览器对象
WebDriver driver = new InternetExplorerDriver();
String baseUrl;
baseUrl="http://localhost/"; driver.get(baseUrl + "/ws/ecshop/upload/index.php");
Thread.sleep(5000);
new Select(driver.findElement(By.id("category"))).selectByVisibleText("手机配件");
driver.findElement(By.id("keyword")).clear();
driver.findElement(By.id("keyword")).sendKeys("9");
driver.findElement(By.name("imageField")).click();
//String count = driver.findElement(By.cssSelector("b")).getText();
//System.out.println(count); driver.get(baseUrl + "/ws/ecshop/upload/index.php");
driver.findElement(By.cssSelector("#ECS_MEMBERZONE > a > img")).click();
driver.findElement(By.name("username")).clear();
driver.findElement(By.name("username")).sendKeys("testing");
driver.findElement(By.name("password")).clear();
driver.findElement(By.name("password")).sendKeys("123456");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("退出")).click();
driver.quit(); }
}

Selenium(4)的更多相关文章

  1. 面试准备——(三)Selenium(1)基础问题及自动化测试

    转载:https://www.cnblogs.com/lesleysbw/p/6413880.html 面试准备——(三)Selenium(1)基础问题及自动化测试   滴滴面试: 1. 自己负责哪部 ...

  2. Selenium(Webdriver)自动化测试常问问题

    http://blog.sina.com.cn/s/blog_c189e2590102w3bv.html Selenium(Webdriver)自动化测试常问问题 (1)selenium中如何保证操作 ...

  3. 初识Selenium(三)

    浅谈基于Selenium的Web自动化测试框架 发表于:2011-4-25 10:58  作者:邵育亮   来源:51Testing软件测试网原创 字体:大 中 小 | 上一篇 | 下一篇 | 打印 ...

  4. 初识Selenium(一)

    Selenium入门相关PPT参考网址:http://wenku.baidu.com/view/d1e7d90390c69ec3d5bb7565.html?from=search 内容引用网址:htt ...

  5. py库: Selenium (自动化测试)

    http://blog.csdn.net/liujingqiu/article/details/50458553 http://www.cnblogs.com/zhaof/p/6953241.html ...

  6. selenium(五)伪造浏览器

    简介: 这个就比较好玩了,大家还记得以前的QQ小尾巴么?还有百度贴吧的小尾巴,就是那个来自***的iphone7,这个功能. 这个功能是基于浏览器的user-agent功能实现的. 还是httpbin ...

  7. Selenium(Webdriver)自动化测试常问到的问题解答(转自:潜龙0318)

    今天朋友问我了几个关于Selenium自动化测试的问题,我看了一下感觉还比较典型.结合我以往自动化测试的经验,给出了一些儿粗浅的答案,希望能帮大家,如果大家有什么好的看法,希望相互交流,相互学习! ( ...

  8. Selenium(一)自动化测试简介

    1.软件开发流程 产品分析需求--架构师确认系统包含哪些模块--开发编码--开发和测试一起做单元测试--测试开展版本(集成)测试(使用手工测试,测试通过后,才开始设计脚本)--测试开展系统测试--最后 ...

  9. Selenium(6)

    一.定位页面元素 1.高级定位:层级定位 思路:先定位到祖先节点,在定位该祖先节点范围内的子节点 2.高级定位:Xpath定位(重点) (1)Xpath定位:Xpath就是一个表达式,表示元素的路径, ...

随机推荐

  1. 打开svn时出现 R6034

    An application has made an attempt to load the C runtime library...... 最后发现是因为环境变量path里面有:E:\anacond ...

  2. cocos2dx-android-添加64位编译

    Application.mk: APP_ABI := armeabi arm64-v8a build.gradle: android{ ndk{ abiFilters "armeabi&qu ...

  3. DRF视图-请求与响应

    DRF视图 drf的代码简写除了在数据序列化体现以外,在视图中也是可以的.它在django原有的django.views.View类基础上,drf内部封装了许多子类以便我们使用. Django RES ...

  4. 《精通并发与Netty》学习笔记(05 - Google Protobuf与Netty的结合)

    protobuf是由Google开发的一套对数据结构进行序列化的方法,可用做通信协议,数据存储格式,等等.其特点是不限语言.不限平台.扩展性强 Netty也提供了对Protobuf的天然支持,我们今天 ...

  5. Openstack知识点总结

    Openstack: 一.云计算+openstack概念: 1.云计算是一种按使用量付费的模式,这种模式提供可用的,便捷的,按需的访问,通过互联网进入可配置的计算资源共享池(资源包括网络,计算,存储, ...

  6. pycharm右键运行unittest、pytest文件

    在实际学习过程中,有时候会出现右键运行文件,但没有任何结果的情况.这就是没有使用unittest/pytest 的方式运行. 解决方法: 添加好

  7. [转帖]开源的监控技术栈除了ELK,还有InfluxData的TICK

    开源的监控技术栈除了ELK,还有InfluxData的TICK   https://cloud.tencent.com/developer/news/357119 来源 | Influxdata 译者 ...

  8. Linux系列(11):之bash进阶与数据流导向、管线命令

    1.万用字符与特殊字符 在bash的环境下有一个特别有用的功能,那就是万用字符!下面展示常见的万用字符: 1.万用字符 除了万用字符还有一些其他的特殊字符,接下来显示特殊字符. 2.特殊字符 2.数据 ...

  9. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  10. win7安装mongodb3.6

    1. 下载 https://www.mongodb.com/download-center/enterprise 选择合适平台点击下载 2. 安装mongodb 在win7系统安装mongodb需要v ...