Automation Scripts

  • Published on
    18-Nov-2014

  • View
    2.571

  • Download
    5

Embed Size (px)

Transcript

Automation ScriptsIntroduction about Selenium

Selenium is an open source testing tool to automate web applications across many platforms. This tool is developed in Java Script and supports all the major browsers on all the platforms. Selenium is a cross-browser, cross-platform solution supporting Internet Explorer on Windows and Mozilla-based browsers on Windows, Mac OSX and Linux and Unix. There are 3 variants in selenium, which can be used to create automation suite for web application. Selenium IDE Selenium Core and Selenium Remote Control.

Selenium Grid is an extension of selenium RC to test in different environment.

Selenium IDE Selenium IDE is an integrated development environment for Selenium tests. It also serves as a starting point for automation. Selenium IDE comes as Firefox extension, which allows recording, editing and debugging tests. Features of Selenium IDE 1. Record and playback. 2. Auto complete for all common Selenium commands. 3. Support for Selenium user-extensions.js file and many more. Selenium Core Selenium Core tests run directly in a browser, just as real users do. Selenium Core used for

Browser compatibility testing- Application can be tested in different browsers and operating system.

System functional testing. Create tests to verify the application functionality and user acceptance.

Selenium Core tests run directly into application web server. This allows the tests to run in any supported browser on the client-side. Selenium Remote Control Selenium RC comes in two parts. 1. A server which automatically launches and kills browsers, and acts as a HTTP proxy for web requests from them. 2. Client libraries for your favorite computer language. The RC server also bundles Selenium Core, and automatically loads it into the browser. Selenium Grid Selenium Grid is a tool that speeds up functional testing of web-apps. It allows running multiple tests in parallel, on multiple machines, in a heterogeneous environment. Selenium Grid coordinates multiple selenium remote control servers, so that test can be run on different platforms concurrently, which saves time and allows wider testing.

Overall architecture of the selenium

