Slide 1

Slide 1 text

Intro To Selenium by Jonathan Clarkin Twitter @_jrwc http://about.me/jclarkin

Slide 2

Slide 2 text

THE FOLLOWING PRESENTATION HAS BEEN RATED TESTERS STRONGLY CAUTIONED PG-13 SOME MATERIAL MAY BE INAPPROPRIATE FOR CHILDREN UNDER 13 CODING SKILLS WEB AUTOMATION LEARNING & GROWTH Twitter @_jrwc http://about.me/jclarkin

Slide 3

Slide 3 text

Introductions

Slide 4

Slide 4 text

- Name - Current Job Role - Previous Job Experience - Experience with Test Automation - Favorite TV Show Introductions Jonathan Clarkin | @_jrwc | #KWSQA

Slide 5

Slide 5 text

- Software Engineer - Team Leader - Visual Designer - Mobile Developer - Software Architect Introductions: Me Jonathan Clarkin | @_jrwc | #KWSQA - Test Engineer - Automation Tools Researcher - …

Slide 6

Slide 6 text

Introductions: Me Jonathan Clarkin | @_jrwc | #KWSQA

Slide 7

Slide 7 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more ! Jonathan Clarkin | @_jrwc | #KWSQA Objectives

Slide 8

Slide 8 text

- Objectives at the beginning - Ask Questions - Ask for Help - Answer Questions - Slides will be available after the workshop Workshop Fundamentals Jonathan Clarkin | @_jrwc | #KWSQA

Slide 9

Slide 9 text

- Coding Experience Level: Beginner - Automation Experience Level: Beginner - It’s OK to leave (and come back) Minimize disruptions Setting Expectations Jonathan Clarkin | @_jrwc | #KWSQA

Slide 10

Slide 10 text

Overview

Slide 11

Slide 11 text

Why Automate? Jonathan Clarkin | @_jrwc | #KWSQA

Slide 12

Slide 12 text

To facilitate testing by ... - detecting changes in a product (via checks) - reducing effort on repetitive activities - eliminating human error setting up data - minimizing time intensive tasks Why Automate? Jonathan Clarkin | @_jrwc | #KWSQA

Slide 13

Slide 13 text

It only does what we tell it to do Automation Is not Smart Jonathan Clarkin | @_jrwc | #KWSQA

Slide 14

Slide 14 text

Automation Is not Smart Jonathan Clarkin | @_jrwc | #KWSQA

Slide 15

Slide 15 text

Why Automate? Testers Need To Code? Jonathan Clarkin | @_jrwc | #KWSQA

Slide 16

Slide 16 text

Nope But it is another useful skill in our tool-belts :) Why Automate? Testers Need To Code? Jonathan Clarkin | @_jrwc | #KWSQA

Slide 17

Slide 17 text

Why Automate? Areas of Testing Jonathan Clarkin | @_jrwc | #KWSQA (credit: Brian Marick, Lisa Crispin)

Slide 18

Slide 18 text

Why Automate? Front-End Automation is Costly Jonathan Clarkin | @_jrwc | #KWSQA

Slide 19

Slide 19 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Testers Need To Code? Objectives Jonathan Clarkin | @_jrwc | #KWSQA

Slide 20

Slide 20 text

A fancy application, like Notepad/TextEdit… but Designed to make coding in Java easier (thank you content assist) What is Eclipse Jonathan Clarkin | @_jrwc | #KWSQA

Slide 21

Slide 21 text

A Java framework designed for creating and reporting on checks - Test runner to execute checks - Collection of assertion rules - Results formatter What is JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 22

Slide 22 text

An automation framework for interacting with browsers and the web - Created in 2004 by ThoughtWorks - Open Source and Popular - Cross browser and cross platform What is Selenium WebDriver Jonathan Clarkin | @_jrwc | #KWSQA

Slide 23

Slide 23 text

Setup

Slide 24

Slide 24 text

- Firefox - Chrome - Safari - Internet Explorer 10+ Modern Browser Jonathan Clarkin | @_jrwc | #KWSQA

Slide 25

Slide 25 text

We’ll be using Java 7 Download URL: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html Java

Slide 26

Slide 26 text

We’ll be using Java 7 Download URL: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html Beware... “Ask Toolbar” Java Jonathan Clarkin | @_jrwc | #KWSQA

Slide 27

Slide 27 text

