Friday 12 July 2013

WebDriver TestNG example


public class GmailTestCases {

  @BeforeMethod
public void launch() {
driver= new FirefoxDriver();
  driver.get("http://gmail.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@AfterMethod
public void quit() {
driver.quit();
}


@Test
public void verifyLoginPage() {

String actual_title = driver.getTitle();
String expected_title = "Gmail: Email from Google";
Assert.assertEquals(expected_title, actual_title);
}


@Test
public void verifyLoginWithValidCredentials() throws Exception
{
driver.findElement(By.id("Email")).sendKeys("test@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("validpassword");
driver.findElement(By.id("signIn")).click();
Thread.sleep(10000);
String expected_username = "Test user";
String actual_username   = driver.findElement(By.id("gbi4t")).getText();
Assert.assertEquals(expected_username, actual_username);
}



@Test
public void verifyLoginWithInValidCredentials() throws InterruptedException {
driver.findElement(By.id("Email")).sendKeys("test@gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("invalidpwd");
driver.findElement(By.id("signIn")).click();
Thread.sleep(10000);
                Assert.assertTrue(driver.getPageSource().contains("The username or password you entered is incorrect"));
}

@Test
public void verifyLinks() throws InterruptedException {

List <WebElement> alllinks = driver.findElements(By.tagName("a"));

for(int i=0;i<alllinks.size();i++) {
System.out.println(alllinks.get(i).getText());
}

for(int i=0;i<alllinks.size();i++){

alllinks = driver.findElements(By.tagName("a"));
alllinks.get(i).click();
Thread.sleep(1000);
driver.navigate().to("http://gmail.com");
Thread.sleep(1000);
}
}

================================================================

XML file

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <!-- Specify the test suite name --> <suite name="Gmail_Login" verbose="1" annotations="JDK"> <!-- Specify the test name --> <test name="gmail_Login_Scenarios" preserve-order="true"> <classes> <class name="packagename.GmailTestCases"> <methods> <include name="verifyLoginPage" /> <include name="verifyLoginWithValidCredentials" /> <include name="verifyLoginWithInValidCredentials" /> <include name="verifyLinks" /> </methods> </class> </classes> </test> </suite>




3 comments:


  1. Best place to learn Appium Android IOS Automation Testing Training in Chennai

    Go for Appium training in chennai for real time mobile automation testing training with real time project support.
    Mr. Vishwa is a best person who can teach and develop people in Appium and Selenium,Coded UI Automation Tools with real time scenarios You can contact 8122241286 for Best APPIUM and selenium Training in Chennai

    ReplyDelete
  2. Appium Training in chennai
    We provides Best Appium Training in Chennai with real time project assistance by our leading Appium mobile App Automation testing architect
    For Free Live Demo @ Call to 8122241286.
    Appium ,Selenium,Mobile Automation Testiing using Android and IOS
    www.thecreatingexperts.com
    Appium training in chennai

    ReplyDelete