能把Selenium RC脚本和JUnit单元测试结合起来,既能涵盖功能测试,又能涵盖数据或后台Java类测试,从而构成一个完整的Web应用测试解决方案。
1. 方法或者执行步骤
新建一个case.java文件,这里面可以写我们需要被测试的方法或者业务逻辑,就像是一条条用例,包含执行步骤执行结果
package com.case;
public class Java_demo {
public static void main()
{
System.out.print("aa");
}
public class Test_clik
{
//public Test_clik(){}
public boolean oppo_com01()
{
boolean Result=false;
System.out.println("我在运行......A");
return Result;
}
public int oppo_com02()
{
int Result=0;
System.out.println("我在运行......B");
return Result;
}
public String oppo_com03()
{
String Result="no";
System.out.println("我在运行......C");
return Result;
}
}
}
2. 测试验证步骤
新建一个testcase.java的文件,引用junit的jar包和junit的注释,在所有测试方法之前我们要执行的步骤写在@BeforeClass注释方法内,在每个方法之前要执行的步骤写在@Before注释方法内,在所有测试方法之后我们要执行的步骤写在@AfterClass注释方法内,在每个方法之后要执行的步骤写在@After注释方法内,不需要这样的操作步骤我们可以不写,可以直接写在@test注释的测试方法内
package com.testcase;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class JunitDemo
{
private static WebDriver driver;
private Java_demo java_demo=new Java_demo();
private Java_demo.Test_clik tc;
//在所有方法执行之前执行:打开浏览器驱动
@BeforeClass
public static void init_driver()
{
System.out.println("打开Chrome浏览器");
System.setProperty("webdriver.chrome.driver","C:\\ProgramFiles(x86)\\Goo gle\\Chrome\\Application\\chromedriver.exe");
driver=new ChromeDriver();
}
//在所有方法执行之后执行:关闭浏览器驱动
@AfterClass
public static void exit_driver()
{
System.out.println("关闭Chrome浏览器");
System.out.println("--------------------");
driver.quit();
}
//在每个方法执行之前执行:去到www.oppo.com页面
@Before
public void init_www()
{
System.out.println("--------------------");
System.out.println("去到www.oppo.com");
driver.get("http://www.oppo.com/cn/");
tc = java_demo.new Test_clik();
}
//在每个方法执行之后执行:打印----------
@After
public void print_()
{
System.out.println("--------------------");
}
@Test
public void testoppo_com01()
{
boolean a=tc.oppo_com01();
assertEquals(true,a);
}
@Test
public void testoppo_com02()
{
int a=tc.oppo_com02();
//int a=1;
assertEquals(1,a);
}
@Test
public void testoppo_com03()
{
String a=tc.oppo_com03();
//int a=1;
assertEquals("no",a);
}
}
junit代码中的验证代码:
@Test
public void testoppo_com01()
{
boolean a=tc.oppo_com01();
assertEquals(true,a);
}
步骤文件中的执行代码:
public boolean oppo_com01()
{
boolean Result=false;
System.out.println("我在运行......A");
return Result;
}
对比执行方法oppo_com01()与验证方法testoppo_com01(),当执行方法中的返回值为false时,assertEquals(true,a);断言验证执行结果不是期望结果。当执行方法中的返回值为true时,assertEquals(true,a);断言验证执行结果不是期望结果。
如图上述junit代码的执行结果,只有@test注释下面的方法验证结果在验证结果中。
上述代码testoppo_com01()和testoppo_com02()验证结果是失败的。
这个是Console运行控制台,这里我们可以看到代码各阶段的运行状态。
Junit常用的注解:
@Before 注解:在每个测试方法之前执行;
@After 注解:在每个测试方法之后执行;
@BeforeClass 注解:在所有方法执行之前执行;
@AfterClass 注解:在所有方法执行之后执行;
@Test(timeout = xxx) 注解:设置当前测试方法在一定时间内运行完,否则返回错误;
@Test(expected = Exception.class) 注解:设置被测试的方法是否有异常抛出。抛出异常类型为:
Exception.class;
@Ignore 注解:注释掉一个测试方法或一个类,被注释的方法或类,不会被执行。