Classification of seleniumBrowser Support Requires Remote Installation Supports HTTPS/SSL Supports Multiple Domains Requires Java Saves Test Results to Disk Language Support Selenium IDE Firefox Only No Yes Yes No No Selenese Only(and also Ruby in selenium 0.7 deprecated) Selenium Core All Yes Yes No No No Selenese Only Selenium RC Many No Yes Yes Yes Yes Many (Java, C#,PHP, Perl, Python and Ruby)

Same Origin Policy The same origin policy states that JavaScript is only allowed to read/modify HTML from the same origin as its source. That policy makes a lot of sense. Let's say you've got your browser window pointing at, for example, your bank's website, but you also have another webpage open pointing to someone's blog. JavaScript is allowed to read values from web pages, as well as change data appearing on webpages you've loaded. If not for the same origin policy, a malicious blogger could read your bank data, or worse, rewrite your bank page to make you think it was saying something else. The blogger could use JavaScript to trick you into giving him sensitive information. Why Selenium RC? Selenium Core needs to be installed on the same website as the Application Under Test (AUT), because of the Same Origin Policy, a security policy which prevents cross-site scripting. That means that one can't just sit down and write a Selenium Core test that runs on google.com; to do that, one should need to install Selenium Core on google.com. The same origin policy states that JavaScript is only allowed to read/modify HTML from the same origin as its source. If a js file is designed to test google.com, the same origin policy denies to run that js file with google.com; instead, selenium RC can be used to test the site. Selenium RC makes a proxy server, which allows running the test against the site. Selenium JS files were actually available on the remote server and the proxy server tricks the browser into thinking there is a directory example: http://www.google.com/selenium/

Installation of Selenium1. Selenium IDE Selenium IDE comes as firefox extension. It can be installed to firefox2.0 (+). 2. Selenium Remote Control Selenium RC can be downloaded from Selenium RC download. Selenium RC comes as a zip file. It is easy to install in the system. Simply unzip the downloaded file to location where your selenium RC to be installed. Selenium RC requires java-1.5 (+). Setting up Selenium RC Set system variable JAVA_HOME= /*---path location of Java home for e.g.: c:\jdk1.5.0_12---*/

Set class path of client driver for selenium Class path= /*-- C:\Selenium\ selenium-remote-control-1.0SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar --*/ Note: If you are going to use firefox 3, then make sure that your download support for the latest version. Before going to test the automations close all the firefox process in your system, which may also restrict to start browser.

3. Selenium Grid Selenium Grid requires java-1.5(+) and also Ant 1.7 (+) to start the hub. Setting up of selenium Grid Set system variable JAVA_HOME= /*---path location of Java home for e.g.: c:\jdk1.5.0_12---*/ Set path of system variable for Ant path= /*--path of Apache Ant bin c:\Selenium\apache-ant-1.7.1\bin --*/ 4. Selenium Eclipse Developing the selenium java along with eclipse makes the work simpler. Test cases can be written in java and it can be executed using eclipse plug-in. Download Selenium Eclipse. (Add the downloaded selenium plug in to your eclipse\plugins folder) Eclipse requires JUnit/TestNG along with selenium to run the script. The selenium project can also be developed using JUnit4.4. Set system variables for junit JUNIT_HOME=/*--path of junit jar file c:\selenium\junit-4.4\junit4.4.jar--*/ 5. Logging Selenium Logging selenium is required for selenium Junit project to produce log report for the test cases. Logging selenium is a java plug-in which can be included to the selenium project. Logging selenium also requires commons-langs 2.4 to generate log report for the test case.

Getting Started with Selenium IDE

Selenium IDE Selenium IDE is mainly used for record and play back. Base URL in the IDE is used to specify the URL which is going to test. Toolbar next to the base URL is used to record and play back the test. The red circle in the toolbar is used to record the actions which are taken place in the web page. Then the recorded test can be played using either run current test or run entire test suite. Test suite can also be played using Test runner, but it can play only html test.

Selenium speed can be controlled using slider of selenium IDE. In Selenium IDE, select options->format->java to convert the test case in java. In Selenium IDE, select options->options, then selenium IDE options dialog box open. It has selenium core extension, which accepts the user-extension.js. For example flashselenium extension.js and flexSeleniumAPI.js allows the selenium to test flash and flex application.

Getting started with selenium RCTo start with selenium RC, selenium server can be started at interactive mode in command prompt and type each command of selenium in it. In Command prompt, go to selenium server location, and then type the command to start the selenium server in Interactive mode java -jar selenium-server.jar interactive Selenium RC can also run html test suite in test runner and it produce log report.

For example: java jar selenium-server.jar htmlSuite -browser=*chrome url=http://www.google.com c:\selenium workspace\testsuite.html c:\selenium workspace\report\log-Result.html Selenium RC can also run the selenium java test case from eclipse.

Getting started with selenium GridTo start with selenium Grid, selenium hub should be launched in command prompt. ant launch-hub A. To run the test in sequence, launch only one remote control. To launch the remote control, in new command prompt, type ant launch-remote-control then in a new command prompt, type ant run-demo-in-sequence B. To run the test in parallel in same machine, launch two or more remote control. Start new terminal for each remote control, then type ant -Dport=5555 Denvironment=*chrome launch-remote-control ant -Dport=5566 Denvironment=*iexplore launch-remote-control ant -Dport=5577 Denvironment=*chrome launch-remote-control (Note: To check, type http://localhost:4444/console in browser, this will display the details of the remote control) C. To run the test in parallel in multiple machine, launch two or more remote control. Start new terminal for each remote control, then type ant -Dport=5555 Dhost=192.170.50.32 DhubURL=http//192.170.50.96:4444 Denvironment=*chrome launch-remotecontrol ant -Dport=5555 Dhost=192.170.50.64 DhubURL=http//192.170.50.96:4444 Denvironment=*chrome launch-remotecontrol Then in a new terminal, type ant run-demo-in-parallel

In Third case, the hub is launched in the system with the ip=192.170.50.96 but the remote control is launched in two different machines with ip=192.170.50.32 and ip=192.170.50.64. In this case console details can be viewed using http://192.170.50.96:4444/console

List of Browser and its NameBrowser *chrome *chrome *chrome *iehta *safari *firefox *chrome *iexplore *iehta *safari *konqueror Name Firefox on Windows Firefox on OS X Firefox on Linux IE on Windows Safari on OS X *firefox *chrome (Firefox) *iexplore (Internet Explorer) *iehta (Internet Explorer) *safari *konqueror

Writing Junit Test in EclipseThe following steps are involved in creating a new test case in eclipse. 1) Create a new java/selenium project in eclipse 2) Then build java path (includes java\lib\*.jar) to run the project