<<

International Journal On Advanced Computer Theory And Engineering (IJACTE) ______

Test Automation Using and WebDriver

Yadvinder Singh Pannu Bachelor of Technology, Computer Science and Engineering

these locators, Selenium WebDriver enables us to Abstract— Automation testing tools are used to take over the monotonous, repetitive and time consuming work of identify any type of WebElement present in a web manually executing a series of steps needed to determine application by using the findElement method. In the the correctness and accuracy of the behavior of situation where more than one element possess the same applications under scrutiny. Amongst the myriad of locator values, a WebElement can be identified uniquely automation testing tools available today, Cucumber and by the use of findElements method. Selenium WebDriver form a very versatile and robust combination as a software testing framework. Cucumber lets us know in plain text what a test is supposed to check and Selenium WebDriver makes direct calls to the web browser and performs the actual steps. Index Terms—Automation, BDD, Cucumber, Selenium WebDriver. I. INTRODUCTION Cucumber is a software testing tool which adopts the Fig. 1.AUTOMATION FRAMEWORK Behavior Driven Development (BDD) approach of II. WRITING TEST CASES USING application testing. Using Cucumber, tests are written in CUCUMBER SELENIUM WEBDRIVER plain text in the format of Given, When and Then statements, where the Given statement specifies what all FRAMEWORK the pre-conditions are, the When statement specifies In this paper, we will describe how to write acceptance what all the conditions are, and the Then condition steps using the Cucumber Selenium WebDriver testing specifies what is the expected outcome. These steps framework. We will use IDE for our written in plain text hide the technicalities and implementation. complexities of the automation scripts bound underneath, thereby making it simpler for a non- To begin with, we would write BDD steps in Feature technical person to understand what really is supposed files. A corresponding Step Definition file would be to happen. These steps are written in Feature files in an generated wherein we would declare our functions and IDE. For an example, a short test is written below: specify the Java class where the actual script code of that function is located. Given I have navigated to the google search homepage To setup Cucumber in Eclipse, we need to add modules When I enter some text and click on google search or Jar files into Eclipse build path from the public button Then I would be shown some search results Maven repository. Similarly, for Selenium WebDriver Selenium WebDriver is an API that facilitates the direct Jars are available from the official Selenium website. communication of our code with the web browser, thus In our experiment, we make a test to navigate to Yahoo making it possible to perform the desired steps without homepage and login to check Mail. manual intervention. The WebDriver API provides us with various implementation classes for different Step 1: environments such as ChromeDriver, FirefoxDriver, We start by creating a Feature file where we define what HtmlUnitDriver, SafariDriver, InternetExplorerDriver, our test is going to do. The steps must be in the form of AndroidDriver, and so on for the various popular web Given, When and Then statements. browsers. Selenium WebDriver identifies each HTML element present in a web application by the use of Locators. Selenium supports 8 types of locators which are id, name, className, xPath, linkText ,partialLinkText, tagName and cssSelector. By appropriately utilizing Fig.2. FEATURE FILE ______ISSN (Print): 2319-2526, Volume -4, Issue -6, 2015 26 International Journal On Advanced Computer Theory And Engineering (IJACTE) ______

Above shown is a screenshot of the Feature file from the an instance of the Firefox web driver. It provides us Eclipse IDE. It may be noted that on line number 8, with a variety of functions which help us in the instead of writing the keyword When, we could also use identification of Web Elements. The navigate functions the keyword And, as the When keyword has already allows us to go to a particular URL. The findElement been specified in the preceding line. The keywords function allows us to uniquely identify Web Elements whose values we need to pass from the feature file are by the use of locators such as id, className, xpath, written in double codes format. partialLinkText, and so on. The sendKeys and click functions let us input a text into a text field and click on Step 2: a link respectively. Corresponding to our feature file, we create a separate Step 4: step definition class to declare our functions for each of our steps specified in the feature file. Now that we have written our Selenium automation code, we will go back to our Step Definition file and call these functions in those declarations as shown below.

Fig. 3. STEP DEFINITION FILE Fig. 5. STEP DEFINITION UPDATED FILE In the above clipping, we bind each step from our Step 5: feature file to a particular function declaration. The above bindings are automatically generated by Eclipse Now we run our code. The browser automatically when we try to run the feature file. Note that we have launches itself and navigates to the Yahoo homepage not as yet implemented our automation scripts, the where it clicks on the mail button. The email and above function declarations are just step skeletons. password are entered and we are signed in. A sample screenshot where the email and password gets entered Step 3: automatically is shown below. Now that we have created a step definitions file, we will now create a Java class which hosts the definitions of all the functions that we have declared for each of the feature file steps. It is in those definitions that the actual Selenium code will be written for all the actions that we wish to perform in a web application.

