JAVA自动化之Junit单元测试框架详解

简介:

JAVA自动化之Junit单元测试框架详解

一、JUnit概述&配置
1、Junit是什么?

Junit是一个Java 编程语言的开源测试框架,用于编写和运行测试。官网 地址:https://junit.org/junit4/

2、Maven配置

?xml version="1.0" encoding="UTF-8"?>

     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>junit</groupId>
<artifactId>junitTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

二、Assertions 断言
JUnit提供了一些辅助的函数,用来判断被测试的方法是否如我们预期的效果一样正常执行。这些辅助函数被称之为断言。
常用断言:

方法 示例 功能
assertArrayEquals assertArrayEquals("message", expected, actual); 判断两个数组是否相等
assertEquals assertEquals("message", "text", "text"); 判断两个对象是否相等
assertFalse assertFalse("failure - should be false", false); 判断表达式是否为false
testAssertTrue assertTrue("failure - should be true", true); 判断表达式是否为true
assertNotNull assertNotNull("should not be null", new Object()); 判断是否非空
assertNull assertNull("should be null", null); 判断是否为空
assertNotSame assertNotSame("should not be same Object", new Object(), new Object()); 判断是否为不同对象
assertSame assertSame("should be same", aNumber, aNumber); 判断是否为同一对象
...... ...... ......

三、Test Runners 测试运行器
JUnit中所有的测试方法都是由测试运行器负责执行。当一个类被@RunWith注释或拓展了一个@RunWith注释的类,JUnit将会使用引用的类来执行测试,而不是使用JUnit内置的运行器。

org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);

Specialized Runners:
Suite:Suite是一个标准的运行器,允许手动构建包含来自许多类的测试集。
Parameterized:Parameterized是一个实现参数化测试的标准运行器。运行参数化测试类时,测试方法和测试数据进行合并来创建测试实例。
Categories:Categories运行器来制定分类,定义测试被包含或排除。

四、Aggregating tests in suites 套件
测试套件用于捆绑几个单元测试用例并且一起执行他们,使用@RunWith 和 @Suite 注解。

@RunWith(Suite.class)
@Suite.SuiteClasses({AssertTests.class, CalculatorTest.class})
public class SuiteTest {

// the class remains empty, used only as a holder for the above annotations

}

五、Test execution order 执行顺序
要改变测试执行的顺序只需要在测试类上使用 @FixMethodOrder注解,并指定一个可用的MethodSorter即可:
@FixMethodOrder(MethodSorters.DEFAULT):JUnit默认使用一个确定的,但不可预测的顺序
@FixMethodOrder(MethodSorters.JVM): 保留测试方法的执行顺序为JVM返回的顺序,每次测试的执行顺序有可能会所不同
@FixMethodOrder(MethodSorters.NAME_ASCENDING):根据测试方法的方法名排序,按照词典排序规则(ASC从小到大递增)

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ExecutionOrderTest {

@Test
public void testB() {
    System.out.println("second");
}
@Test
public void testA() {
    System.out.println("first");
}
@Test
public void testC() {
    System.out.println("third");
}

}

运行结果:

first
second
third

六、Expected Exceptions 异常测试
用于测试某一方法是否抛出了正确的异常。
1、@Test(expected=xxx)方式:当抛出的异常与expected参数指定的异常相同时,测试通过。
2、try...fail...catch...方式:捕捉具体待测语句的异常信息并断言,当没有异常被抛出的时候fail方法会被调用,输出测试失败的信息。
3、ExpectedException Rule方式:使用Rule标记来指定一个ExpectedException,并在测试相应操作之前指定期望的Exception类型。

