SlideShare a Scribd company logo
Page Objects with
Selenium WebDriver
Trójmiejska Grupa Testerska
Spotkanie #3
Marcin Wasilczyk, Oleksandr Perepelytsya
16-Apr-15 1
Commercial In Confidence & Proprietary, Kainos Software 2012
Agenda
 Automated Testing overview
 Selenium
 Page Object pattern
 Demo
 Konkurs (z nagrodami )
16-Apr-15 2
Commercial In Confidence & Proprietary, Kainos Software 2012
Automated Testing overview
16-Apr-15 3
• Reduces amount of manual testing
• Can be run frequently (e.g. every night/build)
• Save testers time and is a quick signal to the dev
team that something was broken recently (either
test or application)
• Requires effort to write them as reliable piece of
software
• Test code needs to be easy to maintain when
application changes
• The code should be as good as production code
Commercial In Confidence & Proprietary, Kainos Software 2012
Selenium
16-Apr-15 4
• Is Selenium a test framework? Nope!
• Selenium is a suite of tools that automate web
browsers
• Supports most of the browsers and operating systems
• Could be used for
• Automating boring web-based administration tasks
• Testing (when run by external test framework like NUnit)
• Two versions exist
• Selenium RC (Remote Control)
• Uses javascript ‘injection’ to control the browser
• not supported anymore
• Selenium WebDriver
• Uses browser’s native support for automation
Selenium Basic Concepts
IWebDriver instance lets you control the browser
ChromeDriver, InternetExplorerDriver, FirefoxDriver, etc...
IWebElement instance lets you operate the page element
IWebElement button =
driver.FindElement(By.ClassName(“PrimaryButton”));
button.Click();
WebDriverWait lets you wait for elements being loaded
var loadWait = new WebDriverWait(driver,
TimeSpan.FromSeconds(5));
loadWait.Until(ExpectedConditions.TitleIs(„My Dashboard“));
16-Apr-15 5
Page Object pattern
Create model of UI pages as objects
Reduces the amount of duplicated code
If you change anything in UI, fix will be applied only to one place
Encapsulation of Page Object internals
Client (e.g. NUnit test) will know only about services exposed as public
methods
Page Objects should return other Page Objects; examples:
var userPage = loginPage.LoginAs(“username”, “password”);
var loginPage = logoutPopup.Confirm();
Changing the id of the button you don’t need to change the test, only the
page object’s internals will be modified
16-Apr-15 6
Page Object pattern -
summary
Generally don't make assertions – these should be done in the test,
not in the Page Object
Need not represent an entire page
Different results for the same action are modelled as different
methods
var patientRecordPage =
dashboardPage.SimpleSearchForSinglePatientWith(searchText);
var dashboardPage =
dashboardPage.SimpleSearchForManyPatientsWith(searchText);
16-Apr-15 7
Page Object pattern -
example
public class LoginPage
{
private IWebDriver Driver { get; set; }
private IWebElement UserName { get; set; }
private IWebElement Password { get; set; }
private IWebElement LoginButton { get; set; }
public LoginPage(IWebDriver driver)
{
Driver = driver;
UserName = driver.FindElement(By....)
// initialization of the remaining elements
}
// methods like
public UserPage LoginAs(string username, string password) { ... }
}
16-Apr-15 8
Selenium – support of
“Page Object pattern”
PageFactory lets you automatically initialize elements defined in
Page Object (binding - action)
PageFactory.InitElements(driver, pageObject);
FindsBy attribute - automatically find elements on page (binding -
definition)
CacheLookup attribute – caches found element
[FindsBy(How = How.Id, Using = „userNameElement")]
[CacheLookup]
private IWebElement UserName { get; set; }
16-Apr-15 9
Selenium – support of
“Page Object pattern”
public class LoginPage
{
private IWebDriver Driver { get; set; }
[FindsBy(How = How.Id, Using = "ctl00_content_UserName")]
[CacheLookup]
private IWebElement UserName { get; set; }
// ... other fields with attributes applied...
public LoginPage(IWebDriver driver)
{
Driver = driver;
PageFactory.InitElements(this.Driver, this);
}
// methods like
public UserPage LoginAs(string username, string password) { ... }
} 16-Apr-15 10
Demo
16-Apr-15 11
Konkurs
Test Steps:
1. Navigate to Home page
2. Search for “laptop”
3. Click on 3 (third) element in the results list
4. Remember seller name and price
5. Add to bucket
6. Verify that item in the bucket has the same
seller and price
7. Remove item from bucket
8. Navigate to Home page
16-Apr-15 12
Questions?
16-Apr-15 13

More Related Content

What's hot

Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Applitools
 
UI Testing Automation
UI Testing AutomationUI Testing Automation
UI Testing Automation
AgileEngine
 
Acceptance & Functional Testing with Codeception - Devspace 2015
Acceptance & Functional Testing with Codeception - Devspace 2015 Acceptance & Functional Testing with Codeception - Devspace 2015
Acceptance & Functional Testing with Codeception - Devspace 2015
Joe Ferguson
 

What's hot (20)

Better Page Object Handling with Loadable Component Pattern
Better Page Object Handling with Loadable Component PatternBetter Page Object Handling with Loadable Component Pattern
Better Page Object Handling with Loadable Component Pattern
 
Automated Testing using JavaScript
Automated Testing using JavaScriptAutomated Testing using JavaScript
Automated Testing using JavaScript
 
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
Mastering UI automation at Scale: Key Lessons and Best Practices (By Fernando...
 
UI Testing Automation - Alex Kalinovsky - CreamTec LLC
UI Testing Automation - Alex Kalinovsky - CreamTec LLCUI Testing Automation - Alex Kalinovsky - CreamTec LLC
UI Testing Automation - Alex Kalinovsky - CreamTec LLC
 
Automated Web Testing using JavaScript
Automated Web Testing using JavaScriptAutomated Web Testing using JavaScript
Automated Web Testing using JavaScript
 
UI Testing Automation
UI Testing AutomationUI Testing Automation
UI Testing Automation
 
Automated UI testing done right (DDDSydney)
Automated UI testing done right (DDDSydney)Automated UI testing done right (DDDSydney)
Automated UI testing done right (DDDSydney)
 
Workshop - E2e tests with protractor
Workshop - E2e tests with protractorWorkshop - E2e tests with protractor
Workshop - E2e tests with protractor
 
Protractor training
Protractor trainingProtractor training
Protractor training
 
Acceptance & Functional Testing with Codeception - Devspace 2015
Acceptance & Functional Testing with Codeception - Devspace 2015 Acceptance & Functional Testing with Codeception - Devspace 2015
Acceptance & Functional Testing with Codeception - Devspace 2015
 
Automation using Javascript
Automation using JavascriptAutomation using Javascript
Automation using Javascript
 
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
Test-driven Development with Drupal and Codeception (DrupalCamp Brighton)
 
Better End-to-End Testing with Page Objects Model using Protractor
Better End-to-End Testing with Page Objects Model using ProtractorBetter End-to-End Testing with Page Objects Model using Protractor
Better End-to-End Testing with Page Objects Model using Protractor
 
Web UI test automation instruments
Web UI test automation instrumentsWeb UI test automation instruments
Web UI test automation instruments
 
Selenium Overview
Selenium OverviewSelenium Overview
Selenium Overview
 
Protractor overview
Protractor overviewProtractor overview
Protractor overview
 
PHP Unit Testing in Yii
PHP Unit Testing in YiiPHP Unit Testing in Yii
PHP Unit Testing in Yii
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular Slides
 
Automation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and BeyondAutomation Abstraction Layers: Page Objects and Beyond
Automation Abstraction Layers: Page Objects and Beyond
 
Codeception presentation
Codeception presentationCodeception presentation
Codeception presentation
 

Similar to Marcin Wasilczyk - Page objects with selenium

Similar to Marcin Wasilczyk - Page objects with selenium (20)

Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
Three Simple Chords of Alternative PageObjects and Hardcore of LoadableCompon...
 
I one aolpage-test-2010
I one aolpage-test-2010I one aolpage-test-2010
I one aolpage-test-2010
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
 
Good practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium testsGood practices for debugging Selenium and Appium tests
Good practices for debugging Selenium and Appium tests
 
Testing ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NETTesting ASP.NET - Progressive.NET
Testing ASP.NET - Progressive.NET
 
Qa process
Qa processQa process
Qa process
 
Page Object Model and Implementation in Selenium
Page Object Model and Implementation in Selenium  Page Object Model and Implementation in Selenium
Page Object Model and Implementation in Selenium
 
Mastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium SuccessfullyMastering Test Automation: How To Use Selenium Successfully
Mastering Test Automation: How To Use Selenium Successfully
 
Integrating Selenium testing infrastructure into Scala Project
Integrating Selenium testing infrastructure into Scala ProjectIntegrating Selenium testing infrastructure into Scala Project
Integrating Selenium testing infrastructure into Scala Project
 
Qa process
Qa processQa process
Qa process
 
Page object pattern
Page object patternPage object pattern
Page object pattern
 
Slides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testingSlides for Automation Testing or End to End testing
Slides for Automation Testing or End to End testing
 
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
 
Load Testing: See a Bigger Picture
Load Testing: See a Bigger PictureLoad Testing: See a Bigger Picture
Load Testing: See a Bigger Picture
 
Automation Abstractions: Page Objects and Beyond
Automation Abstractions: Page Objects and BeyondAutomation Abstractions: Page Objects and Beyond
Automation Abstractions: Page Objects and Beyond
 
BDD with SpecFlow and Selenium
BDD with SpecFlow and SeleniumBDD with SpecFlow and Selenium
BDD with SpecFlow and Selenium
 
Selenium
SeleniumSelenium
Selenium
 
Test Automation Framework Development Introduction
Test Automation Framework Development IntroductionTest Automation Framework Development Introduction
Test Automation Framework Development Introduction
 
Selenium RC: Automated Testing of Modern Web Applications
Selenium RC: Automated Testing of Modern Web ApplicationsSelenium RC: Automated Testing of Modern Web Applications
Selenium RC: Automated Testing of Modern Web Applications
 
Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriver
 

More from Trójmiejska Grupa Testerska

More from Trójmiejska Grupa Testerska (20)

Rafal prezentacja testowanie ai
Rafal prezentacja testowanie aiRafal prezentacja testowanie ai
Rafal prezentacja testowanie ai
 
Tgt 23 przemyslaw_pradzynski
Tgt 23 przemyslaw_pradzynskiTgt 23 przemyslaw_pradzynski
Tgt 23 przemyslaw_pradzynski
 
TGT#21 - Tester eksploracyjny, ostatni zawód na świecie. – Radosław Smilgin
TGT#21 - Tester eksploracyjny, ostatni zawód na świecie. – Radosław SmilginTGT#21 - Tester eksploracyjny, ostatni zawód na świecie. – Radosław Smilgin
TGT#21 - Tester eksploracyjny, ostatni zawód na świecie. – Radosław Smilgin
 
TGT#19 - Wszyscy jestesmy testerami - Michal Rabczuk
TGT#19 - Wszyscy jestesmy testerami - Michal RabczukTGT#19 - Wszyscy jestesmy testerami - Michal Rabczuk
TGT#19 - Wszyscy jestesmy testerami - Michal Rabczuk
 
TGT#20 - Ataki XSS - Robert Charewicz
TGT#20 - Ataki XSS - Robert CharewiczTGT#20 - Ataki XSS - Robert Charewicz
TGT#20 - Ataki XSS - Robert Charewicz
 
TGT#20 - Automated Tests Only For Testers - Kasper Kulikowski
TGT#20 - Automated Tests Only For Testers - Kasper KulikowskiTGT#20 - Automated Tests Only For Testers - Kasper Kulikowski
TGT#20 - Automated Tests Only For Testers - Kasper Kulikowski
 
TGT#20 - TGT in Numbers - Mateusz Radkiewicz
TGT#20 - TGT in Numbers - Mateusz RadkiewiczTGT#20 - TGT in Numbers - Mateusz Radkiewicz
TGT#20 - TGT in Numbers - Mateusz Radkiewicz
 
TGT#19 - 3 seconds or less - Piotr Liss
TGT#19 - 3 seconds or less - Piotr LissTGT#19 - 3 seconds or less - Piotr Liss
TGT#19 - 3 seconds or less - Piotr Liss
 
TGT#18 - End-to-end testing using Protractor - Jakub Raniszewski
TGT#18 - End-to-end testing using Protractor - Jakub RaniszewskiTGT#18 - End-to-end testing using Protractor - Jakub Raniszewski
TGT#18 - End-to-end testing using Protractor - Jakub Raniszewski
 
TGT#18 - BDD with The Three Amigos in API Testing - Julia Szarlej
TGT#18 - BDD with The Three Amigos in API Testing - Julia SzarlejTGT#18 - BDD with The Three Amigos in API Testing - Julia Szarlej
TGT#18 - BDD with The Three Amigos in API Testing - Julia Szarlej
 
TGT#17 - Efektywne testy oprogramowania w środowisku Scrumowym - Marcin Kubecki
TGT#17 - Efektywne testy oprogramowania w środowisku Scrumowym - Marcin KubeckiTGT#17 - Efektywne testy oprogramowania w środowisku Scrumowym - Marcin Kubecki
TGT#17 - Efektywne testy oprogramowania w środowisku Scrumowym - Marcin Kubecki
 
TGT#17 - RestApi testing tips and tricks: how to start testing api of your we...
TGT#17 - RestApi testing tips and tricks: how to start testing api of your we...TGT#17 - RestApi testing tips and tricks: how to start testing api of your we...
TGT#17 - RestApi testing tips and tricks: how to start testing api of your we...
 
TGT#16 - Pain of test automatization in PAAS/SAAS solutions - Krzysztof Lembi...
TGT#16 - Pain of test automatization in PAAS/SAAS solutions - Krzysztof Lembi...TGT#16 - Pain of test automatization in PAAS/SAAS solutions - Krzysztof Lembi...
TGT#16 - Pain of test automatization in PAAS/SAAS solutions - Krzysztof Lembi...
 
TGT#16 - Rozmowa techniczna (short talk) - Waldemar Mozoła
TGT#16 - Rozmowa techniczna (short talk) - Waldemar MozołaTGT#16 - Rozmowa techniczna (short talk) - Waldemar Mozoła
TGT#16 - Rozmowa techniczna (short talk) - Waldemar Mozoła
 
TGT#16 - Sztuka projektowania testów - Agnieszka Garwolińska
TGT#16 - Sztuka projektowania testów - Agnieszka GarwolińskaTGT#16 - Sztuka projektowania testów - Agnieszka Garwolińska
TGT#16 - Sztuka projektowania testów - Agnieszka Garwolińska
 
TGT#15 - Piramida testów w praktyce (notatki z dyskusji)
TGT#15 - Piramida testów w praktyce (notatki z dyskusji)TGT#15 - Piramida testów w praktyce (notatki z dyskusji)
TGT#15 - Piramida testów w praktyce (notatki z dyskusji)
 
TGT#15 - Testowanie w metodykach zwinnych czyli skąd testerzy wiedzą więcej o...
TGT#15 - Testowanie w metodykach zwinnych czyli skąd testerzy wiedzą więcej o...TGT#15 - Testowanie w metodykach zwinnych czyli skąd testerzy wiedzą więcej o...
TGT#15 - Testowanie w metodykach zwinnych czyli skąd testerzy wiedzą więcej o...
 
TGT#14 - @Before – Nie będę automatyzować @After – No dobra, to nie jest taki...
TGT#14 - @Before – Nie będę automatyzować @After – No dobra, to nie jest taki...TGT#14 - @Before – Nie będę automatyzować @After – No dobra, to nie jest taki...
TGT#14 - @Before – Nie będę automatyzować @After – No dobra, to nie jest taki...
 
TGT#14 - Case Study: Współpraca Testera i Product Ownera w zespole scrumowym ...
TGT#14 - Case Study: Współpraca Testera i Product Ownera w zespole scrumowym ...TGT#14 - Case Study: Współpraca Testera i Product Ownera w zespole scrumowym ...
TGT#14 - Case Study: Współpraca Testera i Product Ownera w zespole scrumowym ...
 
TG#13 - Nie samym Selenium człowiek żyje, czyli jak tworzyć stabilne testy fu...
TG#13 - Nie samym Selenium człowiek żyje, czyli jak tworzyć stabilne testy fu...TG#13 - Nie samym Selenium człowiek żyje, czyli jak tworzyć stabilne testy fu...
TG#13 - Nie samym Selenium człowiek żyje, czyli jak tworzyć stabilne testy fu...
 

Recently uploaded

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 

Recently uploaded (20)

How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 

Marcin Wasilczyk - Page objects with selenium

  • 1. Page Objects with Selenium WebDriver Trójmiejska Grupa Testerska Spotkanie #3 Marcin Wasilczyk, Oleksandr Perepelytsya 16-Apr-15 1
  • 2. Commercial In Confidence & Proprietary, Kainos Software 2012 Agenda  Automated Testing overview  Selenium  Page Object pattern  Demo  Konkurs (z nagrodami ) 16-Apr-15 2
  • 3. Commercial In Confidence & Proprietary, Kainos Software 2012 Automated Testing overview 16-Apr-15 3 • Reduces amount of manual testing • Can be run frequently (e.g. every night/build) • Save testers time and is a quick signal to the dev team that something was broken recently (either test or application) • Requires effort to write them as reliable piece of software • Test code needs to be easy to maintain when application changes • The code should be as good as production code
  • 4. Commercial In Confidence & Proprietary, Kainos Software 2012 Selenium 16-Apr-15 4 • Is Selenium a test framework? Nope! • Selenium is a suite of tools that automate web browsers • Supports most of the browsers and operating systems • Could be used for • Automating boring web-based administration tasks • Testing (when run by external test framework like NUnit) • Two versions exist • Selenium RC (Remote Control) • Uses javascript ‘injection’ to control the browser • not supported anymore • Selenium WebDriver • Uses browser’s native support for automation
  • 5. Selenium Basic Concepts IWebDriver instance lets you control the browser ChromeDriver, InternetExplorerDriver, FirefoxDriver, etc... IWebElement instance lets you operate the page element IWebElement button = driver.FindElement(By.ClassName(“PrimaryButton”)); button.Click(); WebDriverWait lets you wait for elements being loaded var loadWait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); loadWait.Until(ExpectedConditions.TitleIs(„My Dashboard“)); 16-Apr-15 5
  • 6. Page Object pattern Create model of UI pages as objects Reduces the amount of duplicated code If you change anything in UI, fix will be applied only to one place Encapsulation of Page Object internals Client (e.g. NUnit test) will know only about services exposed as public methods Page Objects should return other Page Objects; examples: var userPage = loginPage.LoginAs(“username”, “password”); var loginPage = logoutPopup.Confirm(); Changing the id of the button you don’t need to change the test, only the page object’s internals will be modified 16-Apr-15 6
  • 7. Page Object pattern - summary Generally don't make assertions – these should be done in the test, not in the Page Object Need not represent an entire page Different results for the same action are modelled as different methods var patientRecordPage = dashboardPage.SimpleSearchForSinglePatientWith(searchText); var dashboardPage = dashboardPage.SimpleSearchForManyPatientsWith(searchText); 16-Apr-15 7
  • 8. Page Object pattern - example public class LoginPage { private IWebDriver Driver { get; set; } private IWebElement UserName { get; set; } private IWebElement Password { get; set; } private IWebElement LoginButton { get; set; } public LoginPage(IWebDriver driver) { Driver = driver; UserName = driver.FindElement(By....) // initialization of the remaining elements } // methods like public UserPage LoginAs(string username, string password) { ... } } 16-Apr-15 8
  • 9. Selenium – support of “Page Object pattern” PageFactory lets you automatically initialize elements defined in Page Object (binding - action) PageFactory.InitElements(driver, pageObject); FindsBy attribute - automatically find elements on page (binding - definition) CacheLookup attribute – caches found element [FindsBy(How = How.Id, Using = „userNameElement")] [CacheLookup] private IWebElement UserName { get; set; } 16-Apr-15 9
  • 10. Selenium – support of “Page Object pattern” public class LoginPage { private IWebDriver Driver { get; set; } [FindsBy(How = How.Id, Using = "ctl00_content_UserName")] [CacheLookup] private IWebElement UserName { get; set; } // ... other fields with attributes applied... public LoginPage(IWebDriver driver) { Driver = driver; PageFactory.InitElements(this.Driver, this); } // methods like public UserPage LoginAs(string username, string password) { ... } } 16-Apr-15 10
  • 12. Konkurs Test Steps: 1. Navigate to Home page 2. Search for “laptop” 3. Click on 3 (third) element in the results list 4. Remember seller name and price 5. Add to bucket 6. Verify that item in the bucket has the same seller and price 7. Remove item from bucket 8. Navigate to Home page 16-Apr-15 12