INTRODUCTION TO WEB
BROWSER AUTOMATION
Péter Csapó
Szilárd Mikó
Norber Bauman
EPAM Systems, Budapest, 2016
Agenda
Record and Replay1
Selenium-Webdriver2
Challenges / Best practices3
3
RECORD AND REPLAY
4
Record and Replay
• Record exact user interactions previously performed by the tester
• Easy to create test cases
• Ineffective repetition
• Limited options
• Limited verification capabilities
• e.g. Selenium IDE, , QTP, TestComplete
5
SELENIUM WEBDRIVER
6
Script->Driver->Browser
• Selenium Webdriver
• Selenium Grid
• Watir
• Capybara
• PhantomJS
7
Script->Driver->Browser structure
8
Browsers
9
Supported OS
10
Supported Programming Languages
11
Example:
WebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.hu");
driver.FindElement(By.Name("q")).SendKeys("Selenium");
driver.FindElement(By.Name("btnG")).Click();
WebDriverWait wait = new WebDriverWait(driver,
TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.ElementIsVisible
(By.LinkText("Képek")));
driver.FindElement(By.LinkText("Képek")).Click();
Assert.IsTrue(driver.FindElement(By.Id("rg_s")).Displayed);
12
What can you do with a browser?
• Navigate to url
• get title, get url
• take screenshot
• refresh page,
• close browser/tab
• Send keys
• execute script
13
HTML element locator strategy
• Example: driver.FindElement(By.Name("btnG"))
• Most widely used attributes and techniques by order of
preference:
–
– linkText, partialLinkText
– tag, class
– css
– xpath
• Avoid css and xpath for performance and maintainability
reasons
• A good locator strategy is the difference between
maintainable and brittle automation code
14
Choosing a good locator strategy
• the element’s ID:
• IDs are (should be) unique on the page. the fastest and the best. Use it!
• use name when IDs are not available
• text:
• Easy to code
• acceptable only when single language is used, no changes are expected
• not necessarily unique, high maintenance
• CSS selectors:
• Like XPATH only better. Will solve 98% of your problems
• XPath:
• Last resort only - extremely powerful, but the slowest. Needs skill.
• Native XPATH: html/body/div/table[5]/tbody/tr/td[2]/div/a/b – very brittle
• Relative XPATH: //div[@id = 'foo']//a - not so brittle but slow
15
HTML Elements methods
• Element type:
• IWebElement
• Most frequently used methods:
• Clear, Click, FindElement, FindElements, GetAttribute, SendKeys, Submit
• Example:
driver.FindElement(By.Name("btnG")).Click()
• Whatever a real user can do, selenium should be able to do as
well
16
CHALLENGES
BEST PRACTICES
17
Challenges - Wait
• You can use explicit wait to handle
exceptional cases
• modern web pages pull a lot of data after DOM complete
• Worst case scenario: sleep
WebDriverWait wait = new WebDriverWait(driver,
TimeSpan.FromSeconds(5));
wait.Until(d => d.FindElement(By.LinkText("Képek")).Enabled);
• Implicit wait:
• WebDriver waits for the DOM to complete loading before trying to manage the browser or locate an element, but no more than that by default
• Implicit wait can be changed
18
Challenges – Browser differences
• Major functionality is consistent but there can be
subtle differences in:
• Browser size
• Screenshots
• Element visibility
• Performance
• Native events
• Cookie management
• IE is the worst offender
19
Cross Browser testing solutions
• crossbrowsertesting.com
• Browsers including older versions, OS
• Supports lab environment
• webpagetest.org
• Performance data – including visual comparison
• support for browsers, mobile, scripts, api to automate
• Physical location
• saucelabs.com
• automated cross browser testing (selenium) in the cloud
20
The future…
• All big web based companies rely heavily on test
automation
• YahooMail
• Ship early, ship often
• Difference between dev and QA is disappearing
21
Questions
Top Related