Java+PageObject+Maven+TestNg 开源分享

说明

  • selenium3+java 自动化测试
  • 查看源码

功能

  • maven 管理
  • testng 配置测试类
  • yaml维护用例
  • PageObject 模式

用法

初始化mavnen

.....
            
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.2
                
                    true
                    
                        res/testNG.xml
                    
                    
                
            
        
    

    
        
            org.testng
            testng
            6.11
        
        
            org.seleniumhq.selenium
            selenium-java
            3.4.0
        
        
            log4j
            log4j
            1.2.17
        
        
            org.seleniumhq.selenium
            selenium-chrome-driver
            3.4.0
        
        
            com.esotericsoftware.yamlbeans
            yamlbeans
            1.11
        
    


配置PageObject

以登录为例子,所有的page都由三部分构成:

  • 构造函数
  • 操作方法
  • 检查点
public class LoginPage {
    YamlRead yamlRead;
    OperateElement operateElement;
    protected WebDriver driver;

    /***
     * 默认构造函数
     * @param driver
     * @param path yaml配置参数
     */
    public LoginPage(WebDriver driver, String path) {
        this.driver = driver;
        yamlRead = new YamlRead(path);
        operateElement= new OperateElement(this.driver);
    }

    /***
     * 测试步骤
     * @throws YamlException
     * @throws FileNotFoundException
     */
    public void operate() throws YamlException, FileNotFoundException {
        List list = (List) yamlRead.getYmal().get("testcase");
//        System.out.println(list);
        for(Object item: list){
            TestCase testCase = new TestCase();
            testCase.setFind_type((String) ((Map)item).get("find_type"));
            testCase.setElement_info((String) ((Map)item).get("element_info"));
            testCase.setText((String) ((Map)item).get("text"));
            testCase.setOperate_type((String) ((Map)item).get("operate_type"));
            operateElement.operate(testCase);
        }
    }

    /***
     * 检查点
     * @return
     * @throws YamlException
     * @throws FileNotFoundException
     */
    public boolean checkpoint() throws YamlException, FileNotFoundException {
        List list = (List) yamlRead.getYmal().get("check");
//        System.out.println(list);
        for(Object item: list){
            CheckPoint checkPoint = new CheckPoint();
            checkPoint.setElement_info((String) ((Map)item).get("element_info"));
            checkPoint.setFind_type((String) ((Map)item).get("find_type"));
            if (!operateElement.checkElement(checkPoint)) {
                return false;
            }
        }
        return true;
    }

配置登录的yaml

testcase:
    - element_info: user_login
      find_type: id
      operate_type: send_keys
      text: lose
    - element_info: user[password]
      find_type: name
      operate_type: send_keys
      text: password
    - element_info: //*[@id="new_user"]/div[4]/input
      find_type: xpath
      operate_type: click
check:
    - element_info: /html/body/div[1]/nav/div/ul[1]/li/a/img
      find_type: xpath
    - element_info: /html/body/div[1]/nav/div/ul[2]/li[2]/a
      find_type: xpath

登录测试

public class LoginPageTest extends TestBaseSetup {
    private WebDriver driver;

    @BeforeClass
    public void setUp() {
        driver=getDriver();
    }
    @Test
    public void testLogin() throws YamlException, FileNotFoundException {
        LoginPage loginPage = new LoginPage(this.driver, "/Login.yaml");
        loginPage.operate();
        Assert.assertTrue(loginPage.checkpoint(), "检查点不通过");
    }
}

配置testng


    
    
    
    
        
            
        
    
    
        
            
        
    


测试结果

Paste_Image.png

后续版本

  • testng测试报告改造
  • 多设备
  • docker gird

你可能感兴趣的:(Java+PageObject+Maven+TestNg 开源分享)