Basic functionalists of selenium webdriver
package webdriver;
import java.net.URL;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class Webdriver_2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver d=null;
try
{
Capabilities c=DesiredCapabilities.firefox();
d = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), c);
d.get("http://localhost:8080/saome/webdriver/popup.jsp");
/**
* Switches to the element that currently has focus, or the body element if this cannot be detected.
*/
WebElement wb= d.switchTo().activeElement();
wb.click();
Thread.sleep(5000);
/**
*Selects either the first frame on the page, or the main document when a page contains iframes.
*/
WebDriver frmae=d.switchTo().defaultContent();
/**
*
* Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later date
*/
String windowHandle= d.getWindowHandle();
d.get("http://localhost:8080/saome/webdriver/frame.jsp");
/**
* Select a frame by its name, id or (zero-based) index. To select sub-frames, simply separate the frame names/IDs/indexes by dots. As an example "main.child" will select the frame with the name "main" and then it's child "child". If the given string represents an integer number, then it will be used to select a frame by its (zero-based) index.
*/
WebDriver frame= d.switchTo().frame("f1");
frame.findElement(By.name("id1")).clear();
d.switchTo().window(windowHandle);
frame= d.switchTo().frame("f2");
frame.findElement(By.name("id2")).clear();
d.switchTo().window(windowHandle);
d.switchTo().frame(d.findElement(By.name("f1")));
frame.findElement(By.name("id1")).sendKeys("guruashant");
d.close();
/**
* Following code handles alert,confirm and prompt.
*/
Capabilities c=DesiredCapabilities.firefox();
d = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), c);
d.get("http://localhost:8080/saome/prompt.jsp");
d.findElement(By.name("id1")).click();
Alert a= d.switchTo().alert();
System.out.println("Text="+a.getText());
a.sendKeys("gurushant");
a.accept();
d.findElement(By.name("id2")).sendKeys("");
d.findElement(By.name("id1")).click();
a= d.switchTo().alert();
System.out.println("Text="+a.getText());
a.sendKeys("gurushant");
a.dismiss();
d.close();
d=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub") ,DesiredCapabilities.firefox());
d.get("http://localhost:8080/saome/sample.jsp");
JavascriptExecutor js=(JavascriptExecutor)d;
/**
* Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.
*/
Object str=js.executeScript("return document.title");
System.out.println("Title=="+str);
d.get("http://localhost:8080/saome/webdriver/popup.jsp");
WebElement ele= d.findElement(By.name("id1"));
ele.click();
Iterator<String> newhandler=d.getWindowHandles().iterator();
String parent=null,child=null;
if(newhandler.hasNext())
{
parent=newhandler.next();
child=newhandler.next();
}
WebDriver myd= d.switchTo().window(child); //here you can also specify window name.
myd.findElement(By.name("q")).sendKeys("auto");
d.switchTo().window(parent);
d.quit();//closes all open window and unregisters to the hub
/**
* Following code shows how to automate dropdown in webdriver
*/
d=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub") ,DesiredCapabilities.firefox());
d.get("http://localhost:8080/saome/multiselection.jsp");
Select ss=new Select(d.findElement(By.id("id1")));
ss.selectByIndex(1);
ss.selectByVisibleText("Dev");
ss.selectByValue("auto");
List<WebElement> options= ss.getAllSelectedOptions();
ss.deselectByIndex(1);
ss.deselectByValue("auto");
ss.deselectByVisibleText("Dev");
ss.selectByVisibleText("Dev");
ss.deselectAll();
} catch (Exception e1) {
// TODO Auto-generated catch block
d.close(); //closes current window and unregisters to the hub
e1.printStackTrace();
}
}
}
No comments:
Post a Comment