配置maven依赖

以下pom.xml文件是一个selenium的demo项目,使用PO模型编码,分层解耦。将底层方法、元素操作、业务逻辑、测试数据分离。技术选型:java + selenium + testng + allure + maven + git + jenkins,源码可见https://github.com/hjpcs/seleniumdemo

    <properties>
        <aspectj.version>1.8.10</aspectj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
        <dependency>
            <groupId>io.qameta.allure</groupId>
            <artifactId>allure-testng</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <!-- testng配置文件 -->
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                    <!-- 将命名不规范的测试用例.java文件包含 -->
                    <includes>
                        <include>**/**.java</include>
                    </includes>
                    <argLine>
                        -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
                    </argLine>
                    <!-- 设置allure-results文件夹生成在target目录下 -->
                    <systemProperties>
                        <property>
                            <name>allure.results.directory</name>
                            <value>${project.build.directory}/allure-results</value>
                        </property>
                        <property>
                            <name>allure.link.issue.pattern</name>
                            <value>https://example.org/issue/{}</value>
                        </property>
                    </systemProperties>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjweaver</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Allure注解使用

@Step

    //输入关键字
    @Step("输入关键字")
    public void input(String text){
        util.inputById(searchBar, text);
    }

    //搜索
    @Step("点击搜索按钮")
    public void search(){
        util.clickById(searchButton);
    }

效果如图

file

@Epic/@Feature/@Story/@Test/@Description/@Severity

@Epic("百度搜索")
@Feature("输入关键词进行搜索")
public class BaiduSearch extends BaseTest {

    /*
    因为百度搜索后的结果页面搜索框和搜索按钮id都没变,所以这里用@BeforeClass和@AfterClass即可
    三次测试只需要启动/关闭一次浏览器
     */
    @BeforeClass
    public void before(){
        super.initBaidu();
    }

    @AfterClass(alwaysRun = true)
    public void after(){
        util.quit();
    }

    @Test(dataProvider = "searchData", dataProviderClass = BaiduSearchData.class, priority = 0, description = "百度搜索测试")
    @Description("使用dataProvider进行数据驱动")
    @Severity(SeverityLevel.BLOCKER)
    @Story("获取页面title进行断言")
    public void baiduSearchTest(String keyword, String expect){
        baiduPage.input(keyword);
        baiduPage.search();
        try {
            util.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Assert.assertEquals(util.getTitle(), expect);
    }
}

效果如图

file

如何生成报告

1.使用maven命令执行测试 mvn clean test

2.生成在线报告 allure serve allure-results

3.生成实体报告 allure generate allure-results

分类: 测试

发表评论

电子邮件地址不会被公开。