SELENIUM WEBDRIVER SCRIPTS
WWW.PAVANTESTINGTOOLS.COM | WWW.PAVANONLINETRAININGS.COM
,01. WebDriver Basic Commands - Example
package Examples;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class click {
@Test
public void clickmethod()
{
//1. Open the Chrome Browser
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chr
omedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2. Using Implicitly Wait Command
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Open the URL
driver.get("http://www.google.com");
//Get and store page title in to variable
String title = driver.getTitle();
System.out.print(title);
//Get current page URL
String CurrentURL = driver.getCurrentUrl();
System.out.println("My Current URL Is : "+CurrentURL);
//Get and store domain name in variable using JavaScript Executor
, JavascriptExecutor javascript = (JavascriptExecutor) driver;
String DomainUsingJS=(String)javascript.executeScript("return
document.domain");
System.out.println("My Current URL Is : "+DomainUsingJS);
// Checked for search box is enabled or not
if (driver.findElement(By.xpath("//input[@name='q']")).isEnabled())
{
System.out.println("Google search text box Is enabled.");
// Pass the Test - "WebDriver Test Successful" to search box
driver.findElement(By.xpath("//input[@name='q']")).sendKey
s("WebDriver Test successful.");
// clicking the search button
driver.findElement(By.xpath("//button[@name='btnG']")).clic
k();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
// Click the Top most link and open to New Tab
WebElement link=driver.findElement(By.xpath("//div[@id='ir
es']/ol/div/div[1]/div/h3/a"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).click(
link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
}
}
}
02. Navigating Back And Forward
, package Others;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class testsample
{
@Test
public void Navigate_forward_back() throws InterruptedException
{
//1. Open the Chrome Browser
System.setProperty("webdriver.chrome.driver",
"D:\\Softwares\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Open the URL
driver.get("http://www.google.com");
//navigate on specific software web application page or URL
driver.navigate().to("http://selenium-venkat.blogspot.com/p/index_4.html");
//To navigate back (Same as clicking on browser back button)
driver.navigate().back();
//To navigate forward (Same as clicking on browser forward button)
driver.navigate().forward();
}
}
03. Capture Screenshot
Capturing screenshot of software web application page is very easy in selenium webdriver.
As we knows, It is very basic required thing in software automation tools to capture screenshot
on test case failure or whenever required during test case execution.
In Selenium WebDriver/Selenium 2, we need to capture screenshot of web page using bellow
syntax.
WWW.PAVANTESTINGTOOLS.COM | WWW.PAVANONLINETRAININGS.COM
,01. WebDriver Basic Commands - Example
package Examples;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class click {
@Test
public void clickmethod()
{
//1. Open the Chrome Browser
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chr
omedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//2. Using Implicitly Wait Command
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Open the URL
driver.get("http://www.google.com");
//Get and store page title in to variable
String title = driver.getTitle();
System.out.print(title);
//Get current page URL
String CurrentURL = driver.getCurrentUrl();
System.out.println("My Current URL Is : "+CurrentURL);
//Get and store domain name in variable using JavaScript Executor
, JavascriptExecutor javascript = (JavascriptExecutor) driver;
String DomainUsingJS=(String)javascript.executeScript("return
document.domain");
System.out.println("My Current URL Is : "+DomainUsingJS);
// Checked for search box is enabled or not
if (driver.findElement(By.xpath("//input[@name='q']")).isEnabled())
{
System.out.println("Google search text box Is enabled.");
// Pass the Test - "WebDriver Test Successful" to search box
driver.findElement(By.xpath("//input[@name='q']")).sendKey
s("WebDriver Test successful.");
// clicking the search button
driver.findElement(By.xpath("//button[@name='btnG']")).clic
k();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
// Click the Top most link and open to New Tab
WebElement link=driver.findElement(By.xpath("//div[@id='ir
es']/ol/div/div[1]/div/h3/a"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).click(
link).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).build().perform();
driver.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
}
}
}
02. Navigating Back And Forward
, package Others;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class testsample
{
@Test
public void Navigate_forward_back() throws InterruptedException
{
//1. Open the Chrome Browser
System.setProperty("webdriver.chrome.driver",
"D:\\Softwares\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Open the URL
driver.get("http://www.google.com");
//navigate on specific software web application page or URL
driver.navigate().to("http://selenium-venkat.blogspot.com/p/index_4.html");
//To navigate back (Same as clicking on browser back button)
driver.navigate().back();
//To navigate forward (Same as clicking on browser forward button)
driver.navigate().forward();
}
}
03. Capture Screenshot
Capturing screenshot of software web application page is very easy in selenium webdriver.
As we knows, It is very basic required thing in software automation tools to capture screenshot
on test case failure or whenever required during test case execution.
In Selenium WebDriver/Selenium 2, we need to capture screenshot of web page using bellow
syntax.