marcin wasilczyk - page objects with selenium
Post on 18-Jul-2015
90 Views
Preview:
TRANSCRIPT
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 -
summaryGenerally 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 -
examplepublic 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 page16-Apr-15 12
Questions?
16-Apr-15 13
top related