public class ExpectedExceptionsTest {

//方法一:@Test(expected=xxx)
@Test(expected = IndexOutOfBoundsException.class)
public void empty() {
    new ArrayList<Object>().get(0);
}

//方法二:try...fail...catch...  当没有异常被抛出的时候fail方法会被调用,输出测试失败的信息。
@Test
public void testExceptionMessage() {
    try {
        new ArrayList<Object>().get(0);
        fail("Expected an IndexOutOfBoundsException to be thrown");
    } catch (IndexOutOfBoundsException anIndexOutOfBoundsException) {
        assertThat(anIndexOutOfBoundsException.getMessage(), is("Index: 0, Size: 0"));
    }
}

//方法三:在测试之前使用Rule标记来指定一个ExpectedException,并在测试相应操作之前指定期望的Exception类型(如IndexOutOfBoundException.class)
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {
    List<Object> list = new ArrayList<Object>();
    thrown.expect(IndexOutOfBoundsException.class);
    thrown.expectMessage("Index: 0, Size: 0");
    list.get(0);
}

}

七、Matchers and assertThat
JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活。并且使用全新的断言语法assertThat,结合Hamcrest提供的匹配符,只用这一个方法,就可以实现所有的测试。
assertThat语法:
assertThat(T actual, Matcher matcher);
assertThat(String reason, T actual, Matcher matcher);
其中reason为断言失败时的输出信息,actual为断言的值或对象,matcher为断言的匹配器,里面的逻辑决定了给定的actual对象满不满足断言。
Matchers详见:
http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html

八、Ignoring tests 忽略测试
方法用 @Ignore 注解了将不会被执行
类用 @Ignore 注解后,其下所有测试方法将不会被执行

public class IgnoreTest {

@Ignore("Test is ignored as a demonstration")
@Test
public void testSame() {
    assertThat(1, is(1));
}

}

九、Timeout for tests 超时测试
@Timeout 注解用来测试特定方法的执行时间。如果测试方法的执行时间大于指定的超时参数,测试方法将抛出异常,测试结果为失败。指定的超时参数单位为毫秒。
1、@Test注解上的timeout参数,作用域为方法,单位毫秒

@Test(timeout = 2000)

public void testSleepForTooLong() throws Exception {
    log += "ran1";
    TimeUnit.SECONDS.sleep(100); // sleep for 100 seconds
}

2、Timeout Rule,作用域为测试类

public class TimeoutTests {

public static String log;
private final CountDownLatch latch = new CountDownLatch(1);

@Rule
public Timeout globalTimeout = Timeout.seconds(3); // 3 seconds max per method tested

@Test
public void testSleepForTooLong() throws Exception {
    log += "ran1";
    TimeUnit.SECONDS.sleep(100); // sleep for 100 seconds
}

@Test
public void testBlockForever() throws Exception {
    log += "ran2";
    latch.await(); // will block
}

}

十、Parameterized tests 参数化测试
参数化测试允许开发人员使用不同的值反复运行同一个测试。创建参数化测试步骤:

用 @RunWith(Parameterized.class) 来注释 test 类。
创建一个由 @Parameters 注释的公共的静态方法,它返回一个对象的集合(数组)来作为测试数据集合。
创建一个公共的构造函数,接受测试数据。
为每一列测试数据创建一个实例变量。
用实例变量作为测试数据的来源来创建测试用例。
1、Constructor方式

@RunWith(Parameterized.class)
public class FibonacciTest {

@Parameters(name = "{index}: fib({0})={1}")
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
            { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
    });
}
private int fInput;
private int fExpected;
public FibonacciTest(int input, int expected) {
    this.fInput = input;
    this.fExpected = expected;
}
@Test
public void test() {
    assertEquals(fExpected, Fibonacci.compute(fInput));
}

}

2、Field injection方式

@RunWith(Parameterized.class)
public class FibonacciTest {

@Parameters(name = "{index}: fib({0})={1}")
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
            { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
    });
}
@Parameter // first data value (0) is default
public /* NOT private */ int fInput;
@Parameter(1)
public /* NOT private */ int fExpected;
@Test
public void test() {
    assertEquals(fExpected, Fibonacci.compute(fInput));
}

}

