TestNG 自动化开发

运行自动化测试,借助TestNG,

在build.xml 里定制 test 任务:

<!--parameter xml file -->
<import file="build_test_targets.xml"/>

<!-- testng task define -->
<testng haltonfailure="true" failureproperty="test.failed"
configFailurePolicy="skip" timeout="${test.timeout}" outputdir="build/brazil-unit-tests"
haltonskipped="false">
<env key="CORAL_CONFIG_PATH" value="${bp:run.coralconfig}" />
<classpath>
<pathelement path="${classes.dir}" />
<pathelement path="${testclasses.dir}" />
<pathelement path="${bp:run.classpath}" />
<pathelement path="${bp:testrun.classpath}" />
</classpath>
<xmlfileset dir="${testclasses.dir}" includes="${test.testngxmlfile}" />
<!-- <jvmarg value="-Dlog4jConfig=${basedir}/configuration/log4j.conf"></jvmarg> -->
<jvmarg value="-DtestEnvironmentFile=${testEnvironmentFile}"></jvmarg>
<jvmarg value="-Dg2s2version=${g2s2version}"></jvmarg>
<jvmarg value="-DbrowserName=${browserName}"></jvmarg>
<jvmarg value="-Ddevice-type=${device-type}"></jvmarg>
<jvmarg value="-DappActivity=${appActivity}"></jvmarg>
<jvmarg value="-DappPackage=${appPackage}"></jvmarg>
<jvmarg value="-Dapp-wait-activity=${app-wait-activity}"></jvmarg>
<jvmarg value="-Ddevice=${device}"></jvmarg>
<jvmarg value="-DdeviceName=${deviceName}"></jvmarg>
<jvmarg value="-DplatformName=${platformName}"></jvmarg>
<jvmarg value="-Dendpoint=${endpoint}"></jvmarg>
<jvmarg value="-DjobType=${jobType}"></jvmarg>
<jvmarg value="-DplatformName=${platformName}"></jvmarg>
<!-- <jvmarg value="-DtestDetailsFile=${testDetailsFile}"></jvmarg> -->
<jvmarg value="-DautomationName=${automationName}"></jvmarg>
<jvmarg value="-DnewCommandTimeout=${newCommandTimeout}"></jvmarg>
<jvmarg value="-Ddevice=${device}"></jvmarg>
</testng>

在另外一个xml(build_test_targets.xml) 文件里填写 被测的类 和测试的参数,这个文件之前被import 进build.xml 里。 用import 方法可以像套娃一样,随意嵌套,灵活定制参数

<project name="xxx_project" basedir=".">

<target name="run-xxx-TEST-iosBrowser">
<antcall target="run-test-common-iosBrowser">
<param name="test.testngxmlfile" value="xxx_TestSuite_IOSBrowser_config.xml" />
<param name="test.timeout" value="1800000" />
</antcall>
</target>

真正运行selenium 测试的testng 传入函数的参数,放在 xxx_TestSuite_IOSBrowser_config.xml 里, 这个是你自动化框架定义的

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Detail_IOS_Safari" thread-count="1" parallel="methods"
verbose="2" preserve-order="true">
<parameter name="locale" value="CN" /><!-- UK/DE/US -->
<parameter name="stage" value="Prod" /><!-- BETA/GAMMA/PROD -->
<parameter name="driver_server_name" value="none" />
<parameter name="driver_server_port" value="0" />
<parameter name="test_url" value="https://cntech-web-preflight-cn.com/gp/homepage.html"></parameter>
<parameter name="test_browser" value="safari_ios"></parameter>
<parameter name="test_retires" value="3"></parameter>
<parameter name="test_weblabs" value="GLOBAL_STORE_FLAG_40127:T1|GLOBAL_STORE_39815:T1|PHASE2_51123:C|57927:C"></parameter>

<parameter name="test_screenshotflag" value="failure"></parameter>
<parameter name="test_page_timeout_inseconds" value="200"></parameter>
<parameter name="test_element_timeout_inseconds" value="10"></parameter>
<parameter name="report_from" value="[email protected]"></parameter>
<parameter name="report_summary_to" value=""></parameter>
<parameter name="report_detail_to" value=""></parameter>
<parameter name="report_screenshot_to" value=""></parameter>
<parameter name="only_send_failed_report" value="true"></parameter>
<parameter name="report_tim_hashcode" value=""></parameter>
<parameter name="report_pipeline_name" value="xxx-horizonte-prod"></parameter>
<parameter name="upload_to_analyse" value="true"></parameter>

