1、Idea创建一个空的Maven项目
创建后默认项目目录如图所示
2、配置pom.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>selenium-zhtx</groupId> 8 <artifactId>rosa-zhtx</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <!-- maven 运行测试name --> 12 <name>test</name> 13 <url>http://maven.apache.org</url> 14 15 <!-- maven 引用远程库 --> 16 <repositories> 17 <repository> 18 <id>java-net</id> 19 <url>http://download.java.net/maven/2</url> 20 </repository> 21 </repositories> 22 <!-- maven 参数配置,这里引用不同的testng.xml --> 23 <properties> 24 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 25 <xmlFileName>testNG.xml</xmlFileName> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.testng</groupId> 31 <artifactId>testng</artifactId> 32 <version>6.9.9</version> 33 </dependency> 34 <dependency> 35 <groupId>org.uncommons</groupId> 36 <artifactId>reportng</artifactId> 37 <version>1.1.4</version> 38 <scope>test</scope> 39 <exclusions> 40 <exclusion> 41 <groupId>org.testng</groupId> 42 <artifactId>testng</artifactId> 43 </exclusion> 44 </exclusions> 45 </dependency> 46 <dependency> 47 <groupId>velocity</groupId> 48 <artifactId>velocity-dep</artifactId> 49 <version>1.4</version> 50 </dependency> 51 <dependency> 52 <groupId>org.seleniumhq.selenium</groupId> 53 <artifactId>selenium-firefox-driver</artifactId> 54 <version>2.46.0</version> 55 </dependency> 56 <dependency> 57 <groupId>com.google.inject</groupId> 58 <artifactId>guice</artifactId> 59 <version>4.0</version> 60 </dependency> 61 <dependency> 62 <groupId>org.seleniumhq.selenium</groupId> 63 <artifactId>selenium-chrome-driver</artifactId> 64 <version>2.46.0</version> 65 </dependency> 66 67 <dependency> 68 <groupId>org.seleniumhq.selenium</groupId> 69 <artifactId>selenium-htmlunit-driver</artifactId> 70 <version>2.46.0</version> 71 </dependency> 72 73 <dependency> 74 <groupId>org.seleniumhq.selenium</groupId> 75 <artifactId>selenium-support</artifactId> 76 <version>2.46.0</version> 77 </dependency> 78 <dependency> 79 <groupId>log4j</groupId> 80 <artifactId>log4j</artifactId> 81 <version>1.2.16</version> 82 </dependency> 83 </dependencies> 84 85 <build> 86 <plugins> 87 88 <!-- 添加插件 关联testNg.xml --> 89 90 <plugin> 91 <groupId>org.apache.maven.plugins</groupId> 92 <artifactId>maven-surefire-plugin</artifactId> 93 <version>2.5</version> 94 <configuration> 95 <suiteXmlFiles> 96 <suiteXmlFile>res/${xmlFileName}</suiteXmlFile> 97 </suiteXmlFiles> 98 </configuration> 99 </plugin> 100 <!-- 添加插件,添加ReportNg的监听器,修改最后的TestNg的报告 --> 101 <plugin> 102 <groupId>org.apache.maven.plugins</groupId> 103 <artifactId>maven-surefire-plugin</artifactId> 104 <version>2.5</version> 105 <configuration> 106 <properties> 107 <property> 108 <name>usedefaultlisteners</name> 109 <value>false</value> 110 </property> 111 <property> 112 <name>listener</name> 113 <value>org.uncommons.reportng.HTMLReporter, 114 org.uncommons.reportng.JUnitXMLReporter</value> 115 </property> 116 </properties> 117 <workingDirectory>target/</workingDirectory> 118 <forkMode>always</forkMode> 119 </configuration> 120 </plugin> 121 </plugins> 122 </build> 123 124 </project>
3、创建testNG.xml文件
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite" parallel="false"> <test name="Test"> <!--classes> <class name=""> <methods> <include name="*" /> </methods> </class> </classes--> <packages> <package name="com.zhtx.autocase"/> </packages> </test> <listeners> <listener class-name="org.uncommons.reportng.HTMLReporter" /> <listener class-name="org.uncommons.reportng.JUnitXMLReporter" /> </listeners> </suite>
4、编写测试类testZhtx,Strings
package com.zhtx.autocase; import java.util.Iterator; import com.rosa.selenium.Strings; import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.io.File; import java.io.IOException; import java.util.Set; import java.util.concurrent.TimeUnit; public class testZhtx { WebDriver driver; Strings zhtx=new Strings(); @BeforeMethod public void setUp(){ //设置驱动位置 System.setProperty("webdriver.firefox.marionette","D:\libs\geckodriver.exe"); System.setProperty("webdriver.firefox.bin","D:\Program Files\Mozilla Firefox\firefox.exe"); //声明firebug等插件位置 // File fpath=new File("D:\libs\firebug-2.0.18b1.xpi"); // File fbug=new File("D:\libs\firepath-0.9.7-fx.xpi"); driver=new FirefoxDriver();//声明用户信息后,声明驱动方法中要引用该profile //最大化窗口 driver.manage().window().maximize(); driver.get(zhtx.getCsurl()); } @AfterMethod public void tearDown(){ driver.quit(); } @Test public void login(){ WebDriver newWindow; String current_Handles=driver.getWindowHandle(); driver.findElement(By.xpath(".//*[@id='myform']/div/div[2]/div/div[1]/ul/li[1]/input")).clear(); driver.findElement(By.xpath(".//*[@id='myform']/div/div[2]/div/div[1]/ul/li[1]/input")).sendKeys(zhtx.getLoginname()); WebElement pwd=driver.findElement(By.xpath(".//*[@id='myform']/div/div[2]/div/div[1]/ul/li[2]/input")); pwd.clear(); pwd.sendKeys(zhtx.getLoginpassword()); driver.findElement(By.xpath(".//*[@id='btn_login']")).click(); Set<String>all_handles=driver.getWindowHandles(); Iterator<String> it=all_handles.iterator(); while (it.hasNext()){ String sonHandle=it.next(); if(current_Handles==sonHandle)continue; WebDriver window=driver.switchTo().window(sonHandle); } } }
1 package com.rosa.selenium; 2 3 /** 4 * Created by Administrator on 2017/7/6. 5 */ 6 public class Strings { 7 //public String url="https://passport.cnblogs.com/user/signin?ReturnUrl=%2F"; 8 String csurl="http://cs.ccoop.cn"; 9 String url="http://www.baidu.com"; 10 public String loginname="*****"; 11 public String loginpassword="****"; 12 public String ghsNameKey="****"; 13 14 public String getCsurl(){ 15 return csurl; 16 } 17 public void setCsurl(){ 18 this.csurl=csurl; 19 } 20 public String getUrl(){ 21 return url; 22 } 23 public void setUrl(String url){ 24 this.url=url; 25 } 26 public String getLoginname(){ 27 return loginname; 28 } 29 public void setLoginname(){ 30 this.loginname=loginname; 31 } 32 public String getLoginpassword(){ 33 return loginpassword; 34 } 35 public void setLoginpassword(){ 36 this.loginpassword=loginpassword; 37 } 38 public String getGhsNameKey(){ 39 return ghsNameKey; 40 } 41 public void setGhsNameKey(){ 42 this.ghsNameKey=ghsNameKey; 43 } 44 }
5、配置运行文件
6、创建test-output文件夹,存放测试报告
7、build项目
8、运行测试类
执行结果如图所示
也可以进入test-output目录查看html结果