Selenium的简单例子:

package android1708selenium;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class ExecCase {

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {
        // 设置驱动
        System.setProperty("webdriver.chrome.driver","D:\\software\\win_soft\\selenium\\chromedriver_win32\\chromedriver.exe");
        // 启动浏览器
        WebDriver driver=new ChromeDriver();
        // 访问禅道首页
        driver.get("http://127.0.0.1/index.php");
        // 点击“开源版”
        driver.findElement(By.id("zentao")).click();
        // 在登录页面输入用户名,密码,点击登录按钮
        driver.findElement(By.id("account")).sendKeys("admin");
        driver.findElement(By.name("password")).sendKeys("123456.");
        driver.findElement(By.id("submit")).click();
        // 登录后,点击一级导航的“测试”
        driver.findElement(By.xpath("//nav[@id='mainmenu']/ul/li[4]/a")).click();
        // 点击二级导航的“用例”
        driver.findElement(By.xpath("//nav[@id='modulemenu']/ul/li[3]/a")).click();
        // 点击第一条用例的标题,进入用例的详情页
        driver.findElement(By.xpath("//div[@id='datatable-caseList']/div[2]/div/div/table/tbody/tr[1]/td[4]/a")).click();
        // 在用例的详情页点击“执行”按钮,运行测试用例
        driver.findElement(By.xpath("//a[@class='btn runCase' and @href='/zentao/testtask-runCase-0-65-1.html' and @data-width='95%']")).click();
        // 执行用例加载比较慢,设置一个等待时间,以免找不到元素
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 把webdriver定位到用例执行窗口frame中
        driver.switchTo().frame("modalIframe");
        // 取执行用例之前的用例id,供重新查看时验证用例一致使用
        String execcaseid = driver.findElement(By.xpath("//div[@id='titlebar']/div/span/strong")).getText();
        // 在测试用例执行之前,我取这个测试有用例已存在的结果数量,
        String beforeExecCaseNumberStr = driver.findElement(By.xpath("//div[@id='casesResults']/table/caption/strong/span/span")).getText();
        int beforeExecCaseNumberInt = Integer.parseInt(beforeExecCaseNumberStr);
        // 依次取出用例执行之前的所有记录,并放置一个list中。
        // 定义一个list,里面放执行用例之前的结果数据
        List<String> beforeExecCaselist = new ArrayList();
        for(int i=1;i<(2*beforeExecCaseNumberInt);i=i+2){
            String caseResultXpth = "//div[@id='casesResults']/table/tbody/tr["+i+"]/td[2]";
            String tempstr = driver.findElement(By.xpath(caseResultXpth)).getText();
            beforeExecCaselist.add(tempstr);
        }
        
        // 在用例执行窗口,点击“保存”按钮,执行用例的通过操作。
        driver.findElement(By.id("submit")).click();
        // 点击“保存”按钮后,涉及保存数据的执行,需要增加一个等待时间,以免下一步找不到元素。
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 点击“保存”按钮,即执行用例通过操作。此时frame跳转到下一条用例窗口,需要再返回当前用例窗口
        // 点击窗口中的“上一个”,即返回刚执行的用例窗口
        driver.findElement(By.id("pre")).click();
        // 点击“上一个”按钮后,涉及读取数据的执行,需要增加一个等待时间,以免下一步找不到元素。
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 取跳转窗口的用例id,来验证是不是刚才执行过的用例
        String getbackcaseid = driver.findElement(By.xpath("//div[@id='titlebar']/div/span/strong")).getText();
        if(!getbackcaseid.equals(execcaseid)){
            Assert.assertFalse("用来判断执行是否成功的两个case不一致,无法判断", true);
        }
        // 在测试用例执行之后,我取这个测试有用例已存在的结果数量,
        String behi

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值