Selenium Webdriver sample scripts using java
Thursday, July 7, 2016
Sample webdriver scripts to Navigate url
Two
methods:
Method
1:
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
public
class goTo {
public
static void main(String[] args) {
//
TODO Auto-generated method stub
WebDriver
driver = new ChromeDriver();
driver.get("https://www.google.com");
}
}
Method
2:
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
public
class goTo {
public
static void main(String[] args) {
//
TODO Auto-generated method stub
WebDriver
driver = new ChromeDriver();
driver.navigate().to("https://www.google.com");
}
}
Differences
between navigate().to() and get()
Webdriver
will wait until the page has fully loaded before returning the
control to test or script. If there many ajax calls in the current
page which webdriver is loading then webdriver may not know when it
has loaded completely. If you need to make sure such pages are fully
loaded then you can use waits.
Earlier,
we covered navigating to a page using the get command
(driver.get("http://www.example.com"))
As you have seen, webdriver has a number of smaller, task-focused
interfaces, and navigation is a useful task. Because loading a page
is such a fundamental requirement, the method to do this lives on the
main Webdriver interface, but it’s simply a synonym to:
Wednesday, July 29, 2015
Example scripts for Minimise all the application which is running in foreground using java robot class
import java.awt.AWTException;
import java.awt.Robot;
public class MinimizeAllAppln {
public static void main(String[] args) throws AWTException, InterruptedException {
// TODO Auto-generated method stub
for(int i=1;;i++)
{
Robot robot=new Robot();
robot.mouseMove(0, i);
}
}
}
import java.awt.Robot;
public class MinimizeAllAppln {
public static void main(String[] args) throws AWTException, InterruptedException {
// TODO Auto-generated method stub
for(int i=1;;i++)
{
Robot robot=new Robot();
robot.mouseMove(0, i);
}
}
}
Wednesday, August 13, 2014
Example selenium webdriver code to click button/link/any kind of element using java
Example 1:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); // Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // send 'test' value in google search textbox
driver.findElement(By.name("btnG")).click(); // click on google search button
}
}
Example 2: Using mouse action class in selenium
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); // Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // send 'test' value in google search textbox
Actions action = new Actions(driver);
action.click(driver.findElement(By.name("btnG"))).build().perform(); // click on google search button
}
}
Example 3: Using JavascriptExecutor class in selenium
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.JavascriptExecutor;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); // Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // send 'test' value in google search textbox
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.name("btnG"))); // click on google search button
}
}
Tuesday, August 12, 2014
Example selenium webdriver code to enter values in textfield/textbox using java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); //Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // enter 'test' value in google search textbox
}
}
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver(); // launch the firefox browser
driver.get("http://www.google.com"); //Navigate to google.com
driver.findElement(By.name("q")).sendKeys("test"); // enter 'test' value in google search textbox
}
}
Subscribe to:
Posts (Atom)