From your console | terminal | shell run: java -version TEST IT

Slide 28

Slide 28 text

Download and install Eclipse Standard 4.4 Download URL: https://www.eclipse.org/downloads/packages/eclipse-standard-44/lunar Eclipse Jonathan Clarkin | @_jrwc | #KWSQA

Slide 29

Slide 29 text

Try to launch Eclipse TEST IT

Slide 30

Slide 30 text

Get a copy of Selenium WebDriver for Java (Zip contains JUnit) Download URL: http://docs.seleniumhq.org/download/ Selenium & JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 31

Slide 31 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 32

Slide 32 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 33

Slide 33 text

1. Unzip Selenium Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 34

Slide 34 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 35

Slide 35 text

1. Unzip Selenium 2. Launch Eclipse Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 36

Slide 36 text

1. Unzip Selenium 2. Launch Eclipse Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 37

Slide 37 text

1. Unzip Selenium 2. Launch Eclipse Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 38

Slide 38 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 39

Slide 39 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 40

Slide 40 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 41

Slide 41 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 42

Slide 42 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 43

Slide 43 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 44

Slide 44 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 45

Slide 45 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Add all Selenium Jars ● The two JARs in the root folder ● All in the JARs in the /libs/ subfolder Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 46

Slide 46 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 47

Slide 47 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 48

Slide 48 text

1. Unzip Selenium 2. Launch Eclipse 3. Create a new Project 4. Add External JARs Done! Setup Eclipse + Selenium + JUnit Jonathan Clarkin | @_jrwc | #KWSQA

Slide 49

Slide 49 text

TEST IT

Slide 50

Slide 50 text

Drag into your project (copy) BasicScenarioTest.java Ensure it is in the /src/ folder TEST IT

Slide 51

Slide 51 text

Right-Click on the file and select Run As > JUnit Test (detailed explanation on running tests will be covered later) TEST IT

Slide 52

Slide 52 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Jonathan Clarkin | @_jrwc | #KWSQA Objectives

Slide 53

Slide 53 text

Java & JUnit

Slide 54

Slide 54 text

Sample Test Case Jonathan Clarkin | @_jrwc | #KWSQA

Slide 55

Slide 55 text

Classes

Slide 56

Slide 56 text

A class is a block of code used to encapsulate a bunch of: - Variables - Structures - Methods Classes Jonathan Clarkin | @_jrwc | #KWSQA

Slide 57

Slide 57 text

Classes: Create New Jonathan Clarkin | @_jrwc | #KWSQA

Slide 58

Slide 58 text

Classes Example Jonathan Clarkin | @_jrwc | #KWSQA

Slide 59

Slide 59 text

Classes Example Matches the folder structure Jonathan Clarkin | @_jrwc | #KWSQA

Slide 60

Slide 60 text

Classes Example Matches the filename Matches the folder structure Jonathan Clarkin | @_jrwc | #KWSQA

Slide 61

Slide 61 text

Classes Example Matches the folder structure Matches the filename Everything else is part of the Java language conventions Jonathan Clarkin | @_jrwc | #KWSQA

Slide 62

Slide 62 text

Classes Example Matches the folder structure Matches the filename Everything else is part of the Java language conventions Purple signifies special reserved words Jonathan Clarkin | @_jrwc | #KWSQA

Slide 63

Slide 63 text

Classes Example Matches the folder structure Matches the filename Everything else is part of the Java language conventions Purple signifies special reserved words If there is a // in front of a line, it is a comment zone Jonathan Clarkin | @_jrwc | #KWSQA

Slide 64

Slide 64 text

Create a new Class called MyFirstCheck TEST IT

Slide 65

Slide 65 text

Methods

Slide 66

Slide 66 text

A method is a function: a small chunk of executable instructions Methods Jonathan Clarkin | @_jrwc | #KWSQA

Slide 67

Slide 67 text

Methods Jonathan Clarkin | @_jrwc | #KWSQA

Slide 68

Slide 68 text

Methods A method ! Jonathan Clarkin | @_jrwc | #KWSQA

Slide 69

Slide 69 text

Methods A method ! It should have a memorable unique name Jonathan Clarkin | @_jrwc | #KWSQA

Slide 70

Slide 70 text

Methods It should have a memorable unique name This specifies whether other classes can see the logic public vs. private (for us: always public) A method ! Jonathan Clarkin | @_jrwc | #KWSQA

