Download - Gems Of Selenium
@testinggeek www.TestingGeek.com
Gems of Selenium
Anand Ramdeo
Komal Joshi
Selenium Is..
www.atlantissw.com | www.TestingGeek.com
A bit of History..
www.atlantissw.com | www.TestingGeek.com
SELENIUM
It All Started Here• Time and Expense application built using
Python+Plone @ Thoughtworks• Goal was “Fast, Fast, Fast”
www.atlantissw.com | www.TestingGeek.com
ADD ROW
It All Started Here
• Time and Expense application built using Python+Plone @ Thoughtworks
• Goal was “Fast, Fast, Fast”
www.atlantissw.com | www.TestingGeek.com
ADD ROW
It All Started Here
• Time and Expense application built using Python+Plone @ Thoughtworks
• Goal was “Fast, Fast, Fast”
www.atlantissw.com | www.TestingGeek.com
ADD ROW
Problems..
www.atlantissw.com | www.TestingGeek.com
And Solutions..
• Mozilla Extension – “Driftwood” by Andrew McCormick from Thoughtworks. Worked, but it was firefox only solution.
• Paul Gross and Jasson Huggin set out to implement Fit for JS.
• Which eventually became Selenium Core or Java Script Test Runner
www.atlantissw.com | www.TestingGeek.com
Popularity of Selenium
www.atlantissw.com | www.TestingGeek.com
Reason of Success
www.atlantissw.com | www.TestingGeek.com
@testinggeek www.TestingGeek.com
In search of treasure
The journey begins..•Want to automate ? •What are the common problems faced by people when using selenium IDE?
@testinggeek www.TestingGeek.com
In search of treasureTreasure is ….•Reliable automation.•Stable automation.•Maintainable automation.•Effective Automation.
@testinggeek www.TestingGeek.com
Found my first Gem….UI Element
How can this precious gem help me?
@testinggeek www.TestingGeek.com
UI Element is...
• • ...a mapping between semantically meaningful names of elements on webpages, and the elements.
...defined using JSON
@testinggeek www.TestingGeek.com
Abstraction With UI-Element selenium.type("q", “TestingGeek"); selenium.click("btnG");
LOGIN_BUTTON = "css=input[value='Log in']"DELETE_CONFIRMATION = "css=input[value='\"Yes, I'm sure\"']"
myMap.addPageset({ name: 'allPages', description: 'All WLL Pages', pathRegexp: '.*'});
myMap.addElement ('allPages', { name: 'register' , description: 'Register link on all the pages' , locator: "xpath=//*[@id='user']/dd/a[1]" })
sel.click("ui=allPages::register()")
Abstracting with UI Element
@testinggeek www.TestingGeek.com
I want to extend Selenium with some new assertions,actions
No problem User-extension.js is here for that.
@testinggeek www.TestingGeek.com
Extending Slenium...
Selenium Object PrototypePagebot Object Prototype.On startup, Selenium will automatically look
through methods on these prototypes, using name patterns to recognize which ones are actions, assertions and locators.
@testinggeek www.TestingGeek.com
Automation is incomplete without cross browser testing
Selenium RC gem for different browser…• * iehta• * firefox• * iexplore• * chrome• * custom (for custom browsers)
@testinggeek www.TestingGeek.com
https...
Different options of selenium…- https- Multiwindow=<true/false>- Firefoxtemplate=<default template
location>
@testinggeek www.TestingGeek.com
Collecting all the gems and putting in the treasure chest
@testinggeek www.TestingGeek.com
Is this the only treasure• The real gems are in your minds in your brains.• Come up with a solution• Implement a simple but effective solution.
@testinggeek www.TestingGeek.com
Automate using wrappers.
SA
Use a combination of tools....• Selenium + Auto it.• Selenium + Batch files.
@testinggeek www.TestingGeek.com
SA
/** * This method would just kill the PDF*/
public void closesPDF(){{
String[] dialog = new String[] { "src\\test\\resources\\KillAcroRd32.bat"};try {
Process p = Runtime.getRuntime().exec(dialog);System.out.println(p.getErrorStream().toString());System.out.println(p.getInputStream().toString());System.out.println(p.getOutputStream().toString());System.out.println("After execution");
} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("In the exception");e.printStackTrace();throw new AssertionError("No Dialogue box displayed");
} }
}
@testinggeek www.TestingGeek.com
Easy to read Natural language automation• Simple easy to read language
• POM.
@testinggeek www.TestingGeek.com
Componentise your automation
@testinggeek www.TestingGeek.com
Add dynamism...
Connecting to the databses…
@testinggeek www.TestingGeek.com
All this is less effective if automation does not run automatically......
@testinggeek www.TestingGeek.com
Integrate with CI tools
Mavenise your automation... And integrate them with Hudson...
@testinggeek www.TestingGeek.com
Distribute the Tests
With Selenium Grids or
Hudson Slaves...
@testinggeek www.TestingGeek.com
Integrate with CI tools
What about Selenium 2.0 ?.....
Will my old selenium tests run in 2.0?
@testinggeek www.TestingGeek.com
Selenium 2.0
-Web Driver- Presentation redering etc.
@testinggeek www.TestingGeek.com
import java.util.List;
import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.RenderedWebElement;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;
public class TestingGeek {
public static void main(String[] args) throws Exception { // The Firefox driver supports javascript WebDriver driver = new FirefoxDriver();
// Go to the TestingGeek home page driver.get("http://www.testinggeek.com");
// Enter the query string "Cheese" //WebElement query = driver.findElement(By.name("q")); WebElement menu= driver.findElement(By.id("menu27")); menu.click(); // query.sendKeys("Cheese");
// Sleep until the div we want is visible or 5 seconds is over long end = System.currentTimeMillis() + 5000; while (System.currentTimeMillis() < end) { // Browsers which render content (such as Firefox and IE) // return "RenderedWebElements" // RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By.className("gac_m")); RenderedWebElement resultsDiv = (RenderedWebElement) driver.findElement(By. id("menu27")); // If results have been returned, // the results are displayed in a drop down. if (resultsDiv.isDisplayed()) { break; } }
// And now list the suggestions //List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gac_c']")); List<WebElement> menus = driver.findElements(By.xpath("//div/div/div/ul/li[2]/ul")); for (WebElement menu1 : menus) { System.out.println(menu1.getText()); } }
}
@testinggeek www.TestingGeek.com
Thank You.
Anand Ramdeo
www.TestingGeek.com@testinggeek
www.AtlantisSw.com