TestNG – Test Automation with Selenium
来源:互联网 发布:淘宝店铺装修教程手机版 编辑:程序博客网 时间:2024/05/07 11:16
TestNG framework can be used for automation testing with Selenium (web application automation testing tool). Then, definitely a question will popup in our mind “why this framework is needed?” Is it possible to execute Selenium tests without frameworks like JUnit or TestNG. Answer is “Yes”, it is possible to execute Selenium tests without using these frameworks. But, is it possible to get proper reports without framework? How the verification points or checkpoints are going to be handled? How the exception handling is done? If we do not have any frameworks, then it is difficult to get proper reports, handle checkpoints, or exception handling.
Either we should use any of the frameworks available like JUnit, TestNG or we should design our own framework.
In one of the previous post, I have explained how to setup TestNG with Eclipse IDE. It is better go through that post before doing anything explained in this post.
Let us have a look at the code snippet below:
package com.selftechy.testng;import com.thoughtworks.selenium.*;import org.testng.annotations.*;public class TestNGDemo extends SeleneseTestBase{public Selenium selenium;@BeforeMethodpublic void setUp()throws Exception{selenium = new DefaultSelenium("localhost",4444,"*chrome","http://selftechy.com");selenium.start();selenium.windowMaximize();}@Testpublic void testNGDemo() throws Exception {selenium.open("/");selenium.click("link=TestNG (Next Generation Testing Framework) – Understanding Annotations");Thread.sleep(10000);verifyTrue(selenium.isTextPresent("Annotation:"));selenium.click("link=Selenium");Thread.sleep(10000);selenium.click("css=a[title=\"Introduction to Selenium\"]");Thread.sleep(10000);}@Testpublic void testTestAbout() throws Exception {selenium.open("/");selenium.click("link=About");selenium.waitForPageToLoad("30000");verifyTrue(selenium.isTextPresent("Selenium, QTP, Java"));selenium.click("link=Home");selenium.waitForPageToLoad("30000");selenium.click("link=TestNG – Next Generation Testing Framework");selenium.waitForPageToLoad("30000");verifyTrue(selenium.isTextPresent("Need for a Testing Framework:"));}@AfterMethodpublic void tearDown(){selenium.stop();}}
The Java class “TestNGDemo” is implemented in Eclipse IDE using TestNG framework.
In the above code, there are two test methods, which are marked with @Test annotation. There are two other methods, “setUp” and “tearDown” which are marked with @BeforeMethod and @AfterMethod annotations. Hence Before executing each test, setUp method will be executed. After the execution of each test, tearDown gets executed. Hence, Selenium gets instantiated and browser gets opened and closed twice during the execution.
How to execute the test?
Click Run –> Run As –> TestNG Test
Output of the execution should be as below:
[TestNG] Running: C:\Documents and Settings\parwathi\Local Settings\Temp\testng-eclipse-1339593504\testng-customsuite.xmlPASSED: testNGDemoPASSED: testTestAbout=============================================== Default test Tests run: 2, Failures: 0, Skips: 0==============================================================================================Default suiteTotal tests run: 2, Failures: 0, Skips: 0===============================================[TestNG] Time taken by org.testng.reporters.EmailableReporter@1a679b7: 15 ms[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@1e51060: 16 ms[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@337d0f: 0 ms[TestNG] Time taken by org.testng.reporters.XMLReporter@e102dc: 0 ms[TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 0 ms
Test execution also creates set of XML & HTML reports. TestNG creates “test-output” folder in the root folder, inside which we can see the reports. Below are the screenshots of some of the reports created by TestNG.
What we have learnt so far?
- Need for TestNG framework
- How to create a Selenium test with TestNG
- Annotations used in the test
- How to execute the Selenium tests with TestNG
- What are reports generated and how they look
- What is the output of the execution
- TestNG – Test Automation with Selenium
- TestNG-Excute selenium with TestNG.
- Selenium 2.0 WebDriver – A Test Automation example
- Lightweight UI Test Automation with .NET
- TestNG – Timeout Test
- Automation Test
- Automation Test
- Selenium TestNG
- selenium TestNG
- How to test java applet with Selenium
- TestNG: different test method use different DataProvider with Reflect
- TestNG + Selenium – Load Testing Example
- 初识 Selenium (Browser Automation)
- 转 Test Automation Frameworks
- TEST AUTOMATION EFFORT ESTIMATION
- Test Automation Using C#
- Automation test Analyze
- UI Automation test
- leetcode:Sum Root to Leaf Numbers (二叉树的DFS)
- Cocoa 框架 For iOS(一) 框架的介绍,Objectivie-C运行时能力的解析等
- JAVA进阶-IO流(2)
- Expression Tree上手指南 (二)
- Android应用自动更新功能的实现!!!
- TestNG – Test Automation with Selenium
- android性能优化之即时通讯优化
- http协议------费了番脑筋
- [Android]Volley源码分析(五)消息分发器Dispatcher
- 控制台应用程序
- HTML关键字冲突
- Myeclipse集成安装SVN(link方法)
- An error was encountered while running (Domain = LaunchServicesError, Code = 0)
- Expression Tree上手指南 (三)