Automated Web Testing Using Selenium

Post on 18-Jan-2017

7,507 views 1 download

transcript

Automated Web Testing using Selenium

Perficient ChinaWaylon Zhang

Yahoo!m: waylon_zhang2008waylon.zhang@perficientgdc.com.cn

Agenda

Warm-up Introduction of Selenium FrameworkTroubleshooting of SeleniumPracticeQ&A

Warm-up

Google Advance Search Function

Prerequisite

Development experience in Java languageKnowledge of Xpath languageBasic knowledge of HTML/Junit/TestNgKnowledge of The Same Origin Policy

What is Selenium?

Selenium is a suite of tools to automate web app testing across many platforms.

– Selenium Core: DHTML test execution framework

– Selenium IDE: Firefox extension – record and playback

– Selenium Remote Control• Server: launches/kills browser,

HTTP Proxy• Client libraries

– Selenium Grid• Run Selenium tests in parallel

Selenium components

Selenium Core Selenium IDE Selenium Remote Control

Browsers Firefox, IE,Safari Firefox Firefox, IE,Safari

AUT Server side installed

Yes No No

Support Https/SSL Yes Yes Yes

Different Domain No Yes Yes

Need Jre No No Yes

Support I&O No No Yes

Programming Languages

*Selenese Selenese C#, Java, Perl, PHP, Python, Ruby

*Selenese is the set of Selenium Commands that run you tests. Don’t support If or For (While) statements

How to use Selenium RC to develop

Prepare environment

Record Refactor Run

Setup J reSelenium RC

Selenium IDEFireFox

Selenium- IDEManual Test once

Start Selenium Server

FireBugWeb Developer

Check Test Report

How Selenium RC Works?

How Selenium RC Works? (cont.)

Selenium Solutions of HTTPS

Support HTTPS– IE (supported by

default, configure IE browser)

– FireFox (Selenium server doesn’t support add-ons configurations)

– Safari (doesn’t support HTTPS configurations, so Selenium can’t run on it)

Selenium to Test Click Flash (1/2)

You can't get object via Selenium IDE

Selenium to test Click Flash (2/2)

Get help from Developer– javascript:WTKSendFlashClick( "{\"Page\":\"Exterior\", \"ClickType\":\"angleC

lick\",\"ClickParams\":{\"Direction\":\"left\"}}" )Use Selenium’s API

– selenium.runScript(target)

Selenium to test Ajax

Wait For It…

public void waitForElementPresent(String vp, String target, String friendlyMessage) {for (int second = 0;second<WAIT; second++) {if (second >= WAIT) { //fail("timeout");log.info(vp+ " - Time out, trying again while waiting for element to load: " + target);}try { if (selenium.isElementPresent(target)) break; } catch (Exception e) {}try {Thread.sleep(SPEED);} catch (InterruptedException e) {// TODO Auto-generated catch blockif (e.getMessage().indexOf("Current window or frame is closed")>-1)restartSelenium();resultLoger.result(vp, FAILURE, friendlyMessage + ". "+ "Timeout waiting for target:

"+target + ": "+e.getMessage());log.info(vp, new SeleniumException("Timeout waiting for target: "+target));}}

Wait and verify the value until time out

Add API to Selenium (1/2)

Example: add Function of getTableRows(locator) to Selenium– Server Side

• Add js to user-extensions.js• Save the user-extensions.js under the folder where selenium server

resides• start selenium server with user-extensions.js

Add API to Selenium (2/2)

Example: add Function of getTableRows(locator) to Selenium– Client Side

• Add API to Selenium.java under package of com.thoughtworks.selenium • Add function to DefaultSelenium.java under package of

com.thoughtworks.selenium

Selenium Open Issues

How to test GWT web applications?

Problems in Selenium

Verbose – Locators everywhere

Coupling– UI locators, actions, and assertions are coupled

Not expressive– What does the UI look like?

Speed– Since IE is lack of native XPath support, so tests run in IE are very slow

Record and Play– Re-labeling a button or moving it to another part of the window may

require the test to be re-recordedRefactor

– Seems to be difficultReusable

– Less likely

Tellurium’s solutionpackage com.perficient.tellurium.webapp.ui

import org.tellurium.dsl.DslContextpublic class TelluriumModule extends DslContext {

public void defineUi(){ ui.Container(uid: "root", clocator: [tag: "table", name: "Tellurium"]){ InputBox(uid: "firstName", clocator: [tag: "input", type: "text", name: "FirstName"]) InputBox(uid: "lastName", clocator: [tag: "input", type: "text", name: "LastName"]) InputBox(uid: "fullName", clocator: [tag: "input", type: "text", name: "FullName"]) InputBox(uid: "save", clocator: [tag: "input", type: "button", value: "Save"], respond: ["click"]) } } public void getFullName(String firstName,String lastName){ type "root.firstName",firstName type "root.lastName",lastName pause 500 click "root.save" }}

What is Tellurium?

UI module based web testing framework built on top of SeleniumUI is defined in Groovy class and tests can be written in

– Java (JUnit 4 or TestNG)– Groovy– Pure DSL script

Handles the mapping from Java object to UI

Reference: Reference: http://code.google.com/p/aost/

What challenges are addressed in Tellurium

No longer “record and play” style, but UI module orientedSeparatedRobustEasy to refactor

What challenges are addressed in Tellurium

Support jQuery Selector– useJQuerySelector()– disableJQuerySelector()

Summarization: Advantage/Disadvantage

Pre-condition Advantage DisadvantageSelenium It’s required to

have both the HTML and the back-end code ready before we start scripting

Record and Play requires little or no software development

Hard to refactorVery slow in IEUI locators, actions, and assertions are coupled

Tellurium Ideally only defining the HTML elements is required as the entry criteria of scripting

No longer “record and play” style, but UI module orientedEasy to refactorEasy to useUI and tests are separatedFaster performance in IE

Involved new language Very new, some potential risks we don’t know

Best Practices

To Automate or Not to Automate? That is the Question!The test framework should be approved, documented, reviewed.Structure scripts with minimal dependencies.Test automation is a fulltime effort, not a sideline. A dedicated

team is required.

Q&A

References

Tellurium Framework– http://code.google.com/p/aost/

Guidelines for Automation framework design– http://knowledgeinbox.com/articles/automation/frameworks/guidel

ines-for-automation-framework-design/10 Step Test Automation Approach - Guide for automation

testers– http://testing.thoughtworks.com/article10step