33
Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Embed Size (px)

Citation preview

Page 1: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 1 CSCE 747 Fall 2013

CSCE 747 Software Testing and Quality Assurance

Lecture 16 Testing Web Applications

10/23/2013 1

Page 2: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 2 CSCE 747 Fall 2013

Last Time GUI testing 2 Chapter 19

Today Test 1 take-home TestingGeek Testing Web Applications Selenium

Page 3: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 3 CSCE 747 Fall 2013

Locating elements using findElements@Test public void testFindElements() { //Get all the links displayed on Page

List<WebElement> links = driver.findElements(By.tagName("a"));

//Verify there are four links displayed on the page assertEquals(4, links.size());

//Iterate though the list of links and print //target for each link for(WebElement link : links)

System.out.println(link.getAttribute("href")); }

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 4: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 4 CSCE 747 Fall 2013

How it works... The findElements() method returns all the elements matching with the locator specified as a list of WebElements.

In Java, we can use the List class to create an instance of list of WebElements. List<WebElement> links = driver.findElements(By.tagName("a"));

The size() method of the List class will tell us how many elements are there in the

list. assertEquals(4, links.size()); We can iterate using this list in the following way, getting a link

and printing its target value: for(WebElement link : links)

System.out.println(link.getAttribute("href"));

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 5: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 5 CSCE 747 Fall 2013

Finding a link by its text Selenium WebDriver's By class provides the

linkText() method to locate links using the text displayed for the link. In the following example, we will locate the GMail link:

WebElement gmailLink = driver.findElement(By.linkText("GMail")); assertEquals("http://mail.google.com/", gmailLink.getAttribute("href"));

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 6: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 6 CSCE 747 Fall 2013

Finding a link by partial text

WebElement inboxLink = driver.findElement(By.partialLinkText("Inbox")); System.out.println(inboxLink.getText());

See also The Locating an element using the findElement method recipe

The Locating elements using findElements method recipe

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 7: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 7 CSCE 747 Fall 2013

Locating by Tag Name

WebElement loginButton = driver.findElement(By.tagName("button"));

loginButton.click(); Take another example where we want to count how

many rows are displayed in <table>. We can do this in the following way:

WebElement table = driver.findElement(By.id("summaryTable"));

List<WebElement> rows =table.findElements(By.tagName("tr")); assertEquals(10, rows.size());

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 8: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 8 CSCE 747 Fall 2013

CSS

visit http://en.wikipedia.org/wiki/Cascading_Style_Sheets.

DOM

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 9: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 9 CSCE 747 Fall 2013

CSS – Absolute Paths

