软件测试

testNG优雅的使用注解让你的测试项目开发更高效!

2020-11-02  本文已影响0人  Coding测试

testNG大部分是通过xml配置测试类和监听类

现在大部分后端项目是springboot框架,下面就以springboot框架结合testNG实现注解注入bean实例介绍:

@Slf4j
@Component
@SpringBootTest
public class BaseTest  extends AbstractTestNGSpringContextTests{
    /**
     * 驱动基类
     */
    @Autowired
    private BaseChromeDriver baseDriver;
    /**
     * 驱动
     * 对外暴露
     */
    public WebDriver driver;
}
@Listeners(com.iwebui.listener.AssertListener.class)
public class BaiduLoginCase extends BaseTest {
    @Autowired
    private BaiDuCaseEasypoiHandle baiDuCaseEasypoiHandle;
    @Autowired
    BaiDuCaseJpaHandle baiDuCaseJpaHandle;
    /**
     * easypoi实现excel数据驱动
     */
    @Severity( SeverityLevel.NORMAL)
    @Description("百度登录测试示例--使用easypoi实现")
    @Test
    public void baibaiDuCaseEasypoiHandle(){
        //初始化ticketElement,获取驱动
        baiDuCaseEasypoiHandle.serchBaidu(driver);
        //excel数据驱动测试-使用easypoi实现
        baiDuCaseEasypoiHandle.loginCase(driver);
    }
    /**
     * JPA实现数据库数据驱动示例
     */
    @Severity( SeverityLevel.NORMAL)
    @Description("百度登录测试示例--使用springDataJPA实现")
    @Test
    public void baiDuCaseJpaHandle(){
        //进入被测网址
        baiDuCaseJpaHandle.serchBaidu(driver);
        //数据库驱动,使用jpa实现
        baiDuCaseJpaHandle.getAll(driver);
    }
}
@Component
public class BaiDuCaseJpaHandle extends BaseBrowser {
    @Autowired
    private BaiDuLoginDao baiDuLoginDao;
    @Autowired
    private BaiDuUrlDao baiDuUrlDao;

    public void serchBaidu(WebDriver driver){
        log.info("开始进入被测页面");
        enterPage(driver,AccountData.BAIDUURL);
    }

    public List<Logincase> getAll(WebDriver driver) {
        UIElementUtil.clickButton("百度登录","点击右上角登录按钮",driver);
        UIElementUtil.clickButton("百度登录","点击账号密码登录按钮",driver);
        List<Logincase> logincaseList = baiDuLoginDao.findAll();
        List<UrlMessage> urlMessageList = baiDuUrlDao.findAll();
        //2.使用自定义写的sql方法操作数据库
        Map<Long, UrlMessage> map = new HashMap<>();
        urlMessageList.forEach(urlMessage -> {
            map.put(urlMessage.getId(), urlMessage);
        });
        logincaseList.forEach(logincase -> {
            String address = "";
            String actual = "";
            if (logincase.getCaseStatus().equals(1)) {
                UrlMessage urlMessage = map.get(logincase.getUrlId());
                if (urlMessage != null) {
                    address = urlMessage.getAddress();
                }
                if (!address.equals("")) {
                    baiDuLoginDao.updateLogincase(address, logincase.getId());
                }
                UIElementUtil.sendInput("百度登录","登录账号",driver,logincase.getName());
                UIElementUtil.sendInput("百度登录","登录密码",driver,logincase.getPwd());
                UIElementUtil.clickButton("百度登录","登录按钮",driver);
                actual = driver.findElement(AccountData.TIPS).getText();
                baiDuLoginDao.updateActual(actual,logincase.getId());
            }else {
                UrlMessage urlMessage = map.get(logincase.getUrlId());
                address = urlMessage.getAddress();
                baiDuLoginDao.updateLogincase(address, logincase.getId());
            }
        });
        return null;
    }
}
@Repository
public interface BaiDuUrlDao extends JpaRepository<UrlMessage, Long> {
}

另外大多数testNG配置文件也是通过xml、config.properties来完成配置文件的加载,这种配置流程繁琐。而yaml文件的配置更简单上手,这里通过yaml配置文件完成配置参数的加载。testNG通过yaml文件配置如下:

public class LoadStaticConfigUtil {
    public static Object getCommonYml(Object key){
        Resource resource = new ClassPathResource("/application-dev.yml");
        Properties properties = null;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties =  yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }
}

这样就完成了yaml获取配置信息的过程,更多技术分享可以关注[Coding测试]公众号。

点击获取该项目示例源码地址

上一篇下一篇

猜你喜欢

热点阅读