integrating selenium testing infrastructure into scala project
TRANSCRIPT
Test Automation Using Selenium Swati Email- [email protected]
History of The Selenium
Selenium is invent by Jason Huggins
He developed a Javascript library that could drive interactions with the page, allowing him to automatically rerun tests against multiple browsers.
Test Automation
Test automation is the use of software
To set test preconditions.
To control the execution of tests. To compare the actual outcomes to predicted outcomes. To report the Execution Status.
Commonly, test automation involves automating a manual process already in place that uses a formalized testing process.
Why and When To Automate?
Frequent regression testing
Repeated test case Execution is required
User Acceptance Tests
Faster Feedback to the developers
Reduce the Human Effort
Test same application on multiple environments
Selenium
Selenium is a robust set of tools that supports rapid development of test automation for web-based applications.
Selenium provides a rich set of testing functions specifically geared to the needs of testing of a web application.
Selenium operations are highly flexible, allowing many options for locating UI elements and comparing expected test results against actual application behavior.
Selenium Features
Supports Cross Browser Testing. The Selenium tests can be run on multiple browsers.
Allows scripting in several languages like Java, C#, PHP and Python.
Ajax/CSS support.
Assertion statements provide an efficient way of comparing expected and actual results.
Inbuilt reporting mechanism.
Selenium is an open source tool
Selenium Components
Selenium IDE
Selenium Remote Control
Selenium Grid
Selenium IDE
Previously known as Selenium Recorder
Selenium IDE is an integrated development environment for Selenium tests.
It is implemented as a Firefox extension, and allows you to record, edit, and replay the test in firefox
Selenium IDE allows you to save tests as HTML, Java, Ruby scripts, or any other format
It allows you to automatically add assertions to all the pages.
Allows you to add selenese commands as and when required
Selence
Selence special test scripting language for Selenium.
Selenese provides commands for performing actions in a browser (click a link, select an option), and for retrieving data from the resulting pages.
Selenium IDE Installation
First download Mozilla Firefox browser.
Click on the Firefox tab present at the top the browser window.
In drop down menu click on Add-on.
Go to add on page of Mozilla Firefox browser
In the search box of Add on page search the term "Selenium IDE" and press enter
Click on more link to the Selenium IDE add-on
Go to http://docs.seleniumhq.org/download/
Once installation is done the Mozilla Firefox browser will ask you to restart the browser.Just Restart it.
Selenium IDE - UI
ReplayToolbar
Start and Stop Recording
Editor
Editor
Selenese ScriptEditor
Selenium Log
Accessor Area
Recoding a Selenium Test Case
Open Firefox that has the IDE installed
Open the base URL of the application to record.
Keep the application in a common base state.
Go To Tools Selenium IDE and the IDE will be opened
Now perform the operations on the application as you are testing the application.
Once you are done with the recording click on the stop recording button and save the test case through the file menu. By default it will be saved as a selenese script (HTML format)
Creating a Test Suite
In the Selenium IDE you can create any number of test cases and save them as test suite.
To Run the test Suite click on the Play entire test suite button as shown below.
Test Suite with Test1 & test2
Running Options
Run a Test Case Click the Run button to run the currently displayed test case. Run a Test Suite Click the Run All button to run all the test cases in the currently loaded test suite. Stop and Start The Pause button can be used to stop the test case while it is running. The icon of this button then changes to indicate the Resume button. To continue click Resume.
Adding Assertions to the Script
Selenese allows multiple ways of checking for UI elements.
Verifications and assertions are used to check if
an element is present somewhere on the page?
specific text is somewhere on the page?
specific text is at a specific location on the page?
Verifications and assertions are not one and the same.
If an assertion fails, the script will be aborted but if a verification fails the script will continue.
Assertion Statements
assertTextPresentThis will assert if the text is present in the page.
assertTextThis will assert if a particular element is having the particular text.
assertTitle This will assert if the page is having a proper title.
assertValueThis will assert if a Text box or check box has a particular value
assertElementPresentThis will assert if a particular UI Element is present in the page.
User Acceptance Testing (UAT)
User Acceptance Testing (UAT) is the last phase of the software testing process. During UAT, actual software users test the software to make sure it can handle required tasks in real world scenarios, according to specifications.
UAT is one of the final and critical software project procedures that must occur before newly developed software is rolled out to the market.
UAT is also known as beta testing, application testing or end user testing.
Steps Involved in In-House UAT
Planning: The UAT strategy is outlined during the planning step.
Designing test cases: Test cases are designed to cover all the functional scenarios of the software in real-world usage.
Executing test cases and documenting: The testing team executes the designed test cases. All bugs are logged in a testing document with relevant comments.
Bug fixing: Responding to the bugs found by the testing team, the software development team makes final adjustments to the code to make the software bug-free.
Sign-off: When all bugs have been fixed, the testing team indicates acceptance of the software application. This shows that the application meets user requirements and is ready to be rolled out in the market.
Importance of UAT
UAT is important because it helps demonstrate that required business functions are operating in a manner suited to real-world circumstances and usage.
UAT is an effective process with a high rate of return for those who take the time to implement and follow its discipline.
How Selenium Work
Common Problem in Web Testing
Record/replay is not reusable, is fragile.
Managing test cases.
UI changesmake tests brittle.
ScalaTest Using Selenium
ScalaTest 2.0 includes a domain specific language (DSL) for writing browser-based tests using Selenium.
To use ScalaTest's Selenium DSL, you'll first need to add Selenium to your project dependencies. For example, in your sbt build you might add:libraryDependencies += "org.seleniumhq.selenium" % "selenium-java" % "2.23.1" % "test->default"
Mix trait WebBrowser into your test class.This trait provides the DSL in its entirety except for one missing piece : an implicit org.openqa.selenium.WebDriver
Element Locator
ID: id=foo
Name: name=foo
First ID, then name: identifier=foo
DOM: document.forms[myform].myDropdown
XPath: xpath=//table[@id='table1']//tr[4]/td[2]
Link Text: link=sometext
CSS Selector: css=a[href=#id3]
Sensible defaults, e.g. xpath if starts with //
Example of UAT
class BlogSpec extends FlatSpec with ShouldMatchers with WebBrowser { implicit val webDriver: WebDriver = new HtmlUnitDriver val host = "http://localhost:9000/" "The blog app home page" should "have the correct title" in { go to (host + "index.html") pageTitle should be ("Awesome Blog") } }
Terms of Test Cases
Navigation:You can ask the browser to retrieve a page (go to a URL) like this:go to "http://www.artima.com"
Checking input element values:textField("q").value should be ("Cheese!")textArea("body").value should be ("I saw something cool today!")
Implicit wait :implicitlyWait(Span(10, Seconds))
Cleaning up:To close all windows, and exit the driver, use quit:quit()
Test Code of Autocomplete Functionality
Define the autocomplete test code in a trait so that it will be use by all the test class which extends this trait :trait Reattempt { def attemptOrReattempAutoComplete(func: By => WebElement, selector: String, searchText: String,Text: String, startWithCount: Int, maxNumberOfRetries: Int = 5): Unit = { if (startWithCount 0) { func(By.cssSelector(selector)).clear func(By.cssSelector(selector)).sendKeys(searchText) try { if(searchText == Text) { func(By.cssSelector(refrence of div where this search text element present")).click } else { func(By.cssSelector("refrence of div where this search text element presen")).sendKeys(Keys.ARROW_UP) } } catch { case e: TimeoutException => attemptOrReattempAutoComplete(func, selector, searchText.substring(0, searchText.length - 1),Text: String, startWithCount + 1, maxNumberOfRetries) case e: ElementNotVisibleException => attemptOrReattempAutoComplete(func, selector, searchText.substring(0, searchText.length - 1), Text: String, startWithCount + 1, maxNumberOfRetries) } } }}
Sequentially Running Test
To run the whole test cases sequentially we have to put this line in build.sbtparallelExecution in Test := falseconcurrentRestrictions in Global += Tags.limit(Tags.Test, 1)parallelExecution in ScctPlugin.ScctTest := false
Command For Run Acceptace Test
To run single testsbt test-only packageName.className
To run whole testsbt test
To check the coverage reportsbt scct:test
Selenium Test in Headless Mode with Xvfb
Follow the following steps to configure Xvfb (Virtual Framebuffer) to run test cases:
install Xvfb on ubuntu using following command : sudo apt-get install xvfb
Create .sh file in root /etc/init.d/ folder and paste the following script code in the script (.sh) file:
XVFB=/usr/bin/XvfbXVFBARGS=":1 -screen 0 1024x768x24 +extension GLX +render -noreset -nolisten tcp"PIDFILE=/var/run/xvfb.pidcase "$1" in start) echo -n "Starting virtual X frame buffer: Xvfb" start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --background --exec $XVFB -- $XVFBARGS echo "." ;; stop) echo -n "Stopping virtual X frame buffer: Xvfb" start-stop-daemon --stop --quiet --pidfile $PIDFILE echo "." ;; restart) $0 stop $0 start ;; *) echo "Usage: /etc/init.d/xvfb {start|stop|restart}" exit 1esac
exit 0
Selenium Test in Headless Mode with Xvfb
Now,open the terminal and go to your project and run : Xvfb :1 -screen 0 1920x1080x24 +extension GLX +render -noreset -nolisten tcp
and open another tab of terminal and go to your project and run :export DISPLAY=:1sbt test
Thank You