Slide 71

Slide 71 text

Methods This specifies whether other classes can see the logic public vs. private (for us: always public) This specifies whether the logic gives back an answer (for our tests, they will all be void) A method ! It should have a memorable unique name Jonathan Clarkin | @_jrwc | #KWSQA

Slide 72

Slide 72 text

Methods A method ! It should have a memorable unique name This specifies whether other classes can see the logic public vs. private (for us: always public) This specifies whether the logic gives back an answer (for our tests, they will all be void) All logic goes here Jonathan Clarkin | @_jrwc | #KWSQA

Slide 73

Slide 73 text

Create a method named checkForBugs() TEST IT

Slide 74

Slide 74 text

Test Methods

Slide 75

Slide 75 text

Test Methods Only real difference is this: @Test Jonathan Clarkin | @_jrwc | #KWSQA

Slide 76

Slide 76 text

Test Methods Only real difference is this: @Test import is a way to get access to code in other files. In this case, we’re using the magic of the Test class Jonathan Clarkin | @_jrwc | #KWSQA

Slide 77

Slide 77 text

Test Methods Only real difference is this: @Test import is a way to get access to code in other files. In this case, we’re using the magic of the Test class You can have multiple tests in the same file Jonathan Clarkin | @_jrwc | #KWSQA

Slide 78

Slide 78 text

Make your method into a test @Test TEST IT

Slide 79

Slide 79 text

Assertions

Slide 80

Slide 80 text

Assertions This import gets us access to Asserts Jonathan Clarkin | @_jrwc | #KWSQA

Slide 81

Slide 81 text

Assertions This import gets us access to Asserts This is an assert. In this example we are checking that two things are equal. The value on the left is our expected value, the one of the right is the calculated. Jonathan Clarkin | @_jrwc | #KWSQA

Slide 82

Slide 82 text

Assertions This import gets us access to Asserts This is an assert. In this example we are checking that two things are equal. The value on the left is our expected value, the one of the right is the calculated. It’s ok if the expected and actual values don’t match. This is when our automation will report to us something is not right. Jonathan Clarkin | @_jrwc | #KWSQA

Slide 83

Slide 83 text

Add an assertion assertTrue( 42 > 100 ); TEST IT

Slide 84

Slide 84 text

Running Tests

Slide 85

Slide 85 text

Running Tests Jonathan Clarkin | @_jrwc | #KWSQA

Slide 86

Slide 86 text

Running Tests Jonathan Clarkin | @_jrwc | #KWSQA

Slide 87

Slide 87 text

Runs all tests in the Class file Three possible results ● Pass ● Fail ● Error Running Tests Jonathan Clarkin | @_jrwc | #KWSQA

Slide 88

Slide 88 text

Three possible results ● Pass No problems found ● Fail Expectation did not happen ● Error Something unexpected occurred Running Tests Jonathan Clarkin | @_jrwc | #KWSQA

Slide 89

Slide 89 text

State of the set of tests clearly highlighted by a large colourful bar Running Tests Jonathan Clarkin | @_jrwc | #KWSQA

Slide 90

Slide 90 text

What do you predict will be the result of our assertion? Try running it. Did the results match your expectations? TEST IT

Slide 91

Slide 91 text

Strings

Slide 92

Slide 92 text

A String is a bit of text, that can be stored for later reference. E.g. Store some data, as type String, using an id name of ‘person’ String person = “Jonathan”; Strings Jonathan Clarkin | @_jrwc | #KWSQA

Slide 93

Slide 93 text

A String is a bit of text, that can be stored for later reference. E.g. Store some data, as type String, using an id name of ‘person’ String person = “Jonathan”; Strings Store some data Using an id name of person as type String Jonathan Clarkin | @_jrwc | #KWSQA

Slide 94

Slide 94 text

Add a String to your check TEST IT

Slide 95

Slide 95 text

JUnit Methods

Slide 96

Slide 96 text

Other than @Test, the key ones are: @Before @After @BeforeClass @AfterClass JUnit Methods Jonathan Clarkin | @_jrwc | #KWSQA

Slide 97

Slide 97 text

Other than @Test, the key ones are: @Before Method to run before each test @After Method to run after each test @BeforeClass @AfterClass JUnit Methods Jonathan Clarkin | @_jrwc | #KWSQA

Slide 98

Slide 98 text

