Selenium入门之结合Junit进行自动化测试

能把Selenium RC脚本和JUnit单元测试结合起来,既能涵盖功能测试,又能涵盖数据或后台Java类测试,从而构成一个完整的Web应用测试解决方案。java

1. 方法或者执行步骤web

新建一个case.java文件,这里面能够写咱们须要被测试的方法或者业务逻辑,就像是一条条用例,包含执行步骤执行结果chrome

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);

}

步骤文件中的执行代码:ui

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);断言验证执行结果不是指望结果。code

 

attachments-2016-07-gwXp0FWo577cfb4f7d57

如图上述junit代码的执行结果,只有@test注释下面的方法验证结果在验证结果中。blog

上述代码testoppo_com01()和testoppo_com02()验证结果是失败的。get

attachments-2016-07-yWVZykGu577cfb589c7f

这个是Console运行控制台,这里咱们能够看到代码各阶段的运行状态。

Junit经常使用的注解:

@Before 注解:在每一个测试方法以前执行;

@After 注解:在每一个测试方法以后执行;

@BeforeClass 注解:在全部方法执行以前执行;

@AfterClass 注解:在全部方法执行以后执行;

@Test(timeout = xxx) 注解:设置当前测试方法在必定时间内运行完,不然返回错误;

@Test(expected = Exception.class) 注解:设置被测试的方法是否有异常抛出。抛出异常类型为:

Exception.class;

@Ignore 注解:注释掉一个测试方法或一个类,被注释的方法或类,不会被执行。

相关文章
相关标签/搜索