ntroduction to web rowser...

21
I NTRODUCTION TO W EB B ROWSER A UTOMATION Péter Csapó Szilárd Mikó Norber Bauman EPAM Systems, Budapest, 201 6

Upload: buidien

Post on 15-Jun-2018

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

INTRODUCTION TO WEB

BROWSER AUTOMATION

Péter Csapó

Szilárd Mikó

Norber Bauman

EPAM Systems, Budapest, 2016

Page 2: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

Agenda

Record and Replay1

Selenium-Webdriver2

Challenges / Best practices3

Page 3: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

3

RECORD AND REPLAY

Page 4: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 5: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

5

SELENIUM WEBDRIVER

Page 6: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

6

Script->Driver->Browser

• Selenium Webdriver

• Selenium Grid

• Watir

• Capybara

• PhantomJS

Page 7: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

7

Script->Driver->Browser structure

Page 8: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

8

Browsers

Page 9: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

9

Supported OS

Page 10: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

10

Supported Programming Languages

Page 11: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 12: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 13: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 14: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 15: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 16: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

16

CHALLENGES

BEST PRACTICES

Page 17: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 18: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 19: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

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

Page 20: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

20

The future…

• All big web based companies rely heavily on test

automation

• Google

• YahooMail

• LinkedIn

• Facebook

• Ship early, ship often

• Difference between dev and QA is disappearing

Page 21: NTRODUCTION TO WEB ROWSER AUTOMATIONcompalg.inf.elte.hu/~attila/materials/Webdriver_Introduction_JAVA.pdf · •e.g. Selenium IDE, , QTP, TestComplete. 5 SELENIUM WEBDRIVER ... •

21

Questions