Other than @Test, the key ones are: @Before Method to run before each test @After Method to run after each test @BeforeClass @AfterClass JUnit Methods Run Class Before commands Test A After commands Before commands Test B After commands End running Class Jonathan Clarkin | @_jrwc | #KWSQA

Slide 99

Slide 99 text

Other than @Test, the key ones are: @Before Method to run before each test @After Method to run after each test @BeforeClass @AfterClass JUnit Methods Why ? To create common commands once A Selenium Example might be.. - to login a user before each test - to logout the user after each test Jonathan Clarkin | @_jrwc | #KWSQA

Slide 100

Slide 100 text

Other than @Test, the key ones are: @Before Method to run before each test @After Method to run after each test @BeforeClass Method to run once before all tests @AfterClass Method to run once after all tests JUnit Methods Jonathan Clarkin | @_jrwc | #KWSQA

Slide 101

Slide 101 text

Other than @Test, the key ones are: @Before Method to run before each test @After Method to run after each test @BeforeClass Method to run once before all tests @AfterClass Method to run once after all tests JUnit Methods Run Class Before Class commands Test A Test B After Class commands End running Class Jonathan Clarkin | @_jrwc | #KWSQA

Slide 102

Slide 102 text

Other than @Test, the key ones are: @Before Method to run before each test @After Method to run after each test @BeforeClass Method to run once before all tests @AfterClass Method to run once after all tests JUnit Methods Why ? To create common commands once A Selenium Example might be.. - to open up a browser window once - to close the browser once complete Jonathan Clarkin | @_jrwc | #KWSQA

Slide 103

Slide 103 text

Add @before and @after methods to your class Have them print to the console System.out.println(“Something”); TEST IT

Slide 104

Slide 104 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Jonathan Clarkin | @_jrwc | #KWSQA Objectives

Slide 105

Slide 105 text

Selenium Basics

Slide 106

Slide 106 text

Open a browser Go somewhere nice (url) Fill in form fields Click some things Assert that our expectations match reality Close the browser Our mission... Jonathan Clarkin | @_jrwc | #KWSQA

Slide 107

Slide 107 text

Commands differ based on choice of browser. Firefox is the Selenium default choice WebDriver driver = new FirefoxDriver(); After this command executes, Firefox should open Mission: Open the Browser Jonathan Clarkin | @_jrwc | #KWSQA

Slide 108

Slide 108 text

TEST IT

Slide 109

Slide 109 text

Tell the driver which URL to visit driver.get( “http://en.wikipedia.org” ); After which, the browser will load to requested page Mission: Go Somewhere Nice Jonathan Clarkin | @_jrwc | #KWSQA

Slide 110

Slide 110 text

TEST IT

Slide 111

Slide 111 text

Find a form field on the page WebElement searchfield = driver.findElement( By.id(“searchInput”) ); We now have access to this bit of the page. We can interact with it (click, type, etc). Mission: Fill in Form Fields Jonathan Clarkin | @_jrwc | #KWSQA

Slide 112

Slide 112 text

Find the get button and click it searchfield.sendKeys( “The Matrix\n” ); Mission: Fill in Form Fields Jonathan Clarkin | @_jrwc | #KWSQA

Slide 113

Slide 113 text

TEST IT

Slide 114

Slide 114 text

Assert the page title is what we expect String title = driver.getTitle(); assertEquals( title, “The Matrix” ); Mission: Assert Expectations Jonathan Clarkin | @_jrwc | #KWSQA

Slide 115

Slide 115 text

TEST IT

Slide 116

Slide 116 text

driver.quit(); Mission: Close the Browser Jonathan Clarkin | @_jrwc | #KWSQA

Slide 117

Slide 117 text

TEST IT

Slide 118

Slide 118 text

☑ Open a browser ☑ Go somewhere nice ☑ Fill in form fields ☑ Click some things ☑ Assert that our expectations match reality ☑ Close the browser Our mission... Jonathan Clarkin | @_jrwc | #KWSQA

Slide 119

Slide 119 text

Jonathan Clarkin | @_jrwc | #KWSQA Our mission...

Slide 120

Slide 120 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Jonathan Clarkin | @_jrwc | #KWSQA Objectives

Slide 121

Slide 121 text

Browser Tools

Slide 122

Slide 122 text

The browser dev-tools are your friend to finding out how to uniquely identify the elements on a web page. Dev Tools Jonathan Clarkin | @_jrwc | #KWSQA

Slide 123

Slide 123 text

Accessing the Dev Tools Exact menu option depends on the browser Jonathan Clarkin | @_jrwc | #KWSQA

Slide 124

Slide 124 text

Element Selector Click the selector tool Jonathan Clarkin | @_jrwc | #KWSQA

Slide 125

Slide 125 text

Element Selector Click on an element of the page to select it Jonathan Clarkin | @_jrwc | #KWSQA

Slide 126

Slide 126 text

With the HTML shown, we can pick how best to identify our element: id class xpath Finding Ids, Class Names, and more Jonathan Clarkin | @_jrwc | #KWSQA

Slide 127

Slide 127 text

Go to wikipedia Use the Element Selector See the HTML for the search field TEST IT

Slide 128

Slide 128 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Jonathan Clarkin | @_jrwc | #KWSQA Objectives

Slide 129

Slide 129 text

Selectors

Slide 130

Slide 130 text

There are many ways to grab parts of the web page: - Id - Class - Name - Link Text - XPath Selectors Jonathan Clarkin | @_jrwc | #KWSQA

Slide 131

Slide 131 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave Sample DOM Jonathan Clarkin | @_jrwc | #KWSQA

Slide 132

Slide 132 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.id( “eric_h_jung” )); Select By Id Jonathan Clarkin | @_jrwc | #KWSQA

