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



TestNGRunAs


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.


emailable-report


index


methods


What we have learnt so far?


  1. Need for TestNG framework
  2. How to create a Selenium test with TestNG
  3. Annotations used in the test
  4. How to execute the Selenium tests with TestNG
  5. What are reports generated and how they look
  6. What is the output of the execution



0 0