十一、Assumptions with assume 假定测试
使用Assumptions类中的假设方法时,当假设不成立时会报错,但是测试会显示被ignore忽略执行。也就是当一个类中有多个测试方法时,其中一个假设测试方法假设失败,其他的测试方法全部成功,那么该测试类也会显示测试成功。
假设方法适用于:在不影响测试是否成功的结果的情况下根据不同情况执行相关代码。

public class AssumptionsTest {

@Test
public void testAssumTrue() {
    System.out.println("test");
    assumeTrue(3>5);
    //该方法中下面所有的代码在上面假设的条件成立后执行
    //如果上述假设不成立,则会忽略执行该行下面的代码,并报错
    System.out.println("assume is true!");
}
@Test
public void testAssumFalse(){
    assumeFalse(3>5);
    System.out.println("assume is true!");
}

}

以下语法JUnit5支持:

@Test

public void testAssumTrueMessage() {
    assumeTrue(3<5,
            //第二个参数为当第一个参数不成立时,输出的自定义错误信息
            () -> "Aborting test: not on developer workstation");
    System.out.println("assume is true!");
}
@Test
public void testAssumeTrueLambda(){
    //这个方法的第一个参数为函数式接口,无参数返回值为boolean
    assumeTrue(()->{
        System.out.println("in assumeTrue");
        boolean flag = false;
        return flag;
    });
    System.out.println("out assumeTrue");
}
@Test
public void testAssumThat() {
    assumingThat(3>5,
            () -> {
                //与上述方法不同的是,仅当前面假设成立时,才会执行这里面的语句
                //且只会影响到该lambda表达式中的代码
                assertEquals(2, 2);
            });
    //此处的断言不受上述assumingThat限制,在所有情况下都会执行
    System.out.println("no effect");
    assertEquals("a string", "a string");
}

十二、Rules 规则
一个JUnit Rule就是一个实现了TestRule的类,用来在每个测试方法的执行前后执行一些代码。
1、框架自带的Rule
JUnit自带很多已经实现过好了的JUnit Rule,比如Timeout,ExpectedException等等。
2、自定义Rule
自定义一个Rule就是implement一个TestRule interface,实现一个叫apply()的方法。
例:在测试方法运行之前,记录测试方法所在的类名和方法名,然后在测试方法运行之后打印出来。

public class MethodNameExample implements TestRule {

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {
        @Override
        public void evaluate() throws Throwable {
            //base.evaluate()之前为测试方法运行之前所做操作
            String className = description.getClassName();
            String methodName = description.getMethodName();
            //运行测试方法
            base.evaluate();
            //base.evaluate()之后为测试方法运行之后所做操作
            System.out.println("Class name: "+className +", method name: "+methodName);
        }
    };
}

}

public class RuleTest2 {

@Rule
public MethodNameExample methodNameExample = new MethodNameExample();
@Test
public void addition_isCorrect() throws Exception {
    assertEquals(4, 2 + 2);
}
@Test
public void mulitiplication_isCorrect() throws Exception {
    assertEquals(4, 2 * 2);
}

}

十三、Theories
在参数化测试中,我们需要给定所有具体的测试数据组。而在Theories测试中,用户只需给定了一些数据,JUnit自动利用这些数据组合出各种各种可能的组合来执行测试。

1、内置实现

(1)@DataPoints注解静态变量方式

@RunWith(Theories.class)
public class TheoryTest {

//允许的最大误差
private static final double DELTA = 0.01;
/*@DataPoints注解静态变量*/
@DataPoint
public static int ZERO = 0;
@DataPoint
public static int TWO = 2;
@DataPoint
public static int EIGHT = 8;
//标志这个测试为Theory测试
@Theory
public void testDivide(int dividend, int divisor) {
    //跳过除数为0的case
    assumeThat(divisor, not(0));
    //Calculator.divide(dividend, divisor)方法返回他们相除的结果
    assertEquals(dividend / divisor, Calculator.divide(dividend, divisor), DELTA);
    System.out.println("Passed with: dividend=" + dividend + ", divisor=" + divisor);
}

}

