Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Page Objects with
Selenium WebDriver
Trójmiejska Grupa Testerska
Spotkanie #3
Marcin Wasilczyk, Oleksandr Perepelytsya
16-...
Commercial In Confidence & Proprietary, Kainos Software 2012
Agenda
 Automated Testing overview
 Selenium
 Page Object ...
Commercial In Confidence & Proprietary, Kainos Software 2012
Automated Testing overview
16-Apr-15 3
• Reduces amount of ma...
Commercial In Confidence & Proprietary, Kainos Software 2012
Selenium
16-Apr-15 4
• Is Selenium a test framework? Nope!
• ...
Selenium Basic Concepts
IWebDriver instance lets you control the browser
ChromeDriver, InternetExplorerDriver, FirefoxDriv...
Page Object pattern
Create model of UI pages as objects
Reduces the amount of duplicated code
If you change anything in UI...
Page Object pattern -
summary
Generally don't make assertions – these should be done in the test,
not in the Page Object
N...
Page Object pattern -
example
public class LoginPage
{
private IWebDriver Driver { get; set; }
private IWebElement UserNam...
Selenium – support of
“Page Object pattern”
PageFactory lets you automatically initialize elements defined in
Page Object ...
Selenium – support of
“Page Object pattern”
public class LoginPage
{
private IWebDriver Driver { get; set; }
[FindsBy(How ...
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. R...
Questions?
16-Apr-15 13
Upcoming SlideShare
Loading in …5
×

Marcin Wasilczyk - Page objects with selenium

573 views

Published on

III meeting of Trójmiejska Grupa Testerska - testers community in tri-city area, Poland.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Marcin Wasilczyk - Page objects with selenium

  1. 1. Page Objects with Selenium WebDriver Trójmiejska Grupa Testerska Spotkanie #3 Marcin Wasilczyk, Oleksandr Perepelytsya 16-Apr-15 1
  2. 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. 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. 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. 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. 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. 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. 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. 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. 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
  11. 11. Demo 16-Apr-15 11
  12. 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
  13. 13. Questions? 16-Apr-15 13

×