Selenium(4)

 练习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();
	
	}
}
原文地址:https://www.cnblogs.com/KalosOwen/p/8977033.html