Following code demonstrates use of fireevent in selenium. Look at following JSP code there are two input boxes, after typing somthing in the first input box and then when you loose the focus ,it calls
fun method,and fun method types text into the second input box,ideally it should happen . However when we automate following page using selenium it will not call fun method after loosing the focus of first input box ,selenium does not recognize this event handling. To overcome such issues selenium provides fireevent method which takes two parameters first is locator of the element ,second is event to be called .
Ex. selenium.fireEvent("id1","blur"); // It will call fun method of id1 element .
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function fun()
{
document.getElementById("id2").value="guru";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input id="id1" type ="text" onblur="fun()"/>
<input id="id2" type ="text"/>
</body>
</html>
Following is the sample java code :
package selenium;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class FireEventEx {
/**
* @param args
*/
public static void main(String[] args) {
Selenium s=null;
try
{
s=new DefaultSelenium("localhost", 4441, "*firefox", "http://localhost:8080/saome/blur.jsp");
s.start();
s.windowMaximize();
s.open("/saome/blur.jsp");
s.type("id1", "david");
s.fireEvent("id1", "blur");
Thread.sleep(3000);
s.stop();
}
catch (Exception e) {
e.printStackTrace();
s.stop();
}
}
}
fun method,and fun method types text into the second input box,ideally it should happen . However when we automate following page using selenium it will not call fun method after loosing the focus of first input box ,selenium does not recognize this event handling. To overcome such issues selenium provides fireevent method which takes two parameters first is locator of the element ,second is event to be called .
Ex. selenium.fireEvent("id1","blur"); // It will call fun method of id1 element .
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script>
function fun()
{
document.getElementById("id2").value="guru";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input id="id1" type ="text" onblur="fun()"/>
<input id="id2" type ="text"/>
</body>
</html>
Following is the sample java code :
package selenium;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class FireEventEx {
/**
* @param args
*/
public static void main(String[] args) {
Selenium s=null;
try
{
s=new DefaultSelenium("localhost", 4441, "*firefox", "http://localhost:8080/saome/blur.jsp");
s.start();
s.windowMaximize();
s.open("/saome/blur.jsp");
s.type("id1", "david");
s.fireEvent("id1", "blur");
Thread.sleep(3000);
s.stop();
}
catch (Exception e) {
e.printStackTrace();
s.stop();
}
}
}
No comments:
Post a Comment