WebElement userName = driver.findElement(By.cssSelector("html

body div div form input")); You can also use the previous selector in the

following way by describing the direct parent to child relationships with > separator:

WebElement userName = driver.findElement(By.cssSelector("html > body > div > div > form > input"));

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 10: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 10 CSCE 747 Fall 2013

WebElement loginButton = driver.findElement(By.cssSelector("input.login"));

WebElement loginButton = driver.findElement(By.cssSelector(".login"));

WebElement userName = driver.findElement(By.cssSelector("input#username"));

WebElement userName = driver.findElement(By.cssSelector("#username"));

WebElement userName = driver.findElement(By.cssSelector("input[name=username]"));

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 11: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 11 CSCE 747 Fall 2013

WebElement previousButton = driver.findElement(By.cssSelector("img[alt='Previous']"));

WebElement previousButton = driver.findElement(By.cssSelector("input[type='submit'][value='Login']"));

List<WebElement> imagesWithAlt = driver.findElements(By.cssSelector("img[alt]"));

List<WebElement> imagesWithoutAlt = driver.findElements(By.cssSelector("img:not([alt])"));

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 12: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 12 CSCE 747 Fall 2013

Performing partial match on attribute values

^= -starting with- input[id^='ctrl']

$= -ending with- input[id$='_userName']

*= -containing- input[id*='userName']

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 13: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 13 CSCE 747 Fall 2013

Locating elements using XPath

WebElement userName = driver.findElement(By.xpath("html/body/div/div/form/input"));

assuming it is the first <input> element in the DOM:

WebElement userName = driver.findElement(By.xpath("//input"));

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 14: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 14 CSCE 747 Fall 2013

HTML

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 15: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 15 CSCE 747 Fall 2013

doc intro

Selenium Testing Tools Cookbook by Gundecha – 2012

Page 16: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 16 CSCE 747 Fall 2013

Selenium Documentation Note to the Reader–Docs Being Revised for Selenium 2.0! Introduction Test Automation for Web Applications To Automate or Not to Automate? Introducing Selenium Brief History of The Selenium Project Selenium’s Tool Suite Choosing Your Selenium Tool Supported Browsers and Platforms Flexibility and Extensibility What’s in this Book? The Documentation Team–Authors Past and Present

http://docs.seleniumhq.org/docs/

Page 17: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 17 CSCE 747 Fall 2013

Test Automation for Web Applications

test automation specify input (events) specify expected output compare expected with actual output

advantages to test automation repeatability of the tests and the speed at which the tests can be executed

Selenium is possibly the most widely-used open source solution

http://docs.seleniumhq.org/docs/

Page 18: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 18 CSCE 747 Fall 2013

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

http://docs.seleniumhq.org/docs/

Page 19: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 19 CSCE 747 Fall 2013

To Automate or 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.

http://docs.seleniumhq.org/docs/

Page 20: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 20 CSCE 747 Fall 2013

Introducing Selenium Selenium is a set of different software tools Each with a different approach to supporting test

automation. The entire suite of tools results in a rich set of testing

functions specifically geared to the needs of testing of web applications of all types.

These operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behavior.

One of Selenium’s key features is the support for executing one’s tests on multiple browser platforms.

http://docs.seleniumhq.org/docs/

Page 21: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 21 CSCE 747 Fall 2013

Brief History of The Selenium Project

In 2004 Jason Huggins “better uses of his time than manually stepping through the same tests with every change he made. “

He developed a Javascript library that could drive interactions with the page, allowing him to automatically rerun tests against multiple browsers.

That library became Selenium Core, which underlies all the functionality of Selenium Remote Control (RC) and Selenium IDE.

Selenium RC was ground-breaking because no other product allowed you to control a browser from a language of your choice.

http://docs.seleniumhq.org/docs/

Page 22: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 22 CSCE 747 Fall 2013

Web Driver

In 2006 Simon Stewart at Google started work on a project he called WebDriver.

Google had long been a heavy user of Selenium, but testers had to work around the limitations of the product.

Simon wanted a testing tool that spoke directly to the browser using the ‘native’ method for the browser and operating system,

thus avoiding the restrictions of a sandboxed Javascript environment.

http://docs.seleniumhq.org/docs/

Page 23: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 23 CSCE 747 Fall 2013

Merging of Selenium and WebDriver

Selenium had massive community and commercial support, but WebDriver was clearly the tool of the future.

“Why are the projects merging? Partly because webdriver addresses some shortcomings in selenium (by being able to bypass the JS sandbox, for example. And we’ve got a gorgeous API), partly because selenium addresses some shortcomings in webdriver (such as supporting a broader range of browsers) and partly because the main selenium contributors and I felt that it was the best way to offer users the best possible framework.” email from Simon Stewart

http://docs.seleniumhq.org/docs/

Page 24: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 24 CSCE 747 Fall 2013

Selenium Suite

Selenium 2 aka. Selenium Webdriver Selenium 1 aka. Selenium RC or Remote

Control Selenium IDE is a prototyping tool for

building test scripts. Selenium-Grid allows scaling for large test

suites and for test suites that must be run in multiple environments

http://docs.seleniumhq.org/docs/

Page 25: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 25 CSCE 747 Fall 2013

Selenium IDE

Selenium IDE is a prototyping tool for building test scripts.

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 that

can be later executed

http://docs.seleniumhq.org/docs/

Page 26: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 26 CSCE 747 Fall 2013

Choosing Your Selenium Tool

Selenium-WebDriver API being the future direction for Selenium.

Selenium 1 is provided for backwards compatibility.

Still, both have strengths and weaknesses which are discussed in the corresponding chapters of this document.

http://docs.seleniumhq.org/docs/

Page 27: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 27 CSCE 747 Fall 2013

Selenium-WebDriver

Selenium-WebDriver supports the following browsers along with the operating systems these browsers are compatible with. Google Chrome 12.0.712.0+ Internet Explorer 6, 7, 8, 9 - 32 and 64-bit where applicable Firefox 3.0, 3.5, 3.6, 4.0, 5.0, 6, 7 Opera 11.5+ HtmlUnit 2.9 Android – 2.3+ for phones and tablets (devices & emulators) iOS 3+ for phones (devices & emulators) and 3.2+ for tablets

(devices & emulators)

http://docs.seleniumhq.org/docs/

Page 28: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 28 CSCE 747 Fall 2013

Selenium-WebDriver

Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded.

WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

http://docs.seleniumhq.org/docs/

Page 29: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 29 CSCE 747 Fall 2013

How Does WebDriver ‘Drive’ the Browser

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.

This information is isolated in ‘browser drivers’

http://docs.seleniumhq.org/docs/

Page 30: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 30 CSCE 747 Fall 2013

WebDriver and the Selenium-Server

There are some reasons though to use the Selenium-Server with Selenium-WebDriver.1. You are using Selenium-Grid to distribute your

tests over multiple machines or virtual machines (VMs).

2. You want to connect to a remote machine that has a particular browser version that is not on your current machine.

3. You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver

http://docs.seleniumhq.org/docs/

Page 31: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 31 CSCE 747 Fall 2013

Setting Up a Selenium-WebDriver Project for Java

to set up a Selenium 2.0 Java project is to use Maven.

Maven will download the java bindings (the Selenium 2.0 java client library) and all its dependencies, and

will create the project for you, using a maven pom.xml (project configuration) file.

Once you’ve done this, you can import the maven project into your preferred IDE, IntelliJ IDEA or Eclipse

http://docs.seleniumhq.org/docs/

Page 32: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 32 CSCE 747 Fall 2013http://docs.seleniumhq.org/docs/

Page 33: Lec 18 Web with Selenium 1 CSCE 747 Fall 2013 CSCE 747 Software Testing and Quality Assurance Lecture 16 Testing Web Applications 10/23/2013 1

Lec 18 Web with Selenium 33 CSCE 747 Fall 2013Selenium Testing Tools Cookbook by Gundecha – 2012