Why do I need to extend selenium? Selenium Notes, Videos, Examples By.www.laxmiroy.blogspot.com/
If you want certain operations to be done every time before a step is executed then you would have to extend the default selenium. For example let’s say that you wish to wait 5 seconds after every step or you want to highlight the element before clicking on it or you want to record every step in log file or you want to poll for a element’s existence for a certain amount of time before performing an operation (click, type) on it. For all these to happen you would have to extend the default selenium class that you use in your RC test script.
Below is a simple example that demonstrates this concept. The classMySelenium extends the class DefaultSelenium. The methods click andtype of the base class have been over-ridden in such a way that the target element (which is to be clicked on or typed into) is highlighted thrice before the click or type operation. The class UsingMySelenium by the means of a simple example demonstrates the usage of our extended class.
Class MySelenium
package seleniumExtention;
import com.thoughtworks.selenium.DefaultSelenium;
public class MySelenium extends DefaultSelenium{
public MySelenium(String seleniumSeverHost, int port,String browserString,String autURL){
super(seleniumSeverHost, port,browserString, autURL);
}
@Override
public void click(String target) {
try {
for (int i=0;i<3;i++){
super.highlight(target);
Thread.sleep(500);
}
super.click(target);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void type(String target, String value){
try{
for (int i=0;i<3;i++){
super.highlight(target);
Thread.sleep(500);
}
super.type(target,value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Class UsingMySelenium
package seleniumExtention;
import org.junit.Test;
import org.junit.BeforeClass;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.SeleneseTestCase;
public class UsingMySelenium extends SeleneseTestCase{
public MySelenium selenium;
@BeforeClass
public void setUp() throws Exception {
SeleniumServer seleniumserver=new SeleniumServer();
seleniumserver.boot();
seleniumserver.start();
/*using the customized selenium class that we created earlier instead of the default selenium*/
selenium=new MySelenium ("localhost",4444,"*iexplore","http://www.google.com/");
selenium.start();
selenium.windowMaximize();
}
@Test
public void testSimpleGoogleSearch() throws Exception {
selenium.open("/");
selenium.waitForPageToLoad("5000");
selenium.type("q", "Aston Martin volante");
selenium.click("//input[@value='Google Search']");
selenium.waitForPageToLoad("5000");
selenium.click("xpath=/descendant::a[text()=' - The Cars']/em[text()='Aston Martin']/parent::a");
selenium.waitForPageToLoad("25000");
Thread.sleep(5000);
}
}
To make this work you need to do the following
1) Create a package seleniumExtention inside the java project.
2) Create the classes MySelenium & UsingMySelenium and copy paste the content as given above.
3) Run the testSimpleGoogleSearch() given in the classUsingMySelenium (right click Run As-->JUnit Test).
*Following jars need to be referenced
selenium-server.jar
selenium-java-client-driver.jar
junit.jar
No comments:
Post a Comment