WARSZAWQA#10
Author: Michal Lamperski Contact: Find me on LinkedIn
Agenda: 1. Why UI Tests? 2. Selenium / Webdriver intro 3. Main methods, Locators, Waits 4. Nunit 5. Page Object Pattern 6. Continuous Integration – Teamcity 7. Additional links 8. Sources 9. Q&A
1. Why to automate? Test automation has specific advantages for improving the long-term efficiency of a software team’s testing processes. Test automation supports: Frequent regression testing Rapid feedback to developers Virtually unlimited iterations of test case execution Support for Agile and extreme development methodologies Disciplined documentation of test cases Customized defect reporting Finding defects missed by manual testing
Why not to automate? It is not always advantageous to automate test cases. There are times when manual testing may be more appropriate. For instance, if the application’s user interface will change considerably in the near future, then any automation might need to be rewritten anyway. Also, sometimes there simply is not enough time to build test automation. For the short term, manual testing may be more effective. If an application has a very tight deadline, there is currently no test automation available, and it’s imperative that the testing get done within that time frame, then manual testing is the best solution.
Test Pyramid
Hump of pain
Language Universal rule: For tests use language used in application. C# Java Python ... If Angular used-> Protractor
2. Selenium – brief history Selenium first came to life in 2004 by Jason Huggins. He developed a Javascript library that could drive interactions with the page, allowing him to automatically rerun tests against multiple browsers. That library eventually became Selenium Core, which underlies all the functionality of Selenium Remote Control (RC) and Selenium IDE. Selenium RC allows to control a browser from a language of your choice. ISSUESS: Different things became impossible to do because of its Javascript based automation engine and the security limitations. Webapps became more powerful over time making this restrictions more painful.
Selenium 1 (aka. Selenium Remote Control) Selenium RC was the main Selenium project for a long time, before the WebDriver/Selenium merge brought up Selenium 2. Now Selenium 1 is deprecated and is not actively supported (mostly in maintenance mode)
Selenium Remote Control Selenium RC comes in two parts. -> A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them. -> Client libraries for chosen language.
Selenium IDE Selenium IDE (Integrated Development Environment) is a prototyping tool for building test scripts. It is a Firefox plugin and provides an easy-to-use interface for developing automated tests. Selenium IDE has a recording feature, which records user actions as they are performed and then exports them as a reusable script in one of many programming languages that can be later executed. CONS: Available only in Firefox Designed only to create the prototypes of tests No support for iteration and conditional operations Test execution is slow compared to that of Selenium RC and WebDriver.
Selenium IDE
Selenium 2 (aka. Selenium WebDriver) The WebDriver API is primarily intended to allow developers to write tests that automate a browser from a separate controlling process. Selenium and WebDriver developers agreed that both tools have advantages and that merging the two projects would make a much more robust automation tool. Selenium 2.0 supports the WebDriver API and underlying technology, along with the Selenium 1 technology underneath the WebDriver API for maximum flexibility in porting your tests. In addition, Selenium 2 still runs Selenium 1’s Selenium RC interface for backwards compatibility.
Selenium WebDriver Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.
Selenium WebDriver
A different way of automating the browser. Create a browser-specific driver to control the browser directly and have to do this for each browser. Object oriented API Doesn’t need a real browser. No Javascript limitations No need for a server. CONS ??? Maybe there are...
WE DON’T CARE !!!
Selenium Grid Running tests in parallel: ... maybe next time.
3. Main methods Programming basics:
Main methods CloseClose the current window, quitting the browser if it is the last window currently open. Dispose Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. FindElement Finds the first IWebElement using the given method. FindElements Finds all IWebElements within the current context using the given mechanism. ManageInstructs the driver to change its settings. NavigateInstructs the driver to navigate the browser to another location. QuitQuits this driver, closing every associated window. SwitchToInstructs the driver to send future commands to a different frame or window
Main methods WebDriver: New instances with options / switches; Close(); Quit(); For Navigate(): GoToUrl(); For FindElement(): Clear(); Click(); SendKeys(); Submit(); For Manage(): SetPageLoadTimeout(); ImplicitlyWait(); SetScriptTimeout() JavaScriptExecutor ExecuteScript();
Waits ITimeouts ImplicitlyWait(TimeSpan timeToWait); Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. ITimeouts SetPageLoadTimeout(TimeSpan timeToWait); Specifies the amount of time the driver should wait for a page to load when setting the OpenQA.Selenium.IWebDriver.Url property. ITimeouts SetScriptTimeout(TimeSpan timeToWait); Specifies the amount of time the driver should wait when executing JavaScript asynchronously. Tip: while (true) { var ajaxIsComplete = (bool)(Driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"); if (ajaxIsComplete) break; }
Locators Selenium WebDriver uses 8 locators to find the elements on web page: IId Gets a mechanism to find elements by their ID. NName Gets a mechanism to find elements by their name. LLinktext Gets a mechanism to find elements by their link text. PPartial Linktext Gets a mechanism to find elements by a partial match on their link text. TTag Name Gets a mechanism to find elements by their tag name. CClass name Gets a mechanism to find elements by their CSS class. CCssSelector Gets a mechanism to find elements by their cascading style sheet (CSS) selector. XXpath Gets a mechanism to find elements by an XPath query.
4. NUNIT NUnit is a unit-testing framework for all.Net languages. Initially ported from JUnit, the current production release, version 2.6, is the seventh major release of this xUnit based unit testing tool for Microsoft.NET.JUnit Similar unit-testing framework: - SpecFlow, - MSTest, - Full list available at: rameworks rameworks
Attributes Source: [TestFixture] – marks a class that contains tests and, optionally, setup or teardown methods. [SetUp] – used to provide a common set of functions that are performed just before each test method is called. [Test] – marks a method inside a TestFixture class as a test. [TestCase] – serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method. [TearDown] – used to provide a common set of functions that are performed after each test method is run. [Catalog] - attribute provides an alternative to suites for dealing with groups of tests. [Ignore] - an attribute to not run a test or test fixture for a period of time.
Assertions Comparisons Assertions that perform comparisons are often your best choice because they report both expected and actual values. The expected value is always the first argument. The AreSame method tests that the same objects are referenced by both arguments. All the variants of AreEqual test for equality. Condition Tests Methods that test a specific condition are named for the condition they test and take the value tested as their first argument and, optionally a message as the second. Assert.IsTrue, Assert.IsFalse; Assert.IsNull, Assert.IsNotNull; Utility Methods The Assert.Fail method provides you with the ability to generate a failure based on tests that are not encapsulated by the other methods. It is also useful in developing your own project-specific assertions. The Assert.Ignore method provides you with the ability to dynamically cause a test or suite to be ignored at runtime. It may be called in a test, setup or fixture setup method.
5. Page Object Pattern A Page Object is a object oriented class that serves as an interface to a page of AUT (Application Under Test). If UI change, tests don’t need to be change, only the code within the page object need to be changed. 1. There is clean separation between test code and page specific code such as locators and layout. 2. There is single repository for the services or operations offered by the page rather than having these services scattered through out the tests.
Page Object Pattern Each page is defined as it’s own class !!! Actions (including navigation) are represented as functions for a class. Each function returns a new Page object, signifying what page the actions stops on. Tests only talk to the page objects. Page objects only talk to the driver. Elements on the page are stored as variables for the page object Tests become a string of well defined functions, not meaningless gibberish. Class Inheritance can be used to define functionality to a set of pages.
Page Object Example
RULES TO FOLLOW: #1: DESIGN FIRST #2: DO NOT AUTOMATE EVERYTHING #3: WRITE SHORT TESTS #4: CREATE INDEPENDENT TESTS #5: FOCUS ON READABILITY #6: TESTS MUST BE QUICK #7: TESTS RESISTANT TO CHANGE
LET’S CHECK IT ON CODE...
6. Continuos Integration Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.
Continuos Integration
HOW IT REALLY LOOKS...
7. Additional links Tools: Tutorials: Books: Burns D., „Selenium 2 Testing Tools Beginner's Guide”, 2012, Packt publishing Crispin L., Gregory J., „Agile Testing: A Practical Guide for Testers and Agile Teams”, Pearson Education,2008
8. Sources: d-System-Testing-for-Web-Apps-at-CFObjective d-System-Testing-for-Web-Apps-at-CFObjective Jasiński Łukasz, „7 essential principles for automated functional tests”, WrotQA, 2014 presentation-selenium-conference ?related=1 presentation-selenium-conference ?related=1 pattern/ pattern/
9. Q&A
Thank you for your participation!