jenkins maven testng selenium自动化持续集成

准备环境

首先我们新建一个maven的工程,并且在pom.xml中配置好我们依赖的一些jar包

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.46.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.6</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>2.46.0</version>
        </dependency>


    </dependencies>

编写我们selenium脚本

public class NewTest {

    private WebDriver driver;



    @BeforeTest
    public void beforeTest(){
        driver = new FirefoxDriver();
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.baidu.com");
    }

    @AfterTest
    public void afterTest(){
        driver.quit();
    }

    @Test
    public void f()
    {
        System.out.println("heloo");
        By inputBox = By.id("kw");
        By searchButton = By.id("su");

        //智能等待元素加载出来
        intelligentWait(driver, 10, inputBox);
        //智能等待元素加载出来
        intelligentWait(driver, 10, searchButton);
        driver.findElement(inputBox).sendKeys("中国");
        driver.findElement(searchButton).click();

    }


    /**这是智能等待元素加载的方法*/
    public void intelligentWait(WebDriver driver,int timeOut, final By by) {
        try {
            (new WebDriverWait(driver, timeOut)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    WebElement element = driver.findElement(by);
                    return element.isDisplayed();
                }
            });


        } catch (TimeoutException e) {
        Assert.fail("超时L !! " + timeOut + " 秒之后还没找到元素 [" + by + "]", e);
        }
    }
}

再来还要配置下我们的testng.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">

        <classes>
            <class name="com.saii.NewTest">
                <methods>
                    <include name="f" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

工程配置已经结束了,我们来进行jenkins的一些配置吧。进入jenkins的系统配置 
配置全局属性的键值对 
全局属性
这个地方一定要配置,因为如果不配置成utf-8的话,jenkins从git上拉下来的文件编码格式不是utf-8的格式,这样子就会导致文件中的一些中文直接变成了乱码,到时候直接影响到脚本的运行 
进行maven的项目配置 
这里写图片描述
这里是配置maven的编码以及防止oom还有是maven的本地仓库以及maven的安装地址

新建一个projce后,在构建中新建一个构建步骤 invoke-top-level Maven targets 
这里写图片描述
这里只需要配置正确pom就可以了。

运行结果 
这里写图片描述

OK 运行成功!

原文地址:https://www.cnblogs.com/111testing/p/6506433.html