在浏览优快云和博客园的时候,发现很多人的代码自测都没通过,就发布到网上去了
工程目录:
封装类:
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"?> <project name="selenium" default="run" basedir="." > <property name="lib.dir" value="lib"/> <path id="test.classpath"> <fileset dir="${lib.dir}" includes="*.jar"/> </path> <target name="run"> <delete dir="lib/html" /> <xslt 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"> <param name="testNgXslt.outputDir" expression="C:/Selenium/lib/html" /> <param name="testNgXslt.showRuntimeTotals" expression="true" /> <param name="testNgXslt.sortTestCaseLinks" expression="true" /> <param name="testNgXslt.testDetailsFilter" expression="FAIL,SKIP,PASS,CONF,BY_CLASS" /> <classpath refid="test.classpath" /> </xslt> </target> </project>Maven pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project 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"> <modelVersion>4.0.0</modelVersion> <groupId>webDriver</groupId> <artifactId>selenium</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.soureEncodng>UTF-8</maven.compiler.soureEncodng> </properties> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.11</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.7.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-firefox-driver</artifactId> <version>3.7.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-server</artifactId> <version>3.7.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>3.7.1</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-support</artifactId> <version>3.7.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17-beta1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17-beta1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.8-dmr</version> </dependency> <dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.testng</groupId> <artifactId>testng</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.1.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.8</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.1</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>ant</groupId> <artifactId>ant</artifactId> <version>1.7.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <testFailureIgnore>true</testFailureIgnore> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>TestNG testng.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="百度" verbose="2" preserve-order="true"> <test name="搜索51Testing的测试用例"> <classes> <class name="Baidu_1" /> </classes> </test> <test name="搜索Selenium的测试用例"> <classes> <class name="Baidu_2" /> </classes> </test> <listeners> <listener class-name="org.uncommons.reportng.HTMLReporter" /> <listener class-name="org.uncommons.reportng.JUnitXMLReporter" /> </listeners> </suite>配置Jenkins:
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")
点击立即构建: