Wednesday, February 18, 2015

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");
    }
}

Tuesday, February 17, 2015

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());
    }
}

Thursday, February 12, 2015

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();

    }

}