jenkins+selenium+eclipse+ant+testng整合心得

来源:互联网 发布:恋爱的双马尾 知乎 编辑:程序博客网 时间:2024/06/14 07:11

对于一个自动化测试新手,整合selenium还是需要注意很多地方,一个是Jenkins依赖插件包和其版本问题,一个是浏览器和selenium版本问题,还有就是浏览器驱动版本的问题,还要学会调试的方法,要是不注意,你会发现很郁闷,所有组件都已经ready,但是就是不能成功,下面给大家介绍一下我的整合心得,抛开很多错误的示范,直接上正确的。


一. 先安装Jenkins,安装Hudson Seleniumhq plugin插件,此处就不多说了,上网查一下安装方法,比较简单

系统配置:

selenium server配置:

先下载好对应版本的selenium-server包,按下图进行配置:

JDK和ant配置:


二. 环境准备eclipse+ant+testng

eclipse安装-略

ant,下载ant安装包,配置ANT_HOME环境变量,并添加path路径,eclipse如果没有按照ant的,可以安装ant插件

(这里解释一下,为什么需要安装两个,因为Jenkins通过ant构建的时候是使用自己安装的ant来构建的)

testng,在eclipse中安装testng插件


三.selenium和eclipse整合

1.版本选择

selenium2对火狐浏览器支持比较好,所以先来看看selenium和火狐浏览器的版本对应:


我的环境就是采用2.52.0->39.0.3


2. 创建项目

下载selenium2.52.0的包,把selenium-java-2.52.0.jar,selenium-java-2.52.0-srcs.jar,以及libs下所有jar都添加到eclipse项目里面

创建项目,项目右键,创建一个testNG的类,编写一个测试用例脚本(包含三个浏览器的实现):

