In this TestNG tutorial, We are going to cover How you can Start the TestNG in Selenium Java.
TestNG is next generation testing framework like JUnit & NUnit but with extra features(Read keywords, Generate Test Reports, Generate logs) and easy to use. TestNG framework can cover different type of the tests like Integration, Unit, end-to-end, etc.
Installing TestNG in Eclispe:
- Open Eclipse , Select Help / Software updates / Find and Install.
- Search for new features to install.
- New remote site.
- For Eclipse 3.4 and above, enter http://beust.com/eclipse.
- For Eclipse 3.3 and below, enter http://beust.com/eclipse1.
- Make sure the checkbox next to URL is checked and click Next.
- Eclipse will then guide you through the process.
We are going to Test a Demo website “http://demoaut.com/mercuryregister.php” and will write a Test case
to Do a Registration in it.
Project Structure:
Step1:
Create a project in the eclipse. Add the Selenium StandAlone jar and TestNG jar into a new libs folder.
Make sure you add them to Build Path by Right Click – Build Path –> Add to Build Path.
Step2:
Create a Base Class with Test Fixture methods e.g. Setup and Tear Down. Note: I have added function name
as JUnit you can name them as you want), Add annotations as Before Method and After Method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.scrolltest.testng; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; public class BaseClass { WebDriver driver; @BeforeMethod public void setUp() { driver = new FirefoxDriver(); } @AfterMethod public void tearDown() { // driver.close(); driver.quit(); } } |
Step3 :
Create a Separate Class with Name Registration .java to write a Test case for Registration of the
Demo: Website http://demoaut.com/mercuryregister.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.scrolltest.testng; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.Select; import org.testng.Reporter; public class RegisterationPageObect { private WebDriver driver; public RegisterationPageObect(WebDriver driver) { this.driver = driver; } public void RegisterUser() { driver.findElement(By.name("firstName")).sendKeys("Pramod"); driver.findElement(By.name("lastName")).sendKeys("Dutta"); driver.findElement(By.name("phone")).sendKeys("981457452"); WebElement t = driver.findElement(By.id("userName")); driver.findElement(By.id("userName")).sendKeys("a@c.com"); Reporter.log(""+t.getAttribute("maxlength")); // #Drop Down new Select(driver.findElement(By.name("country"))) .selectByVisibleText("ANGUILLA"); // register driver.findElement(By.name("register")).click(); } } |
Step4: Now let’s create a Final class and TestNG xml file.
MainClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.scrolltest.testng; import org.openqa.selenium.By; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.Test; public class MainClass extends BaseClass { @Test public void testRegisterationInDemoAUT() throws InterruptedException { RegistrationPageObect pageOject = new RegistrationPageObect(driver); driver.get("http://demoaut.com/mercuryregister.php"); pageOject.RegisterUser(); assert driver .findElement(By.tagName("body")) .getText() .contains( "Thank you for registering. You may now sign-in using the user name and password you've just entered."); assert driver.findElement(By.tagName("body")).getText() .contains("Dear Pramod Dutta,"); } } |
Step5:
Now Run the Test by Creating new TestNG configuration by Clicking on the Project->Run Configuration, Select
the TestSuite and browse the XML file(testng.xml). and Now Run the Project.
Step6:
After run successfully, If you refresh you project you will see the test-output folder , Open “index.html” and see that You Test Suite Results. Click on the Report output link you can see max length also.(Used as Logging)
You may also want to checkout