Friday, 17 July 2015

SELENIUM TESTNG & SELENIUM WEBDRIVER TESTNG FRAME WORK WITH NORMAL,WEB APPLICATION EXAMPLE


TestNG(Next Generation): it is software which provides some facilities through annotations which make the testing job easier as it is making some(testing) job more easy people generally call it as TestNG Framework.
Navigation for downloading and installing TestNG into eclipse:
Type install TestNG click on search
Click on first URL then copy the url “for eclipse 3.4 and above”
Open eclipse then click on help menu item
Select the option Install New Software
Paste the URL at Work With field then select the checkbox TestNG .
Click on next….next
Click on finish.
Note: after pasting the URL if you are not getting TestNG then follow the below steps
Click on Add then in the Name field type TestNG and in the location field paste the URL then click on Ok.
Now TestNG check box is available then click on next…next
Click on finish.
Configuring the TestNG into eclipse:
Right click on project select Properties.
Click on Java Build Path
Click on Add Library
Click on TestNG then click on Next
Click in finish.
Note: if the project is newly created then configure the selenium web driver also to your project, the process of configuring selenium is mentioned in the above pages.
package pack;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class testng {
          @BeforeClass //these are annotations availble in TestNG frame work
          public void invokeapplicationtest() {
                   System.out.println("application invoking logic is here");
          }
          @AfterClass
          public void closeapplicationtest() {
                   System.out.println("application closing logic is here");
          }
          @BeforeMethod
          public void login() {
                   System.out.println("logic for login is here");
          }
          @AfterMethod
          public void logout()        {
                   System.out.println("logic for logout is here...");
          }
          @Test
          public void withdraw()    {
                   System.out.println("logic for withdraw is here....");
          }
          @Test //test case
          public void balanceenqurytest()        {
                   System.out.println("logic for balance enqury is here....");
          }         }

FACEBOOK
public class testngfacebook {

          static WebDriver wd;
         
         
         
          @BeforeClass
          public void invoke()
          {
                   wd = new FirefoxDriver();
                  
          }
          @AfterClass
          public void close()
          {
                   wd.close();
          }
         
          @BeforeMethod
          public void login() throws InterruptedException
          {
                  
                   wd.get("https://www.facebook.com/");
                    wd.findElement(By.xpath(".//*[@id='email']")).sendKeys("akhilreddyhyd@gmail.com");
                   wd.findElement(By.xpath(".//*[@id='pass']")).sendKeys("sachin143");
                   wd.findElement(By.xpath(".//*[@id='u_0_n']")).click();
                   Thread.sleep(5000l);
          }
         
          @AfterMethod
          public void logout()
          {
                   wd.findElement(By.xpath(".//*[@id='userNavigationLabel']")).click();
                   wd.findElement(By.xpath(".//*[@id='logout_form']/label")).click();
          }
         
         
@Test
public void manish()
{
          wd.findElement(By.xpath(".//*[@id='blueBarNAXAnchor']/div[1]/div/div/div[1]/ul/li[1]/a/span")).click();
         
          }
         
          @Test
          public void home()
          {
                  
                   wd.findElement(By.xpath(".//*[@id='u_0_g']/a")).click();
                  
          }

}


Redbus with property file


WebDriver wd;
   
    @BeforeClass
    public void invoke()
    {
          wd= new FirefoxDriver();
        
    }
   
   
   
   
    @AfterClass
    public void close()
    {
         wd.close();
    }
   
    @BeforeMethod
    public void login() throws InterruptedException, IOException
    {
         Properties p=new Properties();
         FileInputStream fis=new FileInputStream("C:\\Documents and Settings\\akki\\workspaceaaaa\\frame\\src\\akki");
         p.load(fis);

         wd.get("http:redbus.in");
         wd.findElement(By.xpath(".//*[@id='signInLink']")).click();

        wd.findElement(By.id(p.getProperty("a"))).sendKeys("yaminireddy24041982@gmail.com");
        wd.findElement(By.id(p.getProperty("b"))).sendKeys("yamini");
         wd.findElement(By.id(p.getProperty("c"))).click();
         Thread.sleep(5000l);

        
    }
        
         @AfterMethod
         public void logout()
         {
        
             wd.findElement(By.xpath(".//*[@id='signOutLink']")).click();
            
            
            
         }  
            
        
    @Test
    public void buses()
    {
        wd.findElement(By.xpath(".//*[@id='wrapper1']/header/div[2]/div/div[1]/a[1]")).click();
        
        wd.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("hyderabad");
        wd.findElement(By.xpath(".//*[@id='txtDestination']")).sendKeys("kadapa");
        wd.findElement(By.xpath(".//*[@id='txtOnwardCalendar']")).click();
        wd.findElement(By.xpath(".//*[@id='rbcal_txtOnwardCalendar']/table[1]/tbody/tr[6]/td[6]")).click();
        
        wd.findElement(By.xpath(".//*[@id='txtReturnCalendar']")).click();
        wd.findElement(By.xpath(".//*[@id='rbcal_txtReturnCalendar']/table[2]/tbody/tr[7]/td[4]")).click();
        
        
         wd.findElement(By.xpath(".//*[@id='searchBtn']")).click();
    }
   
        
         @Test
         public void hotels()
         {
            
             wd.findElement(By.xpath(".//*[@id='ctl00_hotelDiv']")).click();
        
             wd.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("mumbai");
            
                
             wd.findElement(By.xpath(".//*[@id='txtOnwardCalendar']")).click();
            
             wd.findElement(By.xpath(".//*[@id='rbcal_txtOnwardCalendar']/table[1]/tbody/tr[6]/td[6]")).click();
            

             wd.findElement(By.xpath(".//*[@id='txtReturnCalendar']")).click();
            
             wd.findElement(By.xpath(".//*[@id='rbcal_txtReturnCalendar']/table[1]/tbody/tr[7]/td[2]")).click();
            

             wd.findElement(By.xpath(".//*[@id='searchBtn']")).click();
            
         }
        




}




