Steps to follow:
@Test : Enter Username & Password to Login, Print console message and Log out
@AfterMethod : Close Firefox browser
- Press Ctrl+N , select “TestNG Class” under TestNG category and click Next. Or Right click on Test Case folder, go to TestNG and select “TestNG Class“.
- If your project is set up and you have selected the Test Case folder before creating TestNG class then the source folder and the package name will be prepopulated on the form. Set class name as ‘TestNG‘. Under Annotations, check “@BeforeMethod”, “@AfterMethod” and click Finish. That’s it.
- Now it will display the newly created TestNg class under the Test Case package (folder). TestNG class will be displayed with three empty methods. One method f() by default and before & after method, as selected during the creation of the class. Now it is the time to write the first TestNG test case.
- Let’s take an example of login and divide the test case in to three parts.
@Test : Enter Username & Password to Login, Print console message and Log out
@AfterMethod : Close Firefox browser
package
automationFramework;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.Test;
import
org.testng.annotations.BeforeMethod;
import
org.testng.annotations.AfterMethod;
public
class TestNG {
public WebDriver driver;
@BeforeMethod
public void beforeMethod() {
// Create a new instance of the Firefox
driver
driver = new FirefoxDriver();
//Put an Implicit wait, this means that
any search for elements on the page could take the time the implicit wait is
set for before throwing exception
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
//Launch the Website
driver.get("http://www.gmail.com");
}
@Test
public void testLogin() {
// Find the element that's ID attribute
is 'txtUsername' (Username)
// Enter Username on the element found by
above desc.
driver.findElement(By.id("txtUsername")).sendKeys("testuser1");
// Find the element that's ID attribute
is 'txtPwd' (Password)
// Enter Password on the element found by
the above desc.
driver.findElement(By.id("txtPwd")).sendKeys("Test@123");
// Now submit the form.
driver.findElement(By.id("btnLogin")).click();
// Print a Log In message to the screen
System.out.println(" Login
Successfully, now it is the time to Log Off buddy.");
// Find the element that's ID attribute
is 'btnLogout'
driver.findElement(By.id("btnLogout")).click();
}
@AfterMethod
public void afterMethod() {
// Close the driver
driver.quit();
}
}
5. Run the test by right click on the test case script and select Run As
> TestNG
Test.
Results of running the Testng Test Case
Results of running the Testng Test Case
6. Give it few minutes to
complete the execution; once it is finished the results will be displayed in the TestNg Result window.
It displayed ‘passed : 1′. This means test is
successful and Passed.
There are 3 sub tabs. “All Tests”, “Failed Tests” and “Summary”. Just click “All Tests” to see what is there.
7. TestNG also produce HTML reports. To access those reports go to the Project directory and open test-output folder.
8. Open ‘emailable-report.html‘, as this is an html report open it with browser.
9. TestNG also produce ‘index.html‘ report and it resides in the same test-output folder. This report gives the link to all the different component of the TestNG reports like Groups & Reporter Output. On clicking these will display detailed descriptions of execution.
There are 3 sub tabs. “All Tests”, “Failed Tests” and “Summary”. Just click “All Tests” to see what is there.
7. TestNG also produce HTML reports. To access those reports go to the Project directory and open test-output folder.
8. Open ‘emailable-report.html‘, as this is an html report open it with browser.
9. TestNG also produce ‘index.html‘ report and it resides in the same test-output folder. This report gives the link to all the different component of the TestNG reports like Groups & Reporter Output. On clicking these will display detailed descriptions of execution.
No comments:
Post a Comment