(2)@DataPoints注解静态方法方式

@RunWith(Theories.class)
public class TheoryTest {

//允许的最大误差
private static final double DELTA = 0.01;
/*@DataPoints注解一个静态方法*/
@DataPoints
public static int[] getTestData() {
    return new int[]{0, 2, 8};
}
//标志这个测试为Theory测试
@Theory
public void testDivide(int dividend, int divisor) {
    //跳过除数为0的case
    assumeThat(divisor, not(0));
    //Calculator.divide(dividend, divisor)方法返回他们相除的结果
    assertEquals(dividend / divisor, Calculator.divide(dividend, divisor), DELTA);
    System.out.println("Passed with: dividend=" + dividend + ", divisor=" + divisor);
}

}

@DataPoint用于注解静态变量(或静态方法),表示这个变量是个数据点。当执行testDivide这个Theory测试时,JUnit会把所有的DataPoint数据两两组合,形成一组组的测试数据,并用这些数据分别执行测试。执行上面的测试会输出以下结果:

Passed with: dividend=0, divisor=2
Passed with: dividend=0, divisor=8
Passed with: dividend=2, divisor=2
Passed with: dividend=2, divisor=8
Passed with: dividend=8, divisor=2
Passed with: dividend=8, divisor=8

(3)如果需要限定某个参数,可以使用@TestOn注解

import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.experimental.theories.suppliers.TestedOn;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
@RunWith(Theories.class)
public class TheoryTest {

//允许的最大误差
private static final double DELTA = 0.01;
//如果需要限定某个参数,可以使用@TestOn注解
@Theory
public void testDivide2(
        @TestedOn(ints = {0, 2, 8}) int dividend,
        @TestedOn(ints = {2, 8}) int divisor
) {
    assertEquals(dividend / divisor, Calculator.divide(dividend, divisor), DELTA);
    System.out.println("Passed with: dividend=" + dividend + ", divisor=" + divisor);
}

}

2、自定义实现

JUnit默认只提供了一个int型的简单 Parameter Supplier 实现,而Theory机制真正的价值在于,能参考@TestedOn的做法,相对简单的完全自定义出可重用 Parameter Supplier,适应于各种复杂要求的限定范围参数值测试场景,满足开发者所需的高度动态自定义范围取值自动化测试,同时保留与一般@Test相同的强大兼容性。
例:
(1)定义annotation注解接口Between

@Retention(RetentionPolicy.RUNTIME)
// 声明注解接口所使用的委托处理类
@ParametersSuppliedBy(BetweenSupplier.class)
public @interface Between{

// 声明所有可用参数,效果为 @Between([first = int,] last = int)
int first() default 0;  // 声明默认值
int last();

}

(2)定义委托处理类 BetweenSupplier

public class BetweenSupplier extends ParameterSupplier {

@Override
public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
    // 自定义实参值列表
    List<PotentialAssignment> list = new ArrayList<PotentialAssignment>();
    // 获取注解变量
    Between between = sig.getAnnotation(Between.class);
    // 获取通过注解@Between传入的first值
    int first = between.first();
    // 获取通过注解@Between传入的last值
    int last = between.last();
    for (int i = first; i <= last; i++) {
        // PotentialAssignment.forValue(String name, Object value)
        // name为value的描述标记,没实际作用
        // value为实参可选值
        list.add(PotentialAssignment.forValue("name", i));
    }
    return list;
}

}

(3)调用方式

@RunWith(Theories.class)
public class TheoryDefinedTest {

@Theory
public final void test(@Between(last = 0) int i, @Between(first = 3, last= 10) int j) {
    // i 取值为 0(first默认=0,last=0),j 取值为 3-10
    System.out.println("i="+i+"  j="+j);
}

}

(4)运行结果

i=0 j=3
i=0 j=4
i=0 j=5
i=0 j=6
i=0 j=7
i=0 j=8
i=0 j=9
i=0 j=10