package com.selenium;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.ie.InternetExplorerDriver;import org.testng.Assert;import org.testng.annotations.Test;public class NewTest {@Testpublic void testbaidu() {//IE//System.setProperty("webdriver.ie.driver","src/resource/IEDriverServer.exe");//InternetExplorerDriver driver=new InternetExplorerDriver();//Chrome//System.setProperty("webdriver.chrome.driver","src/resource/chromedriver.exe");//ChromeOptions options = new ChromeOptions(); //options.setBinary("D:/ToolSoftware/GoogleChromePortable52/App/Google Chrome/chrome.exe"); //ChromeDriver driver = new ChromeDriver(options); //FirefoxSystem.setProperty("webdriver.firefox.bin", "C:/Program Files/Mozilla Firefox/firefox.exe");WebDriver driver = new FirefoxDriver();driver.get("https://www.baidu.com/");// we expect the title “Google “ should be presentString Expectedtitle = "百度一下,你就知道";// it will fetch the actual titleString Actualtitle = driver.getTitle();System.out.println("Before Assetion " + Expectedtitle + Actualtitle);// it will compare actual title and expected titleAssert.assertEquals(Actualtitle, Expectedtitle);// print out the resultSystem.out.println("After Assertion " + Expectedtitle + Actualtitle+ " Title matched ");}}

在项目根目录创建一个testng.xml脚本:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"><suite name="testng2" parallel="none" verbose="1">  <test name="com.selenium.NewTest">  <!-- <groups>       <run>          <include name="NewTest" />        </run>     </groups> -->     <classes>      <class name="com.selenium.NewTest"/>    </classes>  </test> <!-- Test --></suite> <!-- Suite -->


在项目根路径创建build.xml文件:

<?xml version="1.0" encoding="UTF-8" ?><project name="Jenkins_selenium" default="run" basedir=".">    <echo  message="import libs" />    <path id="run.classpath">        <fileset dir="${basedir}">        <include name="lib/testng.jar" />            <include name="lib/sikuli-api-1.1.0.jar" />        <include name="lib/sikuli-webdriver-1.0.1.jar" />        <include name="lib/bsh-2.0.jar" />        <include name="lib/snakeyaml-1.13.jar" />        <include name="lib/jcommander-1.30.jar" />        <include name="libs/*.jar" />        </fileset>         <fileset dir="${basedir}/lib/selenium">            <include name="selenium-java-2.52.0.jar" />        <include name="selenium-java-2.52.0-srcs.jar" />        </fileset>    </path>    <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="run.classpath" />    <target name="clean">        <delete dir="build"/>    </target>    <target name="compile" depends="clean">        <echo message="mkdir"/>        <mkdir dir="build/classes"/>        <javac srcdir="src" destdir="build/classes" debug="on" encoding="UTF-8">            <classpath refid="run.classpath"/>        </javac>    </target>    <path id="runpath">          <path refid="run.classpath"/>          <pathelement location="build/classes"/>        </path>     <target name="run" depends="compile">        <testng  classpathref="runpath"  outputDir="test-output">            <xmlfileset dir="${basedir}" includes="testng2.xml"/>            <jvmarg value="-ea" />        </testng>    </target></project>


3.selenium和chromedriver/iedriver版本的对应关系

selenium2需要调用chrome或者ie浏览器,需要引入驱动文件,版本对应关系如下:

->selenium和chrome:


所有chromedriver均可在下面链接中下载到:

http://chromedriver.storage.googleapis.com/index.html

此处引用:http://blog.csdn.net/wang8978/article/details/52934506

->selenium和ie:

下载IEDriverServer。进入索引页,首先选择版本号,IEDriverServer的版本号和Selenium的版本号一定要一致,因为我选择的是selenium-2.52,所以IEDriverServer也选择的是2.52版本的。

 针对windows vista和windows 7上的IE7或者更高的版本,必须在IE选项设置的安全页中,4个区域的启用保护模式的勾选都去掉(或都勾上),即保持四个区域的保护模式是一致的。如下图所示:

这里写图片描述

针对IE10和更高的版本,必须在IE选项设置中的高级页中,取消增强保护模式。如下图所示

这里写图片描述

浏览器的缩放比例必须设置为100%,这样元素定位才不会出现问题,如下图所示

这里写图片描述

针对IE11,需要修改注册表。如果是32位的windows,key值为

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_BFCACHE

如果是64位的windows,key值为

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_BFCACHE

如果key值不存在,就添加。之后在key内部创建一个iexplorer.exe,DWORD类型,值为0,我的windows是64位的,修改后的注册表如下图所示: 

这里写图片描述

这里写图片描述

此处引用:http://blog.csdn.net/zyl26/article/details/51011073


四、 创建Jenkins job

创建一个自由风格的job

配置ant:

报告配置:

开始构建:

Started by user guyingjie[EnvInject] - Loading node environment variables.Building in workspace D:\BuildSoftware\Jenkins\workspace\testngtest2[WorkSpaces] $ cmd.exe /C "D:\BuildSoftware\apache-ant-1.9.6\bin\ant.bat -file Jenkins_selenium run && exit %%ERRORLEVEL%%"Buildfile: E:\WorkSpaces\Jenkins_selenium\build.xml     [echo] import libsclean:   [delete] Deleting directory E:\WorkSpaces\Jenkins_selenium\buildcompile:     [echo] mkdir    [mkdir] Created dir: E:\WorkSpaces\Jenkins_selenium\build\classes    [javac] E:\WorkSpaces\Jenkins_selenium\build.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds    [javac] Compiling 1 source file to E:\WorkSpaces\Jenkins_selenium\build\classesrun:   [testng] [Parser] Running:   [testng]   E:\WorkSpaces\Jenkins_selenium\testng2.xml   [testng]    [testng] Before Assetion 百度一下,你就知道    [testng] After Assertion 百度一下,你就知道 Title matched   [testng]    [testng] ===============================================   [testng] testng2   [testng] Total tests run: 1, Failures: 0, Skips: 0   [testng] ===============================================   [testng] BUILD SUCCESSFULTotal time: 46 seconds[htmlpublisher] Archiving HTML reports...[htmlpublisher] Archiving at PROJECT level E:\WorkSpaces\Jenkins_selenium\test-output\html to D:\BuildSoftware\Jenkins\jobs\testngtest2\htmlreports\HTML_ReportFinished: SUCCESS

如上代表成功,可以到项目路径下Jenkins_selenium\test-output\html 查看网页报告,也可以在Jenkins页面查看:


注意:在Jenkins上构建执行的时候,不会不会弹出浏览器,是后台运行的


五、在eclipse上运行测试脚本

右键run as,选择testng test运行,可以看到浏览器执行脚本。

阅读全文
1 0
原创粉丝点击