Fig. 6. AUTOMATED APPLICATION

III. ADVANTAGES OF CUCUMBER-SELENIUM AUTOMATION FRAMEWORK • The steps of the feature file hide the actual implementation of the Selenium code, thereby making it easier for anyone from a non-technical background to understand what really is happening. It bridges the gap between the technology and the business teams working on a testing project. • Reusability of code is another plus point of this Fig. 4. AUTOMATION SCRIPT FILE type of framework. Since Cucumber maps each and every step with a function or a method, we may come In the above shown code, we have made use of across another test case wherein we require a similar Selenium functions like findElement to identify specific step to be performed. In such a situation, we can just Web Elements in the Yahoo application. We first create reuse the step from the feature file.

______ISSN (Print): 2319-2526, Volume -4, Issue -6, 2015 27 International Journal On Advanced Computer Theory And Engineering (IJACTE) ______

• When the test data changes, we need not applications, we might not have access to correct test rework on the Selenium scripts that we have already data beforehand and may require to generate test data at written. Cucumber allows to change the values in the runtime by connecting to a mainframe database or some feature file itself. other type of database. In some other instances, the test data may be static wherein it would be more efficient • Both Cucumber and Selenium are open source and less time consuming to hardcode the data instead of tools. Operability is currently extended to a large variety fetching it from elsewhere. of environments and platforms. IV. DISADVANTAGES OF CUCUMBER- REFERENCES SELENIUM AUTOMATION FRAMEWORK [1] Antawan Holmes and Marc Kellogg, “Äutomating Functional Tests Using Selenium”, • Setting up Cucumber and Selenium framework Agile Conference, 2006, pp. 275-281, July 2006. can be a tedious and challenging task; more so if one is not aware of the testing requirements beforehand. For [2] Xinchun Wang, “Build an Auto Testing example, a simple change in our test case step will force Framework Based on Selenium and FitNesse”, us to alter our step definition functions, our feature file Information Technology and Computer Science steps as well as the automation scripts. If we simply use 2009, vol.2, pp. 436-439, July 2009. Selenium WebDriver without Cucumber, we will only [3] R. Hametner, “Agile testing concepts based on have to rework on our automation scripts. keyword-driven testing for industrial automation • Writing feature file steps would require us to systems”, IECON 2012, pp.3727-3732, October have an in-depth knowledge of the test data extraction 2012. strategy for our testing beforehand. Multiple strategies [4] http://cucumber.io/ would necessitate us to write our feature file steps in different ways which would increase the complexity of [5] http://www.seleniumhq.org/ our automation framework and make it more error [6] https://www.thoughtworks.com/insights/blog/%E prone. 2%80%8B3-essential-basics-setting-automation- • Even a minor change to the UI Web Elements suite-web-apps can render our Selenium automation script useless, [7] http://www.ibm.com/developerworks/library/a- thereby making our Cucumber test fail. Finding Web automating-ria/ Elements uniquely can be a challenging task depending upon the complexity of the web application. [8] Niranjanamurthy M, Arun Kumar R, SahanaSrinivas andManoj RK, “Research Study V. CONCLUSION on Web Application Testing Framework”, Though Cucumber and Selenium automation framework IJCSMC, vol.3, pp.121-126, October 2014. is a very flexible framework for testing, the [9] Fei Wang, “A Framework appropriateness of its usage on a particular application Based on WEB”, Computer and Information will be determined on a case to case basis. For example, Science ICIS, pp. 683-687, June 2012. if a project demands the automation to be executed on many different browsers, Selenium Grid may be a better [10] M. Leotta, D. Clerissi, F. Ricca, C. Spadaro, alternative to Selenium WebDriver, as execution time “Reparing Selenium Test Cases: An Industrial using WebDriver would be high. Using this framework Case Study about Web Page Element can also be a challenge if we do not follow a suitable Localization”,Proceedings of the Sixth strategy for obtaining test data. In the example International Conference on Software Testing, illustrated in this paper, we have hardcoded our test data pp.487-488, March 2013. directly into the feature file. In some instances of



______ISSN (Print): 2319-2526, Volume -4, Issue -6, 2015 28