十四、Test fixtures
Test Fixture是指一个测试运行所需的固定环境,也是就是测试运行之前所需的稳定的、公共的可重复的运行环境,这个“环境”不仅可以是数据,也可以指对被测软件的准备,例如实例化被测方法所依赖的类、加载数据库等等。

@Before - 在每个@Test方法之前运行
@After - 在每个@Test方法之后运行
@BeforeClass - 在所有的@Test方法之前运行一次
@AfterClass - 在所有的@Test方之后运行一次
注:
1、如果创建一个子类继承有fixture注解的父类,那么子类中的@Before方法会在测试方法之前、父类的@Before执行之后执行。
2、如果@Before方法里抛出了异常,@Test方法会跳过,但是@After还是会执行
3、每个测试方法都会在单独的测试类的实例里面运行,@BeforeClass在测试实例创建之前执行

public class FixtureTest {

private static int quantity = 0;
public FixtureTest() {
    quantity++;
}
@BeforeClass
public static void breforeTestOnlyOnce() throws Exception {
    System.out.println("Run before all test only once..."+ quantity);
}
@AfterClass
public static void afterTestOnlyOnce() throws Exception {
    System.out.println("Run after all test only once..."+ quantity);
}
@Before
public void beforePerTest() {
    System.out.println("Run before per test ..."+ quantity);
}
@After
public void afterPerTest() {
    System.out.println("Run after per test ..."+ quantity);
}
//Test Method
@Test
public void testOne() {
    System.out.println("testOne Start..."+ quantity);
}
@Test
public void testTwo() {
    System.out.println("testTwo Start..."+ quantity);
}

}

运行结果:

Run before all test only once...0
Run before per test ...1
testOne Start...1
Run after per test ...1
Run before per test ...2
testTwo Start...2
Run after per test ...2
Run after all test only once...2

十五、Categories 用例分类
category 和 testSuite的比较:testSuite是类级分组(xx.class),category是用例级分组(@Test),category是testSuite的升级
category使用步骤:
1、创建好测试类,及测试类中的测试用例
2、创建接口:按用例的分组来创建
3、@Category注解:将用例用@Category注解分组
4、创建类来执行这些分组的类

public interface FastTests { / category marker / }
public interface SlowTests { / category marker / }

public class A {
@Test
public void a() {

fail();

}
@Category(SlowTests.class)
@Test
public void b() {
}
}

@Category({SlowTests.class, FastTests.class})
public class B {
@Test
public void c() {
}
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b, but not A.a or B.c
}

十六、总结
如果你觉得此文对你有帮助,如果你对软件测试、接口测试、自动化测试、面试经验交流感兴趣欢迎加入软件测试技术群:695458161,群里发放的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

独行踽近,众行致远!欢迎加入《软件测试技术交流群:695458161》各种软件测试资料,WEB自动化,接口自动化,APP自动化,jenkins持续集成下的不要太爽! 还可以定期免费领取测试进阶资料哦!
作者:诸葛
原文地址https://www.cnblogs.com/csmashang/p/12696717.html

