安装TestGN
安装流程
- 在eclispe中安装TestNG Link
Eclipse > Help > install new software > Enter "http://beust.com/eclipse/" in the Work with box > add TestNG > waiting loading > select TestNG > install > next > next > i accept > Finish
after loading done> install anyway > restart
2.也可以使用链接中的方法三, 下载文件 解压到eclipse安装包dropins路径下的新文件夹中
eclipse\dropins\testng-eclipse-6.11\
安装成功的显示
创建TestNG project在eclipse
-
打开eclipse > File > new Project
-
输入 name 然后 click next , 请不要选择 finish
-
当点击next之后, 在libraries一栏去添加jar包,由于已经将TestNG都添加到eclipse中, 所以不选咋导入额外jar包而是选择 Add Libraries
-
导入Selenuim的jar包
-
导入selenium的jar包
先去下载jar包并解压到相关的文件中, 下载路径为http://docs.seleniumhq.org/download/
我自己在project下面创建了一个lib文件,然后将jar包复制到该路径下
将其导入
创建一个新的testNG test file
自己设置packageName和class name
然后就会生成如下的class
下载与浏览器相关版本的driver, Chrome的网站为https://chromedriver.chromium.org/downloads;注意一定要检查自己浏览器对应的版本
输入代码 然后run as TestNG
package demoPakage;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class NewTestng {
public String baseUrl = "http://demo.guru99.com/test/newtours/";
String driverPath = "D:\\EclipseSpace\\firstTestNGProject\\data\\chromedriver.exe";
public WebDriver driver ;
@Test
public void verifyHomepageTitle() {
System.out.println("launching firefox browser");
System.setProperty("webdriver.chrome.marionette", driverPath);
driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
// driver.close();
}
}
输出界面中
Console界面:
TestNG界面
查看html report
参数化的使用
代码如下
package demoPakage;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class demo2 {
public String baseUrl = "http://demo.guru99.com/test/newtours/";
String driverPath = "D:\\\\EclipseSpace\\\\firstTestNGProject\\\\data\\\\chromedriver.exe";
public WebDriver driver ;
@AfterTest //Jumbled
public void terminateBrowser(){
driver.close();
}
@BeforeTest //Jumbled
public void launchBrowser() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.marionette", driverPath);
driver = new ChromeDriver();
driver.get(baseUrl);
}
@Test(priority = 0) //Jumbled
public void verifyHomepageTitle2() {
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
@Test (priority = 1)
public void verifySecurityProject() {
String expectedTitle = "Security Project";
String actualTitle = driver.findElement(By.xpath("//*[@id=\"navbar-brand-centered\"]/ul/li[6]/a")).getText();
Assert.assertEquals(actualTitle, expectedTitle);
}
}
priority的数字越小 优先级越高
使用参数化xml文件来运行testng中的case
TestNG教程:简单实例
TestNG是一个自动化的测试框架,其中NG的意思是“Next Generation”;它是收到Junit的启发来创建的。使用TestNG,你可以产生一个适宜的report,而且容易知道那些case已经pass;那些是skip的。
你还可以单独去执行failed的case,TestNG提供来一个选项, 在输出文件夹中有一个Testng-failed.xml文件,如果你希望执行失败的case,那你就可以直接去运行这个文件。
除了这些简单的概念你能理解, 你也可以学习进阶篇, 如何使用@test标签来创建一个测试方法;如何将这些class转换成测试集文件, 然后使用命令行和eclipse来运行。
- 创建一个新的TestNG Class: demo2
- 代码如下
这个代码是熟悉常用的groups属性,将所有的methods添加上相应的group信息, 注意其中的beforeGroup和afterGroup也有相应的参数 (groups = { })
如果代码没有指定任何的运行设置属性, 那么会按照字母进行排序运行
package demoPakage;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import org.testng.internal.GroupsHelper;
public class demo2 {
public String baseUrl = "http://demo.guru99.com/test/newtours/";
String driverPath = "D:\\\\EclipseSpace\\\\firstTestNGProject\\\\data\\\\chromedriver.exe";
public WebDriver driver ;
@AfterGroups (groups = {"test1", "test2"})
// @Test (groups = {"test2","test1"})
public void terminateBrowser(){
System.out.println("closed it");
driver.close();
}
@BeforeGroups (groups = {"test1", "test2"})
// @Test (groups = {"test2","test1"})
public void launchBrowser() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.marionette", driverPath);
driver = new ChromeDriver();
driver.get(baseUrl);
}
@Test (groups = {"test1"})
public void verifyHomepageTitle2() {
System.out.println("testing1");
String expected = "Welcome: Mercury Tours";
String actualString = driver.getTitle();
Assert.assertEquals(actualString, expected);
}
@Test (groups = {"test2"})
public void check2() {
System.out.println("check2 gogogo");
String expected = "Sign-on: Mercury Tours";
driver.findElement(By.partialLinkText("SIGN-ON")).click();
String actualString = driver.getTitle();
Assert.assertEquals(actualString, expected);
}
}
然后
生成一个testng.xml文件 然后添加具体的class 和 group信息
点击RunAs > Run config > 选择具体的project 和suit文件 点击run
testng.xml文件中设置的dependon
<classes>
<class name="TestGuru99MultipleSession">
<include value="testGuru99TC1" dependsOnMethods=" testGuru99TC2"/>
<include value="testGuru99TC2"/>
</class>
xml文件的层级结果
<suite name = "" parallel="tests">
<test name="" >
<classes>
<class m=name ="" />
</classes>
</test>
<test name ="">
<classes>
<class name="" />
</classes>
</test>
</suite>
TestNG Listeners in Selenium
有两种主要的Listener: WebDriver Listener 和 TestNG Listener; 主要使用testng lisenter
listeners就是用来调整默认的TestNG行为的接口,可以用来自定义report和log。 TestNG本身包含很多listener
IAnnotationTransformer ,
IAnnotationTransformer2 ,
IConfigurable ,
IConfigurationListener ,
IExecutionListener,
IHookable ,
IInvokedMethodListener ,
IInvokedMethodListener2 ,
IMethodInterceptor ,
IReporter,
ISuiteListener,
ITestListener .
ItestListener 为例, 它主要包含下面几个方法
OnStart- OnStart method is called when any Test starts.
onTestSuccess- onTestSuccess method is called on the success of any Test.
onTestFailure- onTestFailure method is called on the failure of any Test.
onTestSkipped- onTestSkipped method is called on skipped of any Test.
onTestFailedButWithinSuccessPercentage- method is called each time Test fails but is within success percentage.
onFinish- onFinish method is called after all Tests are executed.
Step by Step for ITestlistener
-
创建一个class 继承“ITestListener”
-
source > override/implement methods >
selectAll and click OK
于是得到如下:
重写其中的部分代码
package demoPakage;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class listenerDemo implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("* START * :"+result.getName());
}
@Override
public void onTestSuccess(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("*** passed :"+result.getName() );
}
@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
System.out.println("---- failed :" +result.getName());
}
@Override
public void onTestSkipped(ITestResult result) {
// TODO Auto-generated method stub
System.out.println(" skiped :"+result.getName());
}
@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// TODO Auto-generated method stub
ITestListener.super.onTestFailedButWithinSuccessPercentage(result);
}
@Override
public void onTestFailedWithTimeout(ITestResult result) {
// TODO Auto-generated method stub
ITestListener.super.onTestFailedWithTimeout(result);
}
@Override
public void onStart(ITestContext context) {
// TODO Auto-generated method stub
ITestListener.super.onStart(context);
}
@Override
public void onFinish(ITestContext context) {
// TODO Auto-generated method stub
ITestListener.super.onFinish(context);
}
}
两种方式可以使用listener
第一种是在testng class的类上方添加 @Listeners(demoPakage.listenerDemo.class)
第二种是在testng.xml文件中添加
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<listeners>
<listener class-name="demoPakage.listenerDemo.class" />
</listeners>
<test thread-count="5" name="Test">
<classes>
<class name="demoPakage.demo2"/>
</classes>
<groups>
<run>
<include name="test2" />
</run>
</groups>
</test> <!-- Test -->
</suite> <!-- Suite -->
竟然报错
[RemoteTestNG] detected TestNG version 7.0.0
org.testng.TestNGException: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 10; The content of element type "test" must match "(method-selectors?,parameter*,groups?,packages?,classes?)".
at org.testng.TestNG.parseSuite(TestNG.java:330)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:350)
at org.testng.TestNG.initializeEverything(TestNG.java:980)
at org.testng.remote.support.RemoteTestNG6_12.initialize(RemoteTestNG6_12.java:22)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:98)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 10; The content of element type "test" must match "(method-selectors?,parameter*,groups?,packages?,classes?)".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.testng.xml.XMLParser.parse(XMLParser.java:39)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
at org.testng.xml.Parser.parse(Parser.java:160)
at org.testng.xml.Parser.parse(Parser.java:246)
at org.testng.TestNG.parseSuite(TestNG.java:298)
... 6 more