Automating GUI testing with Selenium WebDriver, Java and Eclipse Selenium Basics Automating GUI testing with Selenium WebDriver, Java and Eclipse Tom Wimsatt, CTAL Columbia Software Test Meetup December 1, 2016 1
Selenium Basics - Agenda Introduction to Selenium Eclipse JUnit Selenium Environments IDE WebDriver Browsers – IE and FF 2 ©2013 Sotera Defense Solutions, Inc. All Rights Reserved. 2
Selenium Basics - Agenda Test Formatting Test Case, Test Suite, Test Runner Design Patterns Making the Most of Test Automation Handling Errors and Exceptions Object Identification - Locators Resources 3 ©2013 Sotera Defense Solutions, Inc. All Rights Reserved. 3
Selenium Basics - Introduction Selenium is for browser automation FireFox Internet Explorer Chrome Etc. Supports many languages Java Python Ruby 4 ©2013 Sotera Defense Solutions, Inc. All Rights Reserved. 4
Selenium Basic - Introduction Selenium leverages JUnit and is supported by Eclipse: Asserts TestCase, TestSuite, TestRunner 5 ©2013 Sotera Defense Solutions, Inc. All Rights Reserved. 5
Selenium Basics - Environments Selenium IDE FireFox Browser plug-in Includes formatters for Ruby, Python, Java, C# Useful for debugging – Locating objects in particular Quick scripting of tests Not recommended for long term or serious test development – inflexible (no looping, etc).
Selenium Basics - Environments Selenium WebDriver Works with Eclipse through jars Step toward Selenium Grid – multiple machine and environment automation. Ultimate in flexibility
Selenium Basics - Environments Browsers Internet Explorer – simple but somewhat inflexible browser for testing. Tips Object ID – F12 for Developer Tools Compatibility View pre-IE 11
Selenium Basics - Environments Browsers FireFox – very flexible. Profiles give the tester options for setting up the browser for different purposes. Object ID – Developer Tools > Inspector FireBug plug-in for older FF versions Disable all add-ins for a clean profile for testing. Use that as the default. firefox.exe -p
Selenium Basics - Environments Browsers FireFox (cont'd) Create profiles or rename them for specific purposes firefox.exe -p Older versions of Selenium required separate FF jar To get all FF preferences: about:config in the address bar
Selenium Basics – Test Formatting JUnit Classes Test Case – Individual or collected tests @Before or setUp – execute this method before every test Initialize the browser with default timing Navigate to initial home page to begin testing
Selenium Basics – Test Formatting JUnit Classes Test Case (cont'd) @Test or testName – the actual test One or more tests in a test class @Before and @After are executed for each test @After or tearDown executed after each test method Close the browser
Selenium Basics – Test Formatting JUnit Classes Test Suite – collection of Test Cases Test Runner – Execution of test cases (main)
Selenium Basics – Test Formatting Design Patterns Classes for browser initialization Include default timing parameters for page loads, object appearances, failure waits. Default settings e.g. default certificate handling Object Repositories Repository class for each page Collect in a separate package
Selenium Basics – Making the Most of Automated Testing The keys to GUI test automation are: Handling errors – when the test fails. Handling dynamic object names – the code may name the objects on the page as it loads or after compiling.
Selenium Basics – Making the Most of Automated Testing Handling errors – capture the error and continue testing. Define your initial condition for all tests All tests in a suite begin at the same place Return to the initial condition at the end of each test – reset to default. Carefully plan tests that may have to build on one another
Selenium Basics – Making the Most of Automated Testing Handling errors (cont'd) If testing for errors, know what the possible errors could be: Runtime exceptions Error dialog – modal? Browser crashing
Selenium Basics – Making the Most of Automated Testing Object Identification – Locators By ID, By Name, By Xpath – Not as widely supported. IE and FF support Xpath. By CSS Selector Native support in most browsers. Faster than Xpath
Selenium Basics – Making the Most of Automated Testing Object Identification – Locators (cont'd) The findElement() method Locate a single element on a page Perform actions on elements click clear getAttribute and getText sendKeys
Selenium Basics – Making the Most of Automated Testing Object Identification - Locators (cont'd) The findElements() method Locate a list of similar elements on a page Use to navigate down to the element you need Narrows down the elements Select the Nth element in a set Example: List<WebElement> elements = driver.findElements(By.cssSelector(“div.button”));
Selenium Basics – Making the Most of Automated Testing Object Identification - Locators (cont'd) The trick to locators is in the wildcarding: Element div attribute id begins with 't' by.cssSelector(“div[id^='t']”) Element div attribute id ends with 't' by.cssSelector(“div[id$='t']”)
Selenium Basics – Making the Most of Automated Testing Object Identification - Locators (cont'd) Combine attributes to better locate the element: Element div attribute id and class ends with 't' by.cssSelector(“div[id^='t'][class$='t']”)
Selenium Basics – Making the Most of Automated Testing Object Identification - Locators (cont'd) Iterate through a list using attributes: List<WebElement> elements = driver.findElements(by.cssSelector... for(WebElement element : elements) { String attrib = element.getAttribute(“value”); }
Selenium Basics – Making the Most of Automated Testing Object Identification - Locators (cont'd) Iterate through a list using containing text: List<WebElement> elements = driver.findElements(by.cssSelector... for(WebElement element : elements) { String attrib = element.getText(); }
Selenium Basics – Making the Most of Automated Testing Selection When presented with a list of options: Select(WebElement).selectByVisibleText(“This One”); Switching Windows Set<String> handles = driver.getWindowHandles(); Driver.switchTo().window(handle);
Selenium Basics - Resources Selenium HQ - http://www.seleniumhq.org/ Rosetta Stone for Locators: http://www.cheat-sheets.org/saved-copy/Locators_groups_1_0_2.pdf http://www.cheat-sheets.org/saved-copy/Locators_table_1_0_2.pdf Selenium 2 Testing Tools Selenium Testing Tools Cookbook Selenium WebDriver Practical Guide Packt Publishing: https://www.packtpub.com/ 2626 ©2014 Sotera Defense Solutions, Inc. All Rights Reserved. 26
Selenium Basics - Resources Robert T. (Tom) Wimsatt, CTAL Robert.Wimsatt@soteradefense.com 410-379-5658 Sotera Defense Solutions, Inc. https://www.linkedin.com/in/tomwimsatt 2727 ©2014 Sotera Defense Solutions, Inc. All Rights Reserved. 27