Wednesday, February 13, 2013
How to execute JavaScript using Selenium WebDriver
With Java Script we can access the DOM Properties. By doing so we can get the properties values of those objects.
Say i have a Div or a Span control like below:
<Div id="SampleDiv1">I am in Div</Div> or
<Span id="SampleSpan1">I am in Span</Div>
Here we have two solutions to get the value of the objects.
Solution 1: Use the getAttribute method.
This method is described in below location:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute%28java.lang.String%29
So for ex above, we can use like:
String sReturnText=driver.findElement(By.id("SampleSpan1")).getAttribute("innerHTML");
System.out.println(sReturnText);
Because in the above example we are trying to get text from the control we can directly use getText method like below:
String sReturnText=driver.findElement(By.id("SampleSpan1")).getText();
System.out.println(sReturnText);
Solution 2: We can get the DOM properties be JavaScript
There might be situations you will be needing to run the JavaScript in your automation. To do this/cover the example follow below steps:
String sJScript = "return document.getElementById('SampleSpan1').innerHTML;";
String sReturnText = (String) ((JavascriptExecutor) driver).executeScript(sJScript);
System.out.println(sReturnText);
Tuesday, February 12, 2013
How to close a dialog box using Selenium WebDriver
There might be places, when you click on an object a dialog will pop up asking your conformation. How to close those kind of alert messages?
The below code help you to close any such alert boxes.
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PopupDialog
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.agoda.com/?ymsg=1&tick=634955861691");
Alert alt=driver.switchTo().alert();
alt.accept();
}
}
The below code help you to close any such alert boxes.
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PopupDialog
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.agoda.com/?ymsg=1&tick=634955861691");
Alert alt=driver.switchTo().alert();
alt.accept();
}
}
Saturday, February 9, 2013
How to work with Cookies in a web site using selenium webdriver
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindAllCookiesInaWebSite
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://in.yahoo.com/");
Set<Cookie> cookies=driver.manage().getCookies();
//To find the number of cookies used by this site
System.out.println("Number of cookies in this site "+cookies.size());
for(Cookie cookie:cookies)
{
System.out.println(cookie.getName()+" "+cookie.getValue());
//This will delete cookie By Name
//driver.manage().deleteCookieNamed(cookie.getName());
//This will delete the cookie
//driver.manage().deleteCookie(cookie);
}
//This will delete all cookies.
//driver.manage().deleteAllCookies();
}
}
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindAllCookiesInaWebSite
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://in.yahoo.com/");
Set<Cookie> cookies=driver.manage().getCookies();
//To find the number of cookies used by this site
System.out.println("Number of cookies in this site "+cookies.size());
for(Cookie cookie:cookies)
{
System.out.println(cookie.getName()+" "+cookie.getValue());
//This will delete cookie By Name
//driver.manage().deleteCookieNamed(cookie.getName());
//This will delete the cookie
//driver.manage().deleteCookie(cookie);
}
//This will delete all cookies.
//driver.manage().deleteAllCookies();
}
}
Subscribe to:
Posts (Atom)