Selenium软件测试-添加断言验证测试结果

通过定义系统关键特征与预期特征比较,判断功能测试是否通过。伴随测试Build不断更新,这些关键特征可以发生变化,需要修改测试脚本,使用Git保存不同版本的测试脚本。

登录用例验证:
登录系统的标题通常唯一,可以通过标题与预期标题比对,如果一致说明系统正常使用。

	driver.get("http://localhost:6060/");
	String title = driver.getTitle();
	assertEquals(title,"古方红糖财务管理系统");

登录成功验证,系统登录后会将登录用户显示在页面上,可以根据页面是否存在登录用户判断是否登录成功。

    //定位登录用户DIV
    String result = driver.findElement(By.id("userdiv")).getText();
    System.out.println("result==="+result);
    assertEquals(result,"当前用户:admin");

在这里插入图片描述
修改预期用户名

    //定位登录用户DIV
    String result = driver.findElement(By.id("userdiv")).getText();
    System.out.println("result==="+result);
    assertEquals(result,"当前用户:admin2");

在这里插入图片描述
登录脚本

package com.gf;

//import org.junit.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class LoginTest {
  private WebDriver driver;
  private Map<String, Object> vars;
  JavascriptExecutor js;
  @Before
  public void setUp() {
    driver = new FirefoxDriver();
    js = (JavascriptExecutor) driver;
    vars = new HashMap<String, Object>();
  }
  @After
  public void tearDown() {
    driver.quit();
  }
  @Test
  public void test(){
	driver.get("http://localhost:6060/");
	String title = driver.getTitle();
	assertEquals(title,"古方红糖财务管理系统");

    driver.manage().window().setSize(new Dimension(1382, 744));
    driver.findElement(By.id("loginId")).click();
    driver.findElement(By.id("loginId")).sendKeys("admin");
    driver.findElement(By.id("pwd")).click();
    driver.findElement(By.id("pwd")).sendKeys("1");
    driver.findElement(By.id("loginbtn")).click();
    String result = driver.findElement(By.id("userdiv")).getText();
    System.out.println("result==="+result);
    assertEquals(result,"当前用户:admin");
    try {
        Thread.sleep(3000);     //让线程等待3秒钟
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    driver.findElement(By.cssSelector(".easyui-linkbutton .l-btn-text")).click();
  }
}


使用Selenium-IDE录制添加组织结构测试用例,并导出为Java单元测试类

package com.gf;

//import org.junit.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
//import org.openqa.selenium.support.ui.ExpectedConditions;
//import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class AddOrgTest {
  private WebDriver driver;
  private Map<String, Object> vars;
  JavascriptExecutor js;
  @Before
  public void setUp() {
    driver = new FirefoxDriver();
    js = (JavascriptExecutor) driver;
    vars = new HashMap<String, Object>();
  }
  @After
  public void tearDown() {
    driver.quit();
  }
  @Test
  public void test(){
	    driver.get("http://localhost:6060/");
	    driver.manage().window().setSize(new Dimension(1382, 744));
	    driver.findElement(By.id("loginId")).click();
	    driver.findElement(By.id("loginId")).sendKeys("admin");
	    driver.findElement(By.id("pwd")).click();
	    driver.findElement(By.id("pwd")).sendKeys("1");
	    driver.findElement(By.id("loginbtn")).click();
	    
	    driver.findElement(By.cssSelector(".accordion-expand")).click();
	    driver.findElement(By.cssSelector("#\\_easyui_tree_4 > .tree-title")).click();
	    driver.switchTo().frame(0);
	    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	    driver.findElement(By.cssSelector(".tree-hit")).click();
	    driver.findElement(By.cssSelector(".tree-collapsed")).click();
	    driver.findElement(By.cssSelector(".tree-title:nth-child(4)")).click();
	    driver.findElement(By.id("_easyui_textbox_input1")).clear();
	    driver.findElement(By.id("_easyui_textbox_input1")).sendKeys("Test3");
	    driver.findElement(By.id("_easyui_textbox_input2")).clear();
	    driver.findElement(By.id("_easyui_textbox_input2")).sendKeys("1");
	    driver.findElement(By.xpath("//a[@id=\'btn\']/span/span")).click();
	    try {
	        Thread.sleep(3000);
	    } catch (InterruptedException e) {
	        e.printStackTrace();
	    }

	    driver.findElement(By.cssSelector(".tree-hit")).click();
	    driver.findElement(By.cssSelector(".tree-collapsed")).click();
  }
}


添加部门,需要执行Ajax,最好根据Ajax的返回值判断是否添加成功,但现在还需要探索如何抓取Ajax的返回,现在还没有好的方法解决。

	    driver.findElement(By.xpath("//a[@id=\'btn\']/span/span")).click();
	    try {
	        Thread.sleep(3000);
	    } catch (InterruptedException e) {
	        e.printStackTrace();
	    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值