Selenium intro

24
Selenium An Introduction

Transcript of Selenium intro

Page 1: Selenium intro

Selenium

An Introduction

Page 2: Selenium intro

Selenium

❖ What is it?

❖ What is it good for?

❖ How do I get started?

❖ What are the gotchas?

Page 3: Selenium intro

Selenium?

❖ AKA Webdriver

❖ Web automation/testing framework

❖ Tests run on real browsers

Page 4: Selenium intro

wd.get("http://www.metafilter.com/")wd.find_element_by_link_text("FAQ").click()

Yesss Master...

Page 5: Selenium intro

Pros

❖ Realistic test conditions

❖ Cross-browser testing

❖ Popular & mature tech

Page 6: Selenium intro

Cons

❖ Slow

❖ Changing HTML may break tests

Page 7: Selenium intro

Uses

❖ Acceptance Testing

❖ Continuous Integration

❖ Monitoring

❖ TDD? Maybe?

Page 8: Selenium intro

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

Page 9: Selenium intro

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?"));

Page 10: Selenium intro

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();

Page 11: Selenium intro

Getting started, option A

❖ Get Selenium server from seleniumhq.org

❖ Download language binding of your choice

❖ Write some code & play it back

Page 12: Selenium intro

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

Page 13: Selenium intro
Page 14: Selenium intro

Gotcha 1: Visibility

❖ Selenium respects display: none

❖ You can’t interact with something invisible

❖ Even if it’s in the HTML

Page 15: Selenium intro

Gotcha 2: Timing

❖ Page needs to load first

❖ Some Javascript has to execute too?

❖ Page changes dynamically?

Page 16: Selenium intro

Gotcha 2: Timing

❖ Wait for page elements

❖ Implicitly or explicitly

❖ Don’t use pause!

Page 17: Selenium intro

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

Page 18: Selenium intro

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

Page 19: Selenium intro

Gotcha 3: Locators

❖ Locators identify UI elements

❖ HTML changes can break locators

Page 20: Selenium intro

Gotcha 3: Locators

❖ Use simple, clean, robust locators

❖ Depends on your approach

❖ Text vs ID vs CSS

❖ Careful XPATH for the tough cases

Page 21: Selenium intro

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]

Page 22: Selenium intro

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]

Page 24: Selenium intro

Thank you! Questions?

@zarkonnen_com