Implicit Wait Example

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import java.util.concurrent.TimeUnit;

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

public class ImplicitWaitTime {

    public static void main(String... args) {

        WebDriver driver = new FirefoxDriver();

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

        driver.get("www.google.com");
    }
}

Global Wait Example

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GlobalWait {

    @Test
    public void testglobalWait() {

        //
        System.out.println("Hello!!! how are you?");

        //
        WebDriver webDriver = new FirefoxDriver();

        //
        webDriver.get("http://not-just-a-tester.blogspot.in/");

        // Wait for elements that haven't appeared yet
        webDriver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);

        //
        System.out.println("I am good!!!");

    }

}

Get Text of Web Element

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GetText {

    //
    WebDriver webDriver = new FirefoxDriver();

    @Test
    public void testgetText() {

        //
        webDriver.get("http://www.google.com");

        //
        WebElement searchButton = webDriver.findElement(By.name("btnK"));

        //
        System.out.println(searchButton.getText());
    }
}

Get Tag Name of Web Element

/*
 * 
 * @Author : Gaurav Khanna
 *  
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GetTagName {

    //
    WebDriver webDriver = new FirefoxDriver();

    @Test

    public void testgetTagName() {

        //
        webDriver.get("http://www.google.com");

        //
        WebElement searchButton = webDriver.findElement(By.name("btnK"));

        //
        System.out.println(searchButton.getTagName());
   }
}

Fetching different attributes of Web Element

/*
 * 
 * @Author : Gaurav Khanna
 *  
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GetAttributeMethod {

    // Declaring variable 'webDriver' of WebDriver Type
    WebDriver webDriver;

    // Declaring baseURL variable of String Type
    String baseUrl;

    // Declaring attributeValue variable of String Type
    String attributeValue;

    @Test
    public void testgetAttributeMethod() {

        // Initializing FireFox Driver
        webDriver = new FirefoxDriver();


        // Assigning URL to variable 'baseUrl'
        baseUrl = "http://book.theautomatedtester.co.uk/chapter1";


        // Open the link
        webDriver.get(baseUrl);

        // Locate Button and Store the value of button to 'attributeValue'
        // variable

        attributeValue = webDriver.findElement(By.id("verifybutton"))
                .getAttribute("value");

        // Printing value of 'attributeValue'
        System.out.println("Attribute Value : " + attributeValue);

        // This will close the browser
        webDriver.quit();
    }
}

Get Attribute Method

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GetAttributes {

    WebDriver webDriver = new FirefoxDriver();

    @Test
    public void testgetAttributes() {

        //
        webDriver.get("http://www.google.com");

        //
        WebElement searchButton = webDriver.findElement(By.name("btnK"));

        //
        System.out.println("Name of the button is: "
                + searchButton.getAttribute("name"));

        //
        System.out.println("Id of the button is: "
                + searchButton.getAttribute("id"));

        //
        System.out.println("Class of the button is: "
                + searchButton.getAttribute("class"));

        //
        System.out.println("Label of the button is: "
                + searchButton.getAttribute("aria-  label"));

    }

}

Get Location of Web Element

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GetLocation {

    WebDriver webDriver = new FirefoxDriver();

    @Test
    public void testgetLocation() {

        //
        webDriver.get("http://www.google.com");

        //
        WebElement searchButton = webDriver.findElement(By.name("btnK"));

        //
        System.out.println(searchButton.getLocation());

    }

}

Get Size of Web Element

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class GetSize {

    //
    WebDriver webDriver = new FirefoxDriver();

    @Test
    public void testgetSize() {

        //
        webDriver.get("http://www.google.com");

        //
        WebElement searchButton = webDriver.findElement(By.name("btnK"));

        //
        System.out.println(searchButton.getSize());

    }

}

Forward And Back Navigations

/*
 *
 * @Author : Gaurav Khanna
 * 
 */

package webdriverScripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class ForwardAndBack {

    // Declaring variable 'webDriver' of WebDriver Type
    WebDriver webDriver;

    // Declaring baseURL variable of String Type
    String baseUrl;

    @Test
    public void testforwardAndBack() throws InterruptedException {

        // Initializing FireFox Driver
        webDriver = new FirefoxDriver();

        // Assigning URL to variable 'baseUrl'
        baseUrl = "http://book.theautomatedtester.co.uk";

        // Open the link
        webDriver.get("http://book.theautomatedtester.co.uk");

        // Maximize browser window
        webDriver.manage().window().maximize();

        // Click on link
        webDriver.findElement(By.linkText("Chapter1")).click();

        System.out.println(webDriver.getTitle());

        Thread.sleep(5000L);

        // Using Back Method
        webDriver.navigate().back();

        //
        System.out.println(webDriver.getTitle());

        Thread.sleep(5000L);

        // Using Forward Method
        webDriver.navigate().forward();

        //
        System.out.println(webDriver.getTitle());

        //
        Thread.sleep(5000L);

        // This will close the browser
        webDriver.quit();

    }
}

Uploading File

package webdriverScripts.others;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class FileUpload {

 // Declaring variable 'webDriver' of WebDriver Type
 WebDriver webDriver;

 // Declaring variable 'baseUrl' of String Type
 String baseUrl;

 @Test

 public void FileUploadExample() throws InterruptedException {

  // Initializing FireFox Driver
  webDriver = new FirefoxDriver();

  // Assigning URL to variable 'baseUrl'
  baseUrl = "http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_FILE.html";

  // Open the link
  webDriver.get(baseUrl);

  // Maximize browser window
  webDriver.manage().window().maximize();

  //
  String fileForUpload = System.getProperty("user.dir") + "\\html\\line-shape.jpg";

  //
  webDriver.findElement(By.name("upfile")).sendKeys(fileForUpload);

  //
  webDriver.findElement(By.xpath("//input[@value='Submit']")).click();

  //
  Thread.sleep(6000);

  // This will close the browser
  webDriver.quit();

 }
}

Extracting Links from Web Page

/*
 * @Author : Gaurav Khanna
 */

package webdriverScripts;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class ExtractingLinks {

    //
    WebDriver webDriver = new FirefoxDriver();

    @Test
    public void testextractingLinks() {

        //
        webDriver.get("http://not-just-a-tester.blogspot.in/");

        //
        List<WebElement> allLinks = webDriver.findElements(By.tagName("a"));

        //
        System.out.println(allLinks.size());

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

            //
            System.out.println(allLinks.get(i).getText());
        }

        //
        System.out.println("**********************************");

    }

}