Slide 133

Slide 133 text

On Wikipedia Using Dev Tools Go to Console tab and type... $(‘#’) (replace with the search field id) TEST IT

Slide 134

Slide 134 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.className( “human” )); Select By Class Jonathan Clarkin | @_jrwc | #KWSQA

Slide 135

Slide 135 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.className( “youth” )); Select By Class Jonathan Clarkin | @_jrwc | #KWSQA

Slide 136

Slide 136 text

Using Dev Tools Console, type $(‘input’) What happened and why? TEST IT

Slide 137

Slide 137 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.name( “address” )); Select By Name Jonathan Clarkin | @_jrwc | #KWSQA

Slide 138

Slide 138 text

Using Dev Tools find the name of the wiki search field TEST IT

Slide 139

Slide 139 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.partialLinkText( “321 South” )); Select By Partial Link Text Jonathan Clarkin | @_jrwc | #KWSQA

Slide 140

Slide 140 text

Select By Link Text 321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.linkText( “321 South Ave” )); Jonathan Clarkin | @_jrwc | #KWSQA

Slide 141

Slide 141 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “/people” )); Means element Means top level Select By XPath Jonathan Clarkin | @_jrwc | #KWSQA

Slide 142

Slide 142 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “/people/person” )); Means second level Select By XPath Jonathan Clarkin | @_jrwc | #KWSQA

Slide 143

Slide 143 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “/people/person[1]” )); Means first match Select By XPath Jonathan Clarkin | @_jrwc | #KWSQA

Slide 144

Slide 144 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “/people/person[2]” )); Means second match Select By XPath Jonathan Clarkin | @_jrwc | #KWSQA

Slide 145

Slide 145 text

Select By XPath 321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “//person[2]” )); Means any depth Jonathan Clarkin | @_jrwc | #KWSQA

Slide 146

Slide 146 text

Using Dev Tools Console $x(“”) retrieve all inputs on the wiki homepage TEST IT

Slide 147

Slide 147 text

321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “//href[@city=’denver’]” )); @ means attribute Select By XPath Jonathan Clarkin | @_jrwc | #KWSQA

Slide 148

Slide 148 text

