TestNG Automated Development

运行自动化测试,借助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>
xml. Using the import method, you can nest like a nesting doll, and flexibly customize parameters <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>

Your framework is to pass your parameters into your function, your function needs to process these parameters, and then generate the corresponding assert ion judgment

//You need to download jcommander.jar, otherwise it will report an error: Reference: 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

//You have to learn how to debug in ant, otherwise 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}"
All compiled into one directory, there is no problem, that is, the above two destdirs are combined into one


// I encountered an error [testng] Configuration Failures: 1, Skips: 2 I
can't find the reason, and later I found the expert advice on the Internet, Looking at the test report in test-output, I found the exception log

// The parameters passed in TestNg @Parameter could not be passed in. Later, it was found that the function did not save the passed parameters as internal variables

. // Jenkins settings, need to be added What about JDK?
JDK can be added in Global Tool Configuration, refer to: http://blog.csdn.net/anning_88/article/details/50635940
I took another method, increase the setting of environment variables in the shell

// use ant -f build.xml runs the testng test script, and encountered a problem, inexplicably unable to get the parameters. Later, the log only prompted that an error of testclass was found, and there was no problem with the class during the compilation process. Although the testng parameter was called, it was useless. There are also no dependencies in the class, but testng considers it a failure that the class calling it cannot strive to pass parameters. Later, this class was deleted, and the test ran normally.//The

useless import should be removed from the test code, otherwise testng will also detect whether the imported class exists or whether the testng parameter is passed, otherwise the test will fail.

// If it is too open If there are many windows, delete the process at one time and force delete it with TaskKill
taskkill /IM chrome.exe /F

// xpath positioning If there are many lines of text, how to directly locate the text-related controls?
Use text() to get the text, but sometimes text() is not good, just use ., such as //span[contains(.,'information management') ], refer to http://www.cnblogs.com/sschen/p/3612503.html

// Error
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
This is mainly because the element has expired, re- Position it. My solution is to add a waitFroElementPresent function
driver.waitForElement(Xpath_ServiceRequest_Wizard1_NextStep_ButtonGroup, 5000);

//Error/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
}

// How to quickly test an API function
Sometimes the JAVA API documentation is not very clear. I want to quickly write an API function and try it out. What should I do? Just copy the function, go to a certain directory, eclipse will quickly build a Snippet APPLICATION for you with this function, which is convenient for testing

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326612363&siteId=291194637