Selenium+Maven+Jenkins

在浏览CSDN和博客园的时候,发现很多人的代码自测都没通过,就发布到网上去了


工程目录:

Selenium+Maven+Jenkins_第1张图片

封装类:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.concurrent.TimeUnit;

public class Read {
    public static WebDriver driver;
    //封装driver

    public static void openBaidu() throws InterruptedException {
        //封装打开首页
        System.setProperty("webdriver.firefox.marionette", "src/main/resourcec/geckodriver.exe");
        String Url = "https://www.baidu.com";
        driver =new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(Url);
        driver.manage().window().maximize();
        Thread.sleep(2000);
    }

    public static void refresh(){
        //封装刷新浏览器
        driver.navigate().refresh();
    }

    public static void quit(){
        //封装结束driver
        driver.quit();
    }

}
测试用例类:

import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class Baidu_1 {
    @BeforeSuite
    public void beforeMethod() throws InterruptedException {
        Read.openBaidu();
    }

    @Test
    public void login() throws InterruptedException {
        Read.driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("51testing");
        Read.driver.findElement(By.xpath(".//*[@id='su']")).click();
        Thread.sleep(2000);

        Assert.assertTrue(Read.driver.getPageSource().contains("中国软件测试领跑者"));
        Reporter.log("搜索51testing的测试用例");
    }

    @AfterMethod
    public void afterMethod() {
        Read.refresh();
    }

}
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

public class Baidu_2 {
    @Test
    public void login_2() throws InterruptedException {
        WebElement webElement = Read.driver.findElement(By.xpath(".//*[@id='kw']"));
        webElement.clear();
        webElement.sendKeys("selenium");
        Read.driver.findElement(By.xpath(".//*[@id='su']")).click();
        Thread.sleep(2000);

        Assert.assertTrue(Read.driver.getPageSource().contains("浏览器自动化测试框架"));
        Reporter.log("搜索selenium的测试用例");
    }

    @AfterSuite
    public void afterMethod() {
        Read.quit();
    }
}
Ant build.xml:

xml version="1.0" encoding="UTF-8"?>
name="selenium" default="run" basedir="." >
    name="lib.dir" value="lib"/>
    id="test.classpath">
        dir="${lib.dir}" includes="*.jar"/>
    
    name="run">
        dir="lib/html" />
        in="C:/Selenium/target/surefire-reports/testng-results.xml"
              style="C:/Selenium/lib/testng-results.xsl"
              out="C:/Selenium/lib/html/UI_Test.html" processor="SaxonLiaison">
            name="testNgXslt.outputDir" expression="C:/Selenium/lib/html" />
            name="testNgXslt.showRuntimeTotals" expression="true" />
            name="testNgXslt.sortTestCaseLinks" expression="true" />
            name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS,CONF,BY_CLASS" />
            refid="test.classpath" />
        
    
Maven pom.xml:

xml version="1.0" encoding="UTF-8"?>
xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    webDriver
    selenium
    1.0-SNAPSHOT

    
        UTF-8
        UTF-8
        UTF-8
    

    
        
            org.testng
            testng
            6.11
        

        
            org.seleniumhq.selenium
            selenium-java
            3.7.1
        

        
            org.seleniumhq.selenium
            selenium-firefox-driver
            3.7.1
        

        
            org.seleniumhq.selenium
            selenium-server
            3.7.1
        

        
            org.seleniumhq.selenium
            selenium-chrome-driver
            3.7.1
        

        
            org.seleniumhq.selenium
            selenium-support
            3.7.1
        

        
            org.apache.poi
            poi
            3.17-beta1
        

        
            org.apache.poi
            poi-ooxml
            3.17-beta1
        

        
            mysql
            mysql-connector-java
            8.0.8-dmr
        

        
            org.uncommons
            reportng
            1.1.4
            test
            
                
                    org.testng
                    testng
                
            
        

        
            com.google.inject
            guice
            4.1.0
            test
        

        
            org.apache.velocity
            velocity
            1.7
        

        
            org.codehaus.jettison
            jettison
            1.3.8
        

        
            org.apache.commons
            commons-lang3
            3.6
        

        
            org.apache.httpcomponents
            httpclient
            4.5.3
        

        
            org.jsoup
            jsoup
            1.11.1
        

        
            redis.clients
            jedis
            2.9.0
        

        
            commons-pool
            commons-pool
            1.6
        

        
            ant
            ant
            1.7.0
        
    

    
        
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.20.1
                
                    true
                    
                        testng.xml
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.7.0
                
                    1.8
                    1.8
                
            
        
    

TestNG testng.xml:

xml version="1.0" encoding="UTF-8"?>
suite SYSTEM "http://testng.org/testng-1.0.dtd">
name="百度" verbose="2" preserve-order="true">
    name="搜索51Testing的测试用例">
        
            name="Baidu_1" />
        
    
    name="搜索Selenium的测试用例">
        
            name="Baidu_2" />
        
    
    
        class-name="org.uncommons.reportng.HTMLReporter" />
        class-name="org.uncommons.reportng.JUnitXMLReporter" />
    
配置Jenkins:

Selenium+Maven+Jenkins_第2张图片Selenium+Maven+Jenkins_第3张图片

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

Selenium+Maven+Jenkins_第4张图片Selenium+Maven+Jenkins_第5张图片Selenium+Maven+Jenkins_第6张图片

点击立即构建:

Selenium+Maven+Jenkins_第7张图片Selenium+Maven+Jenkins_第8张图片



你可能感兴趣的:(Selenium(Java))