Monday 10 November 2014

Setting up TestNG with Eclipse

Installation of TestNG plug-in with Eclipse:
There is no need to download any Jar file or exe file for installation.  We need to just utilize the “Install New Software” option available in the Eclipse.

Installation steps:
1. Click Help –> Install New Software



2. Type “http://beust.com/eclipse” in the “Work with” edit box and click ‘Add’ button
3. In the ‘Name’ column we can see “TestNG” –> Select this and click ‘Next’ button

4.  Click Next and click on the radio button “I accept the terms of the license agreement”
5. Click ‘Next’ button
6. Click ‘Finish’

This will install the TestNG plug-in for Eclipse.

After the installation, it will ask for restart of Eclipse.  Then restart the Eclipse.

Once the Eclipse is restarted, we can see the TestNG icons and menu items as in the below figures.



Monday 3 November 2014

Count a page load time with selenium webdriver

Suppose their is a situation to check time taken for a web page to load. e.g., Check login time into gmail.
import java.awt.AWTException;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TrackWebtime {
 public static void main(String[] args) throws IOException, AWTException{

 WebDriver driver =new FirefoxDriver();
 driver.get("http://www.gmail.com");
 
 long start = System.currentTimeMillis();

 driver.findElement(By.id("Email")).clear();
 driver.findElement(By.id("Email")).sendKeys("username");
 driver.findElement(By.id("Passwd")).clear();
 driver.findElement(By.id("Passwd")).sendKeys("password");

 Robot robot = new Robot();
 robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);

 long finish = System.currentTimeMillis();
 
 long TotalTime = finish - start;

 System.out.println("Total Time for page load - "+TotalTime);

 }
}


Multi Selection

List listItems = driver.findElements("find your select by id or xpath or etc");
Actions builder = new Actions(driver);
builder.clickAndHold(listItems.get(1)).clickAndHold(listItems.get(2)).click();
Action selectMultiple = builder.build();
selectMultiple.perform();

Open a link in a New tab

We use actions objects to open any hyperlink in a new tab. e.g.,

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;

public class NewTab {
    public static void main(String[] args) {
       WebDriver driver = new FirefoxDriver();
        driver.get("http://www.flipcart.com/");
        driver.manage().window().maximize();
        Actions act = new Actions(driver);
        WebElement link = driver.findElement(By.linkText("Flipkart First"));
        act.moveToElement(link).contextClick().sendKeys("F").perform();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
         driver.close();
     
    }
}


Enter a text without using SendKeys in Selenium Webdriver

We can enter a text in text box using SendKeys, but we can also do this in another ways as well.

public class type{

public static void setattribute(WebElement element,String attributename,String Value){

WrapsDriver wrappedElement=(WrapsDriver)element;
JavascriptExecuter driver =(JavaScriptExecutor)wrappedElement.getWrapedDriver;
driver.executeScript(arguments[0].setAttribute(arguments[1],arguments[2]",element,
                                    attributeName,value);
}

//or

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('email').value = 'sunilrathore77@gmail.com';");

Sunday 2 November 2014

Challenges faced while working on Selenium

There are many challenges we face while working on selenium. Some of the challenges are:

1. configuration challenges on different machines
2. selenium's compatibility with browser versions like with FF20 is not supported with some web-driver versions.
3. handling IE/chrome etc.
4. popup handling
5. Ajax handling
6. security certificate handling
7. iframes
8. date pick from calender.
9. identifying locators
10. Upload and download file
11.
Maintain the scripts to work properly irrespective of new build 
12. Diff to identify script has failed may be unable to locate the element or config issue(Browser updated) if your framework is not able to catch issue.
13. If you are using xpath as a locator. writing xpath without using dynamic changing data. i.e., xpath generated dynamically.

TestNG Annotations



@BeforeSuite: The annotate method will be run before all tests in this suite have run.
@AfterSuite: The annotated method will be run after all tests in this suite have run.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeMethod: The annotated method will be run before each test method.
@Test: The annotated method is a part of a test case.
@AfterMethod: The annotated method will be run after each test method.
@DataProvider: The annotated method provides data to the @Test methods. The annotated method returns an Object [][].
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

Example:
package testng;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGAnnotations {

 @BeforeSuite
 public void instantiate() {
  System.out.println("Instantiate Object");
 }

 @BeforeTest
 public void dataBaseConnection() {
  System.out.println("Database Connected");
 }

 @BeforeMethod
 public void BeforeMethod() {
  System.out.println("Run before each Test Case.");
 }

 @Test
 public void testCase1() {
  System.out.println("First Test Case Result..... ");
 }
 
 @Test
 public void testCase2() {
  System.out.println("Second Test Case Result.... ");
 }

 @AfterMethod
 public void AfterMethod() {
  System.out.println("Run after each Test Case");
 }

 @AfterTest
 public void dataBaseDisconnection() {
  System.out.println("Database Disconnected");
 }

 @AfterSuite
 public void destory() {
  System.out.println("Destory Object");
 }
}

Create testng.xml:

testng.xml: Right click on Project --> TestNG --> Convert to TestNG.




  
    
      
    
   
 
Execute TestNG:
Right click on testng.xml --> Run As --> TestNG Suite OR Right click on java file --> Run As --> TestNG Test

Output: