Selenium intro
-
Upload
david-stark -
Category
Software
-
view
300 -
download
1
Transcript of Selenium intro
![Page 1: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/1.jpg)
Selenium
An Introduction
![Page 2: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/2.jpg)
Selenium
❖ What is it?
❖ What is it good for?
❖ How do I get started?
❖ What are the gotchas?
![Page 3: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/3.jpg)
Selenium?
❖ AKA Webdriver
❖ Web automation/testing framework
❖ Tests run on real browsers
![Page 4: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/4.jpg)
wd.get("http://www.metafilter.com/")wd.find_element_by_link_text("FAQ").click()
Yesss Master...
![Page 5: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/5.jpg)
Pros
❖ Realistic test conditions
❖ Cross-browser testing
❖ Popular & mature tech
![Page 6: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/6.jpg)
Cons
❖ Slow
❖ Changing HTML may break tests
![Page 7: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/7.jpg)
Uses
❖ Acceptance Testing
❖ Continuous Integration
❖ Monitoring
❖ TDD? Maybe?
![Page 8: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/8.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/9.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/10.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/11.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/12.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/13.jpg)
![Page 14: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/14.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/15.jpg)
Gotcha 2: Timing
❖ Page needs to load first
❖ Some Javascript has to execute too?
❖ Page changes dynamically?
![Page 16: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/16.jpg)
Gotcha 2: Timing
❖ Wait for page elements
❖ Implicitly or explicitly
❖ Don’t use pause!
![Page 17: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/17.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/18.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/19.jpg)
Gotcha 3: Locators
❖ Locators identify UI elements
❖ HTML changes can break locators
![Page 20: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/20.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/21.jpg)
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](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/22.jpg)
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 23: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/23.jpg)
Resources
❖ Selenium HQ
❖ A CI solution with Builder, Travis & Sauce
❖ Obligatory self-promotion!
![Page 24: Selenium intro](https://reader030.fdocument.pub/reader030/viewer/2022021502/58edf1f61a28aba4108b45ed/html5/thumbnails/24.jpg)
Thank you! Questions?
@zarkonnen_com