java+testng+selenium的自动化测试实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java+testng+selenium的自动化测试实例

潇洒却风流   2022-11-16 我要评论

前言

这是用testng框架加selenium做的一个UI自动化测试的项目

Java代码

package com.justin;

/**
 * @author justin-zhu
 * <p>
 * 2022年02月23日 16:48
 */

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;

public class HelloTestNG {

    private WebDriver driver;
    @BeforeMethod
    public void setBefore(){
        
        System.setProperty("webdriver.chrome.driver","C:\\Users\\betalpha-qa\\code\\testcode\\TestNG-Demo\\src\\main\\resources\\chromedriver.exe");
        //打开浏览器,使其最大化,并隐性等待两秒钟
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
    }

    @AfterMethod
    public void setAfter(){
        //结束驱动程序进程,关闭浏览器
        driver.quit();
    }

    @Test(groups = {"login"})
    public void login() throws InterruptedException {

        //输入网址(输入本地项目的URL,下面为本地项目的登陆界面)
        driver.get("http://192.168.0.188/webapp/session/login");
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        //使用其方法获取浏览器类型,并断言(如果断言失败,不会执行下面代码)
        String browserType = driver.getTitle();
        Assert.assertEquals("Google", browserType);
        //获取账号框定位
        WebElement userName = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/form/div/div[2]/div/div[1]/div/input"));
        //获取密码框定位
        WebElement password = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/form/div/div[3]/div/div[1]/div/div/span/input"));
        //获取验证码框定位
        WebElement authCode = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/form/div/div[4]/div/div/div[1]/div/input"));
        WebElement loginButton = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div/form/div/div[5]/div/div/div/button"));
        //输入账号密码登录,并点击登录
        userName.sendKeys("jusitn@qq.com");
        password.sendKeys("123456");
        authCode.sendKeys("1234");
        driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
        loginButton.click();
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        //获取登录界面的title,验证登录成功
        WebElement title = driver.findElement(By.xpath("//*[@id=\"logo\"]/div/div/div[1]/h1"));
        String actual = title.getText();
        Assert.assertEquals(actual, "指数研发与管理平台");
    }

   @Test(description = "定位百度一下")
    public void testBaiDu(){
        //输入网址
        driver.get("https://www.baidu.com/");
        driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
        //定位到百度一下按钮
        WebElement name = driver.findElement(By.id("su"));
        String text = name.getAttribute("value");
        Assert.assertEquals(text, "百度一下");
    }
    
    @Test(groups = {"fast"})
    public void aFastTest(){
        System.out.println("Fast test");
    }

    @Test(groups = {"slow"})
    public void aSlowTest(){
        System.out.println("Slow test");
    }
}

配置文件

要想上面的test能跑起来,还需要再pon.xml文件里面添加以下依赖

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>

这是testng框架的依赖,有了这个依赖testng的注释才会生效

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.14.0</version>
        </dependency>

如果你只在本地运行代码,那么有selenium-java就够了;但是如果要在远程调用,就需要配置该selenium-server依赖

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>2.42.2</version>
        </dependency>

这是想要再界面上操作元素配置的依赖

扩展

testng用例可以直接运行java代码,也可以配置testng.xml文件进行用例的执行

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "testProj">
    <test name = "testDemo1">

<!--        .XML中指定组内的某些方法,include为执行,exclude为不执行-->
        <groups>
            <run>
                <exclude name="fast"/>
                <exclude name="slow"/>
                <include name="login"/>
            </run>
        </groups>

<!--        .XML指明测试类,按照类名执行-->
        <classes>
            <class name="com.justin.HelloTestNG"/>
        </classes>

<!--        .XML指定包名,执行包内的所有测试类-->
<!--        <packages>-->
<!--            <package name="com.justin"></package>-->
<!--        </packages>-->
    </test>
    
    <listeners>
        <listener class-name="org.uncommons.reportng.HTMLReporter"></listener>
        <listener class-name="org.uncommons.reportng.JUnitXMLReporter"></listener>
    </listeners>
</suite>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们