43
AUTOMATION TESTS - LITTLE BIG ADVENTURE WITH SELENIUM WEBDRIVER WARSZAWQA#10

WARSZAWQA#10. Author: Michal Lamperski Contact: [email protected] [email protected] Find me on LinkedIn

Embed Size (px)

Citation preview

Page 1: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

AUTOMATION TESTS

- LITTLE BIG ADVENTURE WITH SELENIUM

WEBDRIVER

WARSZAWQA#10

Page 2: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Author: Michal Lamperski Contact:

[email protected] Find me on LinkedIn

Page 3: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  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

Page 4: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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

Page 5: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 6: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Test Pyramid

Page 7: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Hump of pain

Page 8: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Language

Universal rule:For tests use language used in

application.

C# Java Python ... If Angular used-> Protractor

Page 9: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 10: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Selenium 1 (aka. Selenium Remote Control)

Selenium RC was the main Selenium project for a long time, before the WebDriver/Seleniummerge brought up Selenium 2.

Now Selenium 1 is deprecated and is not actively supported (mostly in maintenance mode)

Page 11: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 12: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 13: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Selenium IDE

Page 14: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 15: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 16: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Selenium WebDriver

Page 17: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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...

Page 18: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

WE DON’T CARE !!!

Page 19: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Selenium Grid

Running tests in parallel:

... maybe next time.

Page 20: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

3. Main methodsProgramming basics:

Page 21: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Main methodshttps://selenium.googlecode.com/git/docs/api/dotnet/index.html

Close Close 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.

Manage Instructs the driver to change its settings.

Navigate Instructs the driver to navigate the browser to another location.

Quit Quits this driver, closing every associated window.

SwitchTo Instructs the driver to send future commands to a different frame or window

Page 22: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Main methodsWebDriver:

New instances with options / switches;Close();Quit();

For Navigate():GoToUrl();

For FindElement():Clear();Click();SendKeys();Submit();

For Manage():SetPageLoadTimeout();ImplicitlyWait();SetScriptTimeout()

JavaScriptExecutorExecuteScript();

Page 23: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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;

}

Page 24: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

LocatorsSelenium WebDriver uses 8 locators to find the elements on web page: Id Gets a mechanism to find elements by their ID. Name Gets a mechanism to find elements by their name. Linktext Gets a mechanism to find elements by their link text. Partial Linktext Gets a mechanism to find elements by a partial

match on their link text. Tag Name Gets a mechanism to find elements by their tag name. Class name Gets a mechanism to find elements by their CSS class. CssSelector Gets a mechanism to find elements by their cascading

style sheet (CSS) selector. Xpath Gets a mechanism to find elements by an XPath query.

https://selenium.googlecode.com/git/docs/api/dotnet/index.html

Page 25: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Similar unit-testing framework: - SpecFlow,

- MSTest,

- Full list available at: https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks

Page 26: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Attributes Source: http://www.nunit.org/index.php?p=attributes&r=2.2

[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.

Page 27: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Assertions http://www.nunit.org/index.php?p=assertions&r=2.2

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.

Page 28: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn
Page 29: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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 30: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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 31: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Page Object Example

Page 32: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn
Page 33: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn
Page 34: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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

Page 35: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

LET’S CHECK IT ON CODE...

Page 36: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

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.

Page 37: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Continuos Integration

Page 38: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Continuos Integration

Page 39: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

HOW IT REALLY LOOKS...

Page 40: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

7. Additional linksTools: https://www.browserstack.com/ https://dev.modern.ie/tools/vms/windows https://github.com/ministryotech/webdriver

Tutorials: http://www.toolsqa.com/selenium-webdriver-tutorials-in-c-selenium-tutorial-in-c/ http://relevantcodes.com/using-nunit-to-execute-selenium-webdriver-tests/ http://mylearnings.net/

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 http://www.fromdev.com/2015/02/selenium-webdriver-books.html https://www.quora.com/What-is-the-best-book-to-learn-Selenium

Page 41: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

8. Sources: http://www.seleniumhq.org/ http://www.w3.org/TR/2013/WD-webdriver-20130117/ http://martinfowler.com/bliki/PageObject.html http://

www.talkingtree.com/blog/index.cfm/2012/5/15/Automated-System-Testing-for-Web-Apps-at-CFObjective

Jasiński Łukasz, „7 essential principles for automated functional tests”, WrotQA, 2014

http://www.slideshare.net/humayunrana786/test-a-27965660 http://www.slideshare.net/orenrubin/page-objects-presentation-

selenium-conference-2014-38767492?related=1 https

://solutionscafe.wordpress.com/2014/01/06/page-object-pattern/ http://blog.yauheni.me/2009_11_01_archive.html http://watirmelon.com/2012/06/ http://qxf2.com/blog/page-object-model-selenium-python/ http://www.agilecoachjournal.com

Page 42: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

9. Q&A

Page 43: WARSZAWQA#10.  Author: Michal Lamperski  Contact: michal.lamperski@gmail.com michal.lamperski@gmail.com  Find me on LinkedIn

Thank you for your participation!