Thursday, 2 July 2015

how to write your first test case using Selenium Webdriver and execute it using TestNG.

In the following post I will go step by step and explain how to write your first test case using Selenium Webdriver and execute it using TestNG.

1. Right click on the src folder ->New -> Package.







2. Provide package name something like com.stm.test and click “Finish”.




3. Right click on the newly created package – > New -> Class
.


4. Provide class name as “RegistrationTest” and click Finish.




5. Write the code given below for your first test.

The code for the first test is as follows:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class Gmaillog {

    public static void main(String[] args) {
       
       
        WebDriver driver = new FirefoxDriver();

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&hl=en");

        driver.manage().window().maximize();
         WebElement element = driver.findElement(By.name("FirstName"));
         element.sendKeys("madhavan");
       
         WebElement element1 = driver.findElement(By.name("LastName"));
         element1.sendKeys("m");
       
       
         WebElement element2 = driver.findElement(By.name("GmailAddress"));
         element2.sendKeys("yourgmailid");
       
         WebElement element3 = driver.findElement(By.name("Passwd"));
         element3.sendKeys("gmailaddress");
       
       
         WebElement element6 = driver.findElement(By.name("PasswdAgain"));
         element6.sendKeys("gmailaddress");
       
         driver.findElement(By.xpath(".//*[@id='BirthMonth']/div")).sendKeys("April");

         WebElement element4 = driver.findElement(By.name("BirthDay"));
         element4.sendKeys("24");
       
       
         WebElement element5 = driver.findElement(By.name("BirthYear"));
         element5.sendKeys("1991");
       
         driver.findElement(By.xpath(".//*[@id='Gender']/div")).sendKeys("Male");
         driver.findElement(By.xpath(".//*[@id='RecoveryPhoneNumber']")).sendKeys("your mobile number");
         driver.findElement(By.xpath(".//*[@id='RecoveryEmailAddress']")).sendKeys("emailid@gmail.com");
         driver.findElement(By.xpath(".//*[@id='SkipCaptcha']")).click();
         driver.findElement(By.xpath(".//*[@id=':h']")).click();
         driver.findElement(By.xpath(".//*[@id=':3j']/div")).click();
               
       
       
         //driver.findElement(By.("goog-menuitem-content")).sendKeys("April");

         //WebElement element7 = driver.findElement(By.id("BirthMonth"));
         //element7.click();
       
         //WebElement elem = driver.findElement(By.id("BirthMonth"));
         //new Select(elem).selectByVisibleText("April");
        // driver.findElement(By.id("link-signin")).click();
          // driver.findElement(By.id("link-signin")).click();
         // // driver.findElement(By.id("account-chooser-link")).click();
          
           //WebElement element9 = driver.findElement(By.name("EMail"));
            // element2.sendKeys("12mca");
           
            // WebElement element10 = driver.findElement(By.name("Passwd"));
            // element3.sendKeys("       ");
          
        // TODO Auto-generated method stub

    }

}








6. After finishing the test right click on the test and click on RunAs – >TestNG Test


 



7. After executing the test select the project and press F5 to refresh the project. A new folder “test-results” will get created which will show you the results for the execution. Right click on index.html->open with->web browser to see the execution report.