相关文章
|
11天前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
16 0
|
11天前
|
XML 前端开发 Java
《手把手教你》系列技巧篇(十四)-java+ selenium自动化测试-元素定位大法之By xpath上卷(详细教程)
【4月更文挑战第6天】按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath。xpath 的定位方法, 非常强大。使用这种方法几乎可以定位到页面上的任意元素。xpath 是XML Path的简称, 由于HTML文档本身就是一个标准的XML页面,所以我们可以使用Xpath 的用法来定位页面元素。XPath 是XML 和Path的缩写,主要用于xml文档中选择文档中节点。基于XML树状文档结构,XPath语言可以用在整棵树中寻找指定的节点。
42 0
|
3天前
|
Java 测试技术 定位技术
《手把手教你》系列技巧篇(二十三)-java+ selenium自动化测试-webdriver处理浏览器多窗口切换下卷(详细教程)
【4月更文挑战第15天】本文介绍了如何使用Selenium进行浏览器窗口切换以操作不同页面元素。首先,获取浏览器窗口句柄有两种方法:获取所有窗口句柄的集合和获取当前窗口句柄。然后,通过`switchTo().window()`方法切换到目标窗口句柄。在项目实战部分,给出了一个示例,展示了在百度首页、新闻页面和地图页面之间切换并输入文字的操作。最后,文章还探讨了在某些情况下可能出现的问题,并提供了一个简单的本地HTML页面示例来演示窗口切换的正确操作。
24 0
|
3天前
|
敏捷开发 监控 前端开发
深入理解自动化测试框架Selenium的架构与实践
【4月更文挑战第16天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加快迭代速度的关键手段。Selenium作为一种广泛使用的自动化测试工具,其开源、跨平台的特性使得它成为业界的首选之一。本文旨在剖析Selenium的核心架构,并结合实际案例探讨其在复杂Web应用测试中的高效实践方法。通过详细解读Selenium组件间的交互机制以及如何优化测试脚本,我们希望为读者提供深入理解Selenium并有效运用于日常测试工作的参考。
11 1
|
4天前
|
自然语言处理 测试技术 API
深入理解自动化测试框架Selenium的设计理念与实践
【4月更文挑战第15天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加速迭代的关键手段。Selenium作为一种广泛使用的自动化测试框架,提供了对多种浏览器和平台的支持,极大地促进了Web应用的功能测试。本文旨在剖析Selenium的核心设计理念,探讨其在实际项目中的应用,并指出常见的误区及最佳实践,以期帮助测试工程师更高效地利用Selenium进行测试工作。
|
6天前
|
监控 测试技术 API
深入理解自动化测试框架Selenium的设计与实现
【4月更文挑战第14天】在软件开发过程中,自动化测试是确保代码质量、减少人工重复劳动的关键步骤。Selenium作为一款广泛使用的自动化测试工具,提供了对多种浏览器和操作系统的支持。本文将探讨Selenium的核心组件及其架构设计,分析其如何通过WebDriver与浏览器交互,以及它如何支持多种编程语言进行脚本编写。同时,我们还将讨论Selenium Grid的作用以及它如何实现并行测试,以缩短测试周期并提高测试效率。
170 59
|
6天前
|
前端开发 JavaScript Java
《手把手教你》系列技巧篇(十九)-java+ selenium自动化测试-元素定位大法之By css下卷(详细教程)
【4月更文挑战第11天】按计划今天宏哥继续讲解css的定位元素的方法。但是今天最后一种宏哥介绍给大家,了解就可以了,因为实际中很少用。
27 2
|
7天前
|
Web App开发 前端开发 Java
框架分析(11)-测试框架
框架分析(11)-测试框架
|
7天前
|
前端开发 JavaScript Java
《手把手教你》系列技巧篇(十八)-java+ selenium自动化测试-元素定位大法之By css中卷(详细教程)
【4月更文挑战第10天】本文主要介绍了CSS定位元素的几种方法,包括ID属性值定位、其他属性值定位和使用属性值的一部分定位。作者提供了示例代码,展示了如何使用这些方法在Java+Selenium自动化测试中定位网页元素。通过CSS选择器,可以更精确地找到页面上的特定元素,如输入框、按钮等,并进行相应的操作,如输入文本、点击等。文章还提供了实际运行代码后的控制台输出和浏览器动作的示例。
36 0
|
8天前
|
前端开发 JavaScript Java
《手把手教你》系列技巧篇(十七)-java+ selenium自动化测试-元素定位大法之By css上卷(详细教程)
【4月更文挑战第9天】本文介绍了CSS定位方式的使用,包括它的优势和8种常用的定位方法。CSS定位相比XPath定位更快、更稳定。文章通过示例详细讲解了如何使用CSS定位元素,包括通过id、name、class name、tag name、link text、partial link text以及XPath进行定位。还提供了Java代码示例来演示如何在自动化测试中使用这些定位方法。
38 1