Upload
david-stark
View
300
Download
1
Embed Size (px)
Citation preview
Selenium
An Introduction
Selenium
❖ What is it?
❖ What is it good for?
❖ How do I get started?
❖ What are the gotchas?
Selenium?
❖ AKA Webdriver
❖ Web automation/testing framework
❖ Tests run on real browsers
wd.get("http://www.metafilter.com/")wd.find_element_by_link_text("FAQ").click()
Yesss Master...
Pros
❖ Realistic test conditions
❖ Cross-browser testing
❖ Popular & mature tech
Cons
❖ Slow
❖ Changing HTML may break tests
Uses
❖ Acceptance Testing
❖ Continuous Integration
❖ Monitoring
❖ TDD? Maybe?
Python example
from selenium.webdriver.firefox.webdriver import WebDriver
wd = WebDriver()
wd.implicitly_wait(60)
wd.get("http://www.metafilter.com/")
wd.find_element_by_link_text("FAQ").click()
assert "How do I sign up for a MetaFilter account?"
in wd.find_element_by_tag_name("html").text
PHP!
require_once 'php-webdriver';
$wd = new WebDriver();
$session = $wd->session();
$session->open("http://www.metafilter.com/");
$session->element("link text", "FAQ")->click();
$session->close();
assert(strpos(
$session->element("tag name", "html")->text(),
"How do I sign up for a MetaFilter account?"));
Java!
import org.openqa.selenium.*;
import static java.util.concurrent.TimeUnit.*;
FirefoxDriver wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(60, SECONDS);
wd.get("http://www.metafilter.com/");
wd.findElement(By.linkText("FAQ")).click();
assert wd.findElement(By.tagName("html")).getText()
.contains("How do I sign up for a MetaFilter account?")
wd.quit();
Getting started, option A
❖ Get Selenium server from seleniumhq.org
❖ Download language binding of your choice
❖ Write some code & play it back
Getting started, option B
➔ Get Selenium Builder from sebuilder.com
➔ Record your actions as tests
➔ Play back locally, or via server
➔ Export to code or JSON
➔ Run JSON via interpreters
Gotcha 1: Visibility
❖ Selenium respects display: none
❖ You can’t interact with something invisible
❖ Even if it’s in the HTML
Gotcha 2: Timing
❖ Page needs to load first
❖ Some Javascript has to execute too?
❖ Page changes dynamically?
Gotcha 2: Timing
❖ Wait for page elements
❖ Implicitly or explicitly
❖ Don’t use pause!
Gotcha 2: Timing
from selenium.webdriver.firefox.webdriver import WebDriver
wd = WebDriver()
wd.implicitly_wait(60)
wd.get("http://www.metafilter.com/")
wd.find_element_by_link_text("FAQ").click()
assert "How do I sign up for a MetaFilter account?"
in wd.find_element_by_tag_name("html").text
Gotcha 3: Locators
from selenium.webdriver.firefox.webdriver import WebDriver
wd = WebDriver()
wd.implicitly_wait(60)
wd.get("http://www.metafilter.com/")
wd.find_element_by_link_text("FAQ").click()
assert "How do I sign up for a MetaFilter account?"
in wd.find_element_by_tag_name("html").text
Gotcha 3: Locators
❖ Locators identify UI elements
❖ HTML changes can break locators
Gotcha 3: Locators
❖ Use simple, clean, robust locators
❖ Depends on your approach
❖ Text vs ID vs CSS
❖ Careful XPATH for the tough cases
Good & bad locators
id: home_link
id: 12094b
css: span.green
css: #rightbar > .menu > li:nth-of-type(3) > h5
css: .logout
xpath: //*[@id='menu']//input[3]
xpath: //header/div/div[1]/div[1]/a/h1/span[2]
Good & bad locators
id: home_link
id: 12094b
css: span.green
css: #rightbar > .menu > li:nth-of-type(3) > h5
css: .logout
xpath: //*[@id='menu']//input[3]
xpath: //header/div/div[1]/div[1]/a/h1/span[2]
Resources
❖ Selenium HQ
❖ A CI solution with Builder, Travis & Sauce
❖ Obligatory self-promotion!
Thank you! Questions?
@zarkonnen_com