windows下 maven+selenium+testng项目搭建(七)

Selenium2.47.1 + Maven3.3.9 + TestNG6.8.8html

windows准备好如下环境
一、 Jdk,环境变量配置
二、 maven环境
三、 eclipse 开发工具 ,eclipse安装好testng插件java

动手:
一、 Maven安装配置,参考个人博客:http://www.cnblogs.com/lincj/p/5470032.html

二、 新建个文件夹test,在cmd进入到test目录:
运行mvn archetype:generate
回车直到,看到:"Define value for groupId: :"中止
 输入groupID,格式:"com.test",回车
 输入artifactId,格式:"test",回车
 回车
输入package,格式:"com.test.test",回车  输入Y,回车 
看到BUILD SUCCESS,成功 maven工程建立完毕apache

三、工程作以下修改,把默认建立的src下目录所有删掉,在src下建立一个目录testScript

四、 进入test文件夹,有个pom.xml打开,windows

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.test</groupId>
      <artifactId>test</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>test</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>

把以上内容替换为:eclipse

     <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>com.test</groupId>
      <artifactId>lincj_test</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>jar</packaging>

      <name>lincj_test</name>
      <url>http://maven.apache.org</url>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>

      <dependencies>
        <dependency>
           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>2.47.1</version>
              <scope>test</scope>
         </dependency>
         <dependency>
           <groupId>org.testng</groupId>
           <artifactId>testng</artifactId>
           <version>6.8.8</version>
         </dependency>
      </dependencies>

        <build>
            <sourceDirectory>${basedir}/src</sourceDirectory>
            <testSourceDirectory>${basedir}/src</testSourceDirectory>
            <plugins>
                
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
                
              </plugin>
              <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                    
                </plugin>

                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
                
            </plugins>
        </build>

    </project>

五、在test文件夹下,新建一个批处理文件:build.bat
内容以下 maven

@echo off 
setlocal 
call mvn eclipse:clean 
call mvn -npu eclipse:eclipse -Dwtpversion=1.0 -DdownloadSources=true 
pause
endloca

六、 打开eclipse,安装testng插件(可参考个人博客:http://www.cnblogs.com/lincj/p/5470903.html),若是已经安装跳过工具

七、打开eclipse—》 window—》Preferences,设置两个地方开发工具


Name:M2_REPO Path:C:/Documents and Settings/Administrator/.m2/repository 注:Path为Maven本地仓库测试

九、 重启eclipse—》import—》existing projects into workspace,导入项目"test" 项目
在test项目下建立testng文件,内容以下:ui

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test">
    <test name="version" preserve-order="true">
        <classes>
            <class name="testScript.login">
                <methods>
                    <include name="login" />
                </methods>
            </class>        
        </classes>
    </test>
</suite>

 

关于testng.xml文件各类标签的意义你们自行上网上去查

十、 Maven是约定优于配置,把测试脚本建立在src/testScript下,建立如下脚本:login.java,代码以下:

package testScript;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class login {
    
    WebDriver driver;
    
    @Test
    public void login(){
          driver=new FirefoxDriver();
          driver.get("http://www.baidu.com");
          driver.quit();
    }
}

十一、到此项目搭建完成,能够经过dos窗口,进入test目录,运行:mvn install 或者在eclipse中右键项目选择maven->maven install