Select By XPath 321 South St 123 Main St 321 North St 123 West St 321 South Ave driver.findElements(By.xpath( “//href[@city=’denver’]/..” )); @ means up one level Jonathan Clarkin | @_jrwc | #KWSQA

Slide 149

Slide 149 text

Review of ways to grab parts of the page - Id driver.findElement( By.id() ); - Class driver.findElement( By.className() ); - Name driver.findElement( By.name() ); - Link Text driver.findElement( By.linkText() ); driver.findElement( By.partialLinkText() ); - XPath driver.findElement( By.xpath() ); Selectors Jonathan Clarkin | @_jrwc | #KWSQA

Slide 150

Slide 150 text

Least to most brittle selectors (likely to be broken as software evolves) - Id - Class - Name - Link Text - XPath Selectors Jonathan Clarkin | @_jrwc | #KWSQA

Slide 151

Slide 151 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Jonathan Clarkin | @_jrwc | #KWSQA Objectives

Slide 152

Slide 152 text

Checking with Asserts

Slide 153

Slide 153 text

Here is a list of common asserts you can use to verify your expectations against the actual webpage results Asserts Jonathan Clarkin | @_jrwc | #KWSQA

Slide 154

Slide 154 text

Asserts Statement Description fail( message ) Cause the test to immediately fail, reporting the provided String as message assertTrue( message, condition ) Checks that the condition is true, and reports the message if instead it is false. assertFalse ( message, condition ) Checks that the condition is false, and reports the message if instead it is true. assertEquals( message, expected, actual ) Checks that the expected value matches the actual, or reports the message otherwise Be sure to provide a useful message for when checks fail. This will be the first source of information for diagnostics Jonathan Clarkin | @_jrwc | #KWSQA

Slide 155

Slide 155 text

TEST IT

Slide 156

Slide 156 text

Looping

Slide 157

Slide 157 text

Sometimes we need to perform some logic on a collections of possible elements driver.findElements(By.className(“div”)); This command gets more than one element Looping a little Jonathan Clarkin | @_jrwc | #KWSQA

Slide 158

Slide 158 text

Using a ‘for-loop’ we can investigate each one for(WebElement elem : driver.findElements(By.className(“div”)) ) { // Do something with the specific ‘elem’ element } Looping a little Jonathan Clarkin | @_jrwc | #KWSQA

Slide 159

Slide 159 text

We can check aspects of each one for(WebElement elem : driver.findElements(By.className(“div”)) ) { if(elem.getText().equals( “Neo” ) break; } The ‘break’ command quits the loop early Looping a little Jonathan Clarkin | @_jrwc | #KWSQA

Slide 160

Slide 160 text

TEST IT

Slide 161

Slide 161 text

Multiple Windows

Slide 162

Slide 162 text

Each window has a ‘handle’ to access it String currentWindow = driver.getWindowHandle(); Popups - How to Swap Windows Jonathan Clarkin | @_jrwc | #KWSQA

Slide 163

Slide 163 text

The driver has a list of all window handles driver.getWindowHandles(); Popups - How to Swap Windows Jonathan Clarkin | @_jrwc | #KWSQA

Slide 164

Slide 164 text

We can loop over all window handles and change the current window for( String handle : driver.getWindowHandles()) { driver.switchTo().window( handle ); } Popups - How to Swap Windows Jonathan Clarkin | @_jrwc | #KWSQA

Slide 165

Slide 165 text

As we loop, we can check the window title and stop looping once we find the right one if( driver.getTitle().equals( “The Matrix” ) ) { break; // This means stop looping immediately } Popups - How to Swap Windows Jonathan Clarkin | @_jrwc | #KWSQA

Slide 166

Slide 166 text

Once looping is done, it is wise to confirm that we are on the desired page assertTrue( driver.getTitle().equals( "The Matrix" ) ); Popups - How to Swap Windows Jonathan Clarkin | @_jrwc | #KWSQA

Slide 167

Slide 167 text

Popups - All Together Jonathan Clarkin | @_jrwc | #KWSQA

Slide 168

Slide 168 text

TEST IT

Slide 169

Slide 169 text

Page Objects

Slide 170

Slide 170 text

A Page Object is pattern used to create more maintainable Selenium tests Page Objects Jonathan Clarkin | @_jrwc | #KWSQA

Slide 171

Slide 171 text

A Page Object is a model of a single page of a website. ● It exposes the page interactions ● It hides the volatile selectors Page Objects Jonathan Clarkin | @_jrwc | #KWSQA

Slide 172

Slide 172 text

public class LandingPage { private WebDriver driver; public LandingPage(WebDriver aDriver) { this.driver = aDriver; } } Page Objects Jonathan Clarkin | @_jrwc | #KWSQA - Create a class for the page - It will need a constructor (special type of method) - Let the Page Object access the WebDriver

Slide 173

Slide 173 text

public class LandingPage { // Top Bar private By HOME = By.xpath("//*[@id='gn-apple']/a"); private By STORE = By.xpath("//*[@id='gn-store']/a"); private By MAC = By.xpath("//*[@id='gn-mac']/a"); private By IPHONE = By.xpath("//*[@id='gn-iphone']/a"); …[Constructor code]… } Page Objects Jonathan Clarkin | @_jrwc | #KWSQA Define selectors for each bit of the page

Slide 174

Slide 174 text

public class LandingPage { …[Selectors, Constructor code]… public void goToStore() { driver.findElement(STORE).click(); } public void searchFor(String something) { driver.findElement(SEARCH).sendKeys(something + "\n"); } } Page Objects Jonathan Clarkin | @_jrwc | #KWSQA Add methods for each interaction Use the defined selectors

Slide 175

Slide 175 text

Now we have a reusable Page Object ! Page Objects Jonathan Clarkin | @_jrwc | #KWSQA

Slide 176

Slide 176 text

To use it in a test… ● import LandingPage; ● LandingPage page = new LandingPage( driver ); ● page.searchFor( “Apple Juice” ); // Use it Page Objects Jonathan Clarkin | @_jrwc | #KWSQA

Slide 177

Slide 177 text

Creating Page Objects as a complementary activity to creating new web pages can be a great way to determine if it was written with testing in mind. Page Objects Jonathan Clarkin | @_jrwc | #KWSQA

Slide 178

Slide 178 text

TEST IT

Slide 179

Slide 179 text

Page Factory

Slide 180

Slide 180 text

PageFactory is a special tool that helps further encapsulate Page Object patterns. LoginPage page = PageFactory.initElements(driver, LoginPage.class); PageFactory Jonathan Clarkin | @_jrwc | #KWSQA

Slide 181

Slide 181 text

Pros - Simplifies Selectors in the PageObject - Simplifies the constructors of PageObjects Cons - Does not work well with dynamic pages - Slow if PageObject has lots of selectors PageFactory Jonathan Clarkin | @_jrwc | #KWSQA

Slide 182

Slide 182 text

Sample PageObject for use with PageFactory: PageFactory Jonathan Clarkin | @_jrwc | #KWSQA

Slide 183

Slide 183 text

The test case is nearly identical PageFactory Jonathan Clarkin | @_jrwc | #KWSQA

Slide 184

Slide 184 text

TEST IT

Slide 185

Slide 185 text

Mobile Automation

Slide 186

Slide 186 text

What if Selenium WebDriver could drive mobile apps as well ? Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 187

Slide 187 text

It can with a friend called... Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 188

Slide 188 text

Appium is an open source tool that helps Selenium WebDriver scripts talk to iOS and Android apps Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 189

Slide 189 text

Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 190

Slide 190 text

We write our Selenium tests in the same style as before, but now they interact with our apps Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 191

Slide 191 text

WebDriver driver = new AppiumDriver( new URL("http://127.0.0.1:4723/wd/hub")); WebElement loginUsername = driver.findElements(By.className("UIATextField")); loginUsername.sendkeys(“jclarkin”); Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 192

Slide 192 text

You choose the level of fidelity: - Check against simulated app - Check against emulated app - Check against physically wired app - Robotics ! Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 193

Slide 193 text

Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA

Slide 194

Slide 194 text

Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA https://www.youtube.com/watch?v=DShxaaTb_rg

Slide 195

Slide 195 text

Mobile App Automation Jonathan Clarkin | @_jrwc | #KWSQA More info at: - Appium: appium.io - Robots: tapsterbot.com

Slide 196

Slide 196 text

Review

Slide 197

Slide 197 text

- Setting up Java, Eclipse, JUnit, and Selenium - Authoring syntax of JUnit scripts - Using basic Selenium commands - Peeking under the web via browser tools - Applying Selenium Selectors - And more: Looping, Windows, Page Objects ... Review Jonathan Clarkin | @_jrwc | #KWSQA

Slide 198

Slide 198 text

- Grab a pen & some sticky notes - Write down : - What new insights have you gained ? - How you feel about the workshop ? - Which questions do you still have ? - How did someone on your team help you? - Place them on the communal wall Activity: Feedback Jonathan Clarkin | @_jrwc | #KWSQA

Slide 199

Slide 199 text

THANK YOU This is not a work of fiction. Names, characters, places and incidents are products of the author’s memory. Any resemblance to actual events or locales or persons, living or dead, is not entirely coincidental. All copyright belong to their respective owners. Images and text owned by other copyright holders are used here under the guidelines of the Fair Use provisions of Copyright Law. Special thanks to the Wachoskis and 20th Century Fox for creating The Matrix

Slide 200

Slide 200 text

No content