Some of the basic functionalists of webdriver.
package webdriver;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import javax.tools.Tool;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class WebDriverFun {
/**
* Webdriver basic functionalities
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Capabilities c=DesiredCapabilities.firefox();
WebDriver d=null;
try {
d = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), c);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
d.get("http://localhost:8080/saome/sample.jsp");
WebElement wb= d.findElement(By.name("q1"));
/*
* Clears the value of the element.
No parameters.
Return type: void
*/
wb.clear();
wb.sendKeys("This is selenium automation");
/**
* Gets attribute of an element.
*/
String attrib=wb.getAttribute("id");
System.out.println("Attribute=="+attrib);
/**
* Gets the value of a CSS property of this element.
*/
String cssValue=wb.getCssValue("background-color");
System.out.println("CSS Value="+cssValue);
/**
* It will return the "Point" object.
If you want to get the exact "x" and "y" coordinates of the element then use "getLocation()" method.
*/
Point p= wb.getLocation();
System.out.println("X="+p.x+"\t Y="+p.y);
/**
* It will returns the "Dimension" object.
If you want to get the width and Height of the specific element on the webpage then use "getsize()" method.
It returns including +6 . Suppose you have ,<input id="id4" name="id4" style="width: 200px; height: 50px;" />
It returns 206 widht , height is 56
*/
wb= d.findElement(By.name("id4"));
Dimension dim= wb.getSize();
System.out.println("Width="+dim.width+"\t Height="+dim.height);
/**
* Get the tag name of this element. Not the value of the name attribute: will return "input" for the element <input name="foo" />.
*/
String tagName= wb.getTagName();
System.out.println("Tag Name="+tagName);
/**
* Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute.
*/
wb= d.findElement(By.name("hid1"));
boolean isDisplayed= wb.isDisplayed();
System.out.println("is Displayed="+isDisplayed);
/**
* Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
*/
wb= d.findElement(By.name("q"));
boolean isEnabled= wb.isEnabled();
System.out.println("is Enabled="+ isEnabled);
/**
* Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons.
*/
wb= d.findElement(By.name("ch1"));
boolean isSelected= wb.isSelected();
System.out.println("is Selected="+ isSelected);
/**
* Get a string representing the current URL that the browser is looking at.
*/
String currentUrl= d.getCurrentUrl();
System.out.println("Current URL="+currentUrl);
/**
* Get the source of the last loaded page.
*/
String pageSource= d.getPageSource();
System.out.println("Page Source="+pageSource);
/**
* The title of the current page.
*/
String title=d.getTitle();
System.out.println("Title="+title);
/**
* Return an opaque handle to this window that uniquely identifies it within this driver instance.
*/
String windowHandle=d.getWindowHandle();
System.out.println("Window Handle="+windowHandle);
/**
* Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
*/
Options op= d.manage();
op.timeouts().implicitlyWait(10, TimeUnit.SECONDS);
/**
* Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
*/
op.timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
/**
* Using navigation you can move forward,backward.
*/
Navigation nv= d.navigate();
nv.to("http://localhost:8080/saome/sample2.jsp");
nv.back();
nv.forward();
nv.back();
/**
* Maximizes the browser window
*/
d.manage().window().maximize();
/**
* Shows position of browser window
*/
p= d.manage().window().getPosition();
System.out.println("Window Postion X="+p.x+"\t Width="+p.y);
/**
* Sets the browser location
*/
d.manage().window().setPosition(new Point(100, 100));
/**
* It sets size of browser window
*/
d.manage().window().setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height));
d.close();
}
}
package webdriver;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import javax.tools.Tool;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class WebDriverFun {
/**
* Webdriver basic functionalities
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Capabilities c=DesiredCapabilities.firefox();
WebDriver d=null;
try {
d = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), c);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
d.get("http://localhost:8080/saome/sample.jsp");
WebElement wb= d.findElement(By.name("q1"));
/*
* Clears the value of the element.
No parameters.
Return type: void
*/
wb.clear();
wb.sendKeys("This is selenium automation");
/**
* Gets attribute of an element.
*/
String attrib=wb.getAttribute("id");
System.out.println("Attribute=="+attrib);
/**
* Gets the value of a CSS property of this element.
*/
String cssValue=wb.getCssValue("background-color");
System.out.println("CSS Value="+cssValue);
/**
* It will return the "Point" object.
If you want to get the exact "x" and "y" coordinates of the element then use "getLocation()" method.
*/
Point p= wb.getLocation();
System.out.println("X="+p.x+"\t Y="+p.y);
/**
* It will returns the "Dimension" object.
If you want to get the width and Height of the specific element on the webpage then use "getsize()" method.
It returns including +6 . Suppose you have ,<input id="id4" name="id4" style="width: 200px; height: 50px;" />
It returns 206 widht , height is 56
*/
wb= d.findElement(By.name("id4"));
Dimension dim= wb.getSize();
System.out.println("Width="+dim.width+"\t Height="+dim.height);
/**
* Get the tag name of this element. Not the value of the name attribute: will return "input" for the element <input name="foo" />.
*/
String tagName= wb.getTagName();
System.out.println("Tag Name="+tagName);
/**
* Is this element displayed or not? This method avoids the problem of having to parse an element's "style" attribute.
*/
wb= d.findElement(By.name("hid1"));
boolean isDisplayed= wb.isDisplayed();
System.out.println("is Displayed="+isDisplayed);
/**
* Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
*/
wb= d.findElement(By.name("q"));
boolean isEnabled= wb.isEnabled();
System.out.println("is Enabled="+ isEnabled);
/**
* Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons.
*/
wb= d.findElement(By.name("ch1"));
boolean isSelected= wb.isSelected();
System.out.println("is Selected="+ isSelected);
/**
* Get a string representing the current URL that the browser is looking at.
*/
String currentUrl= d.getCurrentUrl();
System.out.println("Current URL="+currentUrl);
/**
* Get the source of the last loaded page.
*/
String pageSource= d.getPageSource();
System.out.println("Page Source="+pageSource);
/**
* The title of the current page.
*/
String title=d.getTitle();
System.out.println("Title="+title);
/**
* Return an opaque handle to this window that uniquely identifies it within this driver instance.
*/
String windowHandle=d.getWindowHandle();
System.out.println("Window Handle="+windowHandle);
/**
* Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
*/
Options op= d.manage();
op.timeouts().implicitlyWait(10, TimeUnit.SECONDS);
/**
* Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
*/
op.timeouts().setScriptTimeout(10, TimeUnit.SECONDS);
/**
* Using navigation you can move forward,backward.
*/
Navigation nv= d.navigate();
nv.to("http://localhost:8080/saome/sample2.jsp");
nv.back();
nv.forward();
nv.back();
/**
* Maximizes the browser window
*/
d.manage().window().maximize();
/**
* Shows position of browser window
*/
p= d.manage().window().getPosition();
System.out.println("Window Postion X="+p.x+"\t Width="+p.y);
/**
* Sets the browser location
*/
d.manage().window().setPosition(new Point(100, 100));
/**
* It sets size of browser window
*/
d.manage().window().setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height));
d.close();
}
}
No comments:
Post a Comment