<test name="Detail page of an ASIN">
     <groups>
            <run>
                <include name="mobile" />
            </run>
        </groups>
<classes>
<class name="test.desktop.detailpage.DetailPageTest" />
</classes>
</test>

</suite>

你的框架就是把你的参数传进你的函数里,你的函数要处理这些参数,然后产生相应的assert ion 判断

//需要下载 jcommander.jar, 否则就会报错: 参考:http://www.jcommander.org/#Download
testng-execution:
   [testng] java.lang.NoClassDefFoundError: com/beust/jcommander/ParameterException

// testng tag
need add class path into testng tag, otherwise, will report error cannot find test class **Test

//得要学会如何在 ant 中 debug ,不然有你苦头吃的

You can do this in Eclipse with these steps:

    Be sure to open your build file in the ANT editor (right click on build file -> Open with -> Ant editor).
    Double click in the left margin of your build file where you want breakpoint.
    Open the Ant view (Window -> Show view -> Ant).
    If the build file isn't in the view then you can simply add it.
    Once added right click on the ant target you want to run and select Debug as -> Ant build
    The Debug perspective should open up and the process should stop at your breakpoint where you can step through it
    Sometimes it is required to set the arguments at the time of debugging. It can be set by selecting: Debug as -> Ant build. And then need to select Arguments. And then values can be entered as: -Dprop.name=property value


//之前遇到 “TestNG [Error] Cannot instantiate class xxxTest”
原因是我把 src 和 tst 下的类分别编译到两个目录下,如下:
<javac srcdir="${src}" destdir="${src.classes.path}"
<javac srcdir="${tst}" destdir="${tst.classes.path}"
都编译到一个目录下,就没有问题了,也就是把上面两个 destdir 合成一个


// 遇到错误 [testng] Configuration Failures: 1, Skips: 2
一直找不到原因,后来网上找到高人指点,去看 test-output 里的test report, 一看,果然发现exception log

// 用TestNg @Parameter 传参数传不进去,后来发现是函数没有把传进来的参数存为 内部变量

// Jenkins 设置, 需要增加 JDK 时怎么办?
在 Global Tool Configuration 里可以增加 JDK ,参考: http://blog.csdn.net/anning_88/article/details/50635940
我采取了另外一种方法,在shell 里增加环境变量的设置

// 用 ant -f build.xml 运行testng 测试脚本, 遇到问题,莫名其妙的取不到参数,后来,log只提示发现一个testclass 的error, 而编译过程中该类没有问题,虽然调用testng 参数,但是没有用,在其他类中也没有依赖, 但是testng 把调用它的类无法争取传递参数认为是失败的。后来把这个类删掉,测试运行正常了

// 测试代码中要把无用的import 都去掉,否则testng 也会检测import 的类是否存在或 是否争取的传递testng 参数,否则测试失败

// 如果打开太多的窗口,一次删掉进程用  TaskKill
taskkill /IM chrome.exe /F 强制删除

// xpath 定位如果很多行文字,怎么直接定位到文字相关控件
用text() 获得文字,但有时text()不好使, 就用., 比如 //span[contains(.,'资讯管理')],参考http://www.cnblogs.com/sschen/p/3612503.html

// 出错
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
这个主要是element 过期,重新定位一下。我处理是增加一个waitFroElementPresent函数
driver.waitForElement(Xpath_ServiceRequest_Wizard1_NextStep_ButtonGroup, 5000);

//出错 /org.openqa.selenium.NoAlertPresentException: no alert open
need Add wait like below:
try {
    WebDriverWait wait = new WebDriverWait(driver, 2);
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    System.out.println(alert.getText());
    alert.accept();
    Assert.assertTrue(alert.getText().contains("Thanks."));
} catch (Exception e) {
    //exception handling
}

// 如何快速测试一个API函数
有时候 JAVA API 文档说的不是很清楚,想要快速写个API函数,试验一下,怎么办?就是拷贝该函数,到某个目录下, eclipse 就会快速给你用该函数建一个Snippet APPLICATION  ,这样就方便测试了

猜你喜欢

转载自david-95-live-cn.iteye.com/blog/2311425