REDBUS

static WebDriver wd;
         
         
          @BeforeClass
          public void invoke()
          {
                   wd = new FirefoxDriver();
                  
          }
         
         
         
         
          @AfterClass
          public void close()
          {
                   wd.close();
          }
         
          @BeforeMethod
          public void login() throws InterruptedException
          {

                   wd.get("http:redbus.in");
                   wd.findElement(By.xpath(".//*[@id='signInLink']")).click();

                    wd.findElement(By.xpath(".//*[@id='username']")).sendKeys("yaminireddy24041982@gmail.com");
                   wd.findElement(By.xpath(".//*[@id='password']")).sendKeys("yamini");
                   wd.findElement(By.xpath(".//*[@id='signInBtn']")).click();
                   Thread.sleep(5000l);

                  
          }
                  
                   @AfterMethod
                   public void logout()
                   {
                  
                             wd.findElement(By.xpath(".//*[@id='signOutLink']")).click();
                            
                            
                            
                   }        
                            
                  
          @Test
          public void buses()
          {
                    wd.findElement(By.xpath(".//*[@id='wrapper1']/header/div[2]/div/div[1]/a[1]")).click();
                  
                   wd.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("hyderabad");
                   wd.findElement(By.xpath(".//*[@id='txtDestination']")).sendKeys("kadapa");
                   wd.findElement(By.xpath(".//*[@id='txtOnwardCalendar']")).click();
                    wd.findElement(By.xpath(".//*[@id='rbcal_txtOnwardCalendar']/table[1]/tbody/tr[6]/td[6]")).click();
                  
                   wd.findElement(By.xpath(".//*[@id='txtReturnCalendar']")).click();
                    wd.findElement(By.xpath(".//*[@id='rbcal_txtReturnCalendar']/table[2]/tbody/tr[7]/td[4]")).click();
                  
                  
                   wd.findElement(By.xpath(".//*[@id='searchBtn']")).click();
          }
         
                  
                   @Test
                   public void hotels()
                   {
                            
                             wd.findElement(By.xpath(".//*[@id='wrapper1']/header/div[2]/div/div[1]/a[2]/span")).click();
                  
                             wd.findElement(By.xpath(".//*[@id='txtSource']")).sendKeys("mumbai");
                            
                                     
                             wd.findElement(By.xpath(".//*[@id='txtOnwardCalendar']")).click();
                            
                             wd.findElement(By.xpath(".//*[@id='rbcal_txtOnwardCalendar']/table[1]/tbody/tr[6]/td[6]")).click();
                            

                             wd.findElement(By.xpath(".//*[@id='txtReturnCalendar']")).click();
                            
                             wd.findElement(By.xpath(".//*[@id='rbcal_txtReturnCalendar']/table[1]/tbody/tr[7]/td[2]")).click();
                            

                             wd.findElement(By.xpath(".//*[@id='searchBtn']")).click();
                            
                   }
                  


}

REDIAL.IN


         
         
          @BeforeClass
          public void invoke()
          {
                   wd = new FirefoxDriver();
                  
          }
         
          @AfterClass
          public void close()
          {
                   wd.close();
          }
         
          @BeforeMethod
          public void login()
          {
                   wd.get("http:dial.redial.in");
          wd.findElement(By.xpath(".//*[@id='warp']/div[2]/div/div/form/ol/li[1]/input")).sendKeys("SSSS");
          wd.findElement(By.xpath(".//*[@id='warp']/div[2]/div/div/form/ol/li[2]/input")).sendKeys("SSSSSSSS");
          wd.findElement(By.xpath(".//*[@id='warp']/div[2]/div/div/form/ol/li[3]/input[2]")).click();
}
          @AfterMethod
          public void logout()
          {
                   wd.findElement(By.xpath(".//*[@id='warp']/span/a")).click();
          }
         
          @Test
          public void sshistory() throws InterruptedException
          {
                   wd.findElement(By.xpath("html/body/div[1]/div[2]/ul/a[7]/li")).click();
                   wd.findElement(By.xpath(".//*[@id='form']/ol/li[2]/input")).sendKeys("Shaik Ahmed");
                   wd.findElement(By.xpath(".//*[@id='form']/ol/li[6]/a/input")).click();
                   Thread.sleep(4000l);
          }
          @Test
          public void msms()
          {
                   wd.findElement(By.xpath("html/body/div[2]/div[2]/ul/a[2]/li")).click();
                    wd.findElement(By.xpath(".//*[@id='warp']/div[4]/div[2]/table/tbody/tr[7]/td[3]/input")).click();
          }
Note:
 We no need to have main() method in our class because TestNG is going to take care about it.
It is new and famous framework having more no of annotations.
Order of Execution is based on the annotations but not in the order in which we have written the code.
More powerful reports will be generated compared to JUnit.
JUnit and TestNG both are almost same difference is at annotations.
In JUnit @BeforeClass and @AfterClass annotation’s methods must be static but in TestNG no need be static methods.
In JUnit @Before and @After annotations are available but in TestNG we have @BeforeMethod and @AfterMethod
Ignoring or skipping test cases while executing:
In JUnit: we need to write @Ignore before @Test to skip that particular test.
E g.:
 @Ignore
          @Test //test case
          public void balanceenqurytest()        {
          System.out.println("logic for balance enqury is here....");
          }        
In TestNG: to skip any test, in TestNG we need to write (enabled=false) along with @Test
E g.:
@Test(enabled=false)
          public void withdraw()    {
          System.out.println("logic for withdraw is here....");
          }


No comments:

Post a Comment