You are on page 1of 10

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;
@Test
public void main() {
// Find the element that's ID attribute is 'account'(My Account)
driver.findElement(By.id("account")).click();
// Find the element that's ID attribute is 'log' (Username)
// Enter Username on the element found by above desc.
driver.findElement(By.id("log")).sendKeys("testuser_1");
// Find the element that's ID attribute is 'pwd' (Password)
// Enter Password on the element found by the above desc.
driver.findElement(By.id("pwd")).sendKeys("Test@123");
// Now submit the form. WebDriver will find the form for us from the eleme
nt
driver.findElement(By.id("login")).click();
// Print a Log In message to the screen
System.out.println(" Login Successfully, now it is the time to Log Off bud
dy.");
// Find the element that's ID attribute is 'account_logout' (Log Out)
driver.findElement(By.id("account_logout"));
}
@BeforeMethod

out.annotations.SECONDS). import org.wpengine.annotations.Test.annotations.testng. TimeUnit. } @AfterMethod public void afterMethod() { // Close the driver driver.implicitlyWait(10. import org. import org.testng.AfterTest.testng.testng. this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception driver.annotations.testng.annotations.public void beforeMethod() { // Create a new instance of the Firefox driver driver = new FirefoxDriver(). import org.annotations.println("This is the Test Case 1").get("http://www.annotations.BeforeClass. } .testng.testng.BeforeTest.BeforeMethod.manage().AfterSuite. import org.com"). import org. import org.quit().annotations.annotations. import org. import org. } } ------------------package automationFramework.timeouts().testng. //Put a Implicit wait. public class Sequencing { @Test public void testCase1() { System.onlinestore.AfterMethod.toolsqa.testng.BeforeSuite. //Launch the Online Store Website driver.AfterClass.

println("This will execute before the Class") .out. } @BeforeMethod public void beforeMethod() { System. } @BeforeSuite . } @BeforeClass public void beforeClass() { System. } @BeforeTest public void beforeTest() { System.println("This will execute after every Method ").out.out.@Test public void testCase2() { System.println("This will execute before every Metho d").out.println("This will execute after the Class"). } @AfterTest public void afterTest() { System. } @AfterMethod public void afterMethod() { System.out.println("This is the Test Case 2").out.out.println("This will execute after the Test").println("This will execute before the Test"). } @AfterClass public void afterClass() { System.

println("Batch Car .out.println("Batch Scooter . } @Test (groups = { "Car" }) public void Car2() { System.out.Test car 2"). import org.println("This will execute after the Test Sui te").out. } . } } -------------------package automationFramework.Test car 1").Test scooter 2").println("This will execute before the Test Su ite").public void beforeSuite() { System.out.out.testng.println("Batch Scooter .out.Test. } @AfterSuite public void afterSuite() { System.println("Batch Car . } @Test (groups = { "Scooter" }) public void Scooter2() { System.annotations. } @Test (groups = { "Scooter" }) public void Scooter1() { System.Test scooter 1"). public class Grouping { @Test (groups = { "Car" }) public void Car1() { System.

public class MultipleTest { public WebDriver driver.selenium.out.println("Batch Sedan Car . import org.Test sedan 1").Test.openqa.testng. "Sedan Car" }) public void Sedan1() { System. import org. @Test public void One() { System.WebDriver. } } <suite name="Suite"> <test name="Practice Grouping"> <groups> <define name="All"> <include name="Car"/> <include name="Scooter"/> </define> <run> <include name="All"/> </run> </groups> <classes> <class name="automationFramework.@Test (groups = { "Car". .out.Grouping" /> </classes> </test> </suite> ---------------package automationFramework.annotations.println("This is the Test Case number One").

} @Test public void Four() { System. @Test(priority = 0) public void One() { System.testng.WebDriver.} @Test public void Two() { System.println("This is the Test Case number One").out.println("This is the Test Case number Four"). import org. public class MultipleTest { public WebDriver driver.selenium.out. } @Test(priority = 2) public void Three() { . } @Test public void Three() { System.out.annotations.out. } } ----------package automationFramework.Test.openqa.println("This is the Test Case number Three"). import org.println("This is the Test Case number Two").out. } @Test(priority = 1) public void Two() { System.println("This is the Test Case number Two").

} @Test(priority = 2. } @Test(priority = 1) public void Two() { System.out.out.Test.out. } } .out.println("This is the Test Case number Three"). @Test(priority = 0) public void One() { System.annotations. enabled = false) public void Three() { System. } @Test(priority = 3) public void Four() { System.out. } } -------package automationFramework.openqa. import org.out.println("This is the Test Case number Four"). public class MultipleTest { public WebDriver driver. } @Test(priority = 3) public void Four() { System.println("This is the Test Case number Three").testng. import org.println("This is the Test Case number Four").println("This is the Test Case number One").WebDriver.System.println("This is the Test Case number Two").selenium.

driver. driver.annotations.testng.findElement(By. driver. String sPassword) { driver = new FirefoxDriver().get("http://www.manage().com").implicitlyWait(10. driver. import org.//*[@id='account_logout']/a")).SECONDS).//*[@id='account']/a")). import org.By.selenium. @Test @Parameters({ "sUsername".sendKeys(sPassword). TimeUnit.selenium.xpath(".openqa.Test.demoqa. driver.TimeUnit.firefox.findElement(By.util.sendKeys(sUsername). driver. import org.click().xpath(". public class TestngParameters { private static WebDriver driver. import java.openqa. driver. import org.findElement(By.selenium.store.--------------------package automationFramework.TestngParameters" /> .WebDriver. "sPassword" }) public void test(String sUsername.testng.id("login")).findElement(By.timeouts(). } } <suite name="Suite"> <test name="ToolsQA"> <parameter name="sUsername" value="testuser_1"/> <parameter name="sPassword" value="Test@123"/> <classes> <class name="automationFramework.id("pwd")).openqa. driver.click().Parameters.id("log")).annotations.quit().FirefoxDriver.findElement(By.click(). import org.concurrent.

driver.firefox. driver.click(). driver. import java.id("login")).annotations.findElement(By. import org.findElement(By.get("http://www. driver.demoqa.testng.concurrent. import org.//*[@id='account']/a")).openqa. import org.testng.click(). public class DataProviderTest { private static WebDriver driver. import org. @DataProvider(name = "Authentication") public static Object[][] credentials() { return new Object[][] { { "testuser_1".util.DataProvider.TimeUnit.selenium.quit(). "T est@123" }}.store.id("log")). driver.click().//*[@id='account_logout']/a")).com"). TimeUnit.xpath(". driver.annotations.SECONDS).findElement(By.id("pwd")).sendKeys(sUsername).</classes> </test> </suite> ------------package automationFramework.Test. driver. } // Here we are calling the Data Provider object with its Name @Test(dataProvider = "Authentication") public void test(String sUsername. "Test@123" }.sendKeys(sPassword). import org.openqa.By. String sPassword) { driver = new FirefoxDriver().openqa.manage(). driver.findElement(By.selenium.timeouts().WebDriver.selenium. { "testuser_1". } .xpath(".implicitlyWait(10.FirefoxDriver.findElement(By.

} .