Skip to main content

Posts

Showing posts from 2016

File upload and WebDriver (Where is my File?)

You may have come across file upload use case when working with WebDriver. File upload is quite easy since it is same as typing in a text box but herein we need to pass path of file to be uploaded. I used to keep a static file in src/test/resources folder of maven project and would use it for upload operation. But you can also generate file during run time using java.nio.file.Files class. Files class has createTempFile(java.lang. String, java.lang.String, java.nio.file.attribute. FileAttribute...) method which can be used to generate temporary files. For ex, you can generate a temporary pdf file and get back the File object as following - File file = Files. createTempFile ( "test-" + accountNumber, ".pdf" ).toFile(); generatePDF (file, StringUtils. repeat ( "Dummy PDF" , 10 )); And this is how generatePDF looks - public static File generatePDF(File file, String content) { try { Document e = new Document();

Recording curl request with JMeter Recorder

Though it is quite east to convert curl requests to corresponding JMeter request. At times you might be stuck with issue like I faced when uploading a file with  JMeter HTTP request In a gist I kept getting 404 error when using REStful service to upload a file. After days of investigations I found it that I should be using HTTP request implementation java and not HttpClient4. JMeter HTTPs Test Script recorder was of great help to debug this issue. This post describes the process of recording curl request through JMeter HTTPs Test Script recorder. If you have never used JMeter HTTPs Test Script recorder then create a new JMeter Plan and under WorkBench > Add > Non Test Element > HTTP(s) Test Script Recorder.  Specify Global Setting > Port as 8090 If we were using browser to record web application then we would configure its proxy to 127.0.0.1 (Since http proxy server would work on localhost) and port 8090 - Any way, we are not going to record browser

How to remove duplicate page object methods

You may come across situation when you find page object methods on various classes having similar operations. For ex in following example there are two different different methods doing almost same thing. Both methods get the count of ticket in opening and outgoing queue in a system. The only difference is the element locator. One method uses element locator openingTicketCount while other uses  outgoingTicketCount - public int getOpeningTicketCount() { final String count = fluent( openingTicketCount ). getText(); return count.length() == 0 ? 0 : Integer. valueOf (count); } public int getOutgoingTicketCount() { final String count = fluent( outgoingTicketCount ). getText(); return count.length() == 0 ? 0 : Integer. valueOf (count); } We can extract the common steps and create a new private method to encapsulate them - private int getTicketCount(By locator) { final String count = fluent(locator).getText(); return count.length() == 0 ? 0 : Integer. valueOf

What else should page object test?

As we know that objective of page object is to provide services offered by page (or part of it). In its true sense page object does not advocate asserting page condition in page object class. Except the page object constructor which can check if control is on right page on it, like following - But this is not the only use case of a check on page object. When returning data set from page object one could also check if data set is empty, for ex following method checks if collection of webelement is empty using Preconditions.checkState() method before returning it’s content - In the above code snippet, if element list is empty then page object method would throw IllegalStateException exception. This check could also be carried out on test method which receives the member name list. But I prefer to keep such checks on page object method and focus on assertion on test method. What do you think of this approach? Do you also have such checks in your page object methods?

Using chrome console to test xPath and css selectors

Since the advent of selenium there have been many plugin to test xPath / css selectors but you don’t need any of them if you have chrome browser. Using Chrome console you can test both xPath and css selectors. Launch website to be tested in chrome browser and hit F-12 and you would see chrome console opened in lower pane of application - Hit escape key and console would open another pane to write element locators - And now you can start writing xPath or css selectors in chrome console and test them - The syntax for writing css id - $$(“ ”) And hit the enter key. If your expression is right then html snippet of the application element corresponding to the css selector would be displayed - If you mouse over the html snippet in chrome console then it would highlight the corresponding element in application - If you want to clean console of previously written element selectors then just hit ctrl+L keys and chrome console would be empty again. Pro

Structure of test automation project

Have you come across situation when you did not know where you should keep the test class, page object or any supporting class. I am going to share some of the practices I follow when working on test automation project. Given that I work with java and maven , it solves the problem of the directory structure. Maven project may have many directory but these are the ones I use most - src/main/java - any non test class, i.e - page object etc src/main/resources - resources like - properties file, text file etc used by main classes src/test/java - the test classes src/test/resources - resources like - properties file, text file etc used by test classes. target - this directory is created automatically building a maven project and Let’s focus on src/main/java - this is a directory which contains all of non test code. I usually end up having following structure on src/main/java - src/main/java com.google core dataobjects groups model navi

Are your selenium element locators static ?

I usually declare element locator static as well as the helper method using them, since element locators is a class variable or constant and is not dependent on a specific instance of class. For example in the following code snippet, element locators - imageElement and teamDeletionSucessMessage are static. Also, method - isTeamDeletionSucessMessageDisplayed is static - From one of the SO answer, following can be the requirement to make method static - If you are writing utility classes and they are not supposed to be changed. If the method is not using any instance variable(s) or instance method(s). {some thing I consider most important when declaring a variable static} If any operation is not dependent on instance creation. If there is some code that can easily be shared by all the instance methods, extract that code into a static method. If you are sure that the definition of the method will never be changed or overridden. As static methods can not be overr

Processing JMeter Results using awk

awk is Pattern scanning and processing language. Its name is derived from its authors “ A ho, W einberger, and K ernighan”. Some of its features are: Awk views a text file as records and fields Awk has variables, conditionals and loops Awk has arithmetic and string operators Awk can generate formatted reports Syntax: awk '/search pattern1/ {Actions}      /search pattern2/ {Actions}' file Herein : search pattern is a regular expression. Actions – statement (s) to be performed. several patterns and actions can be used in Awk. file is the Input file. Single quotes around program avoids shell interpreting any special characters. Let’s Consider following jmeter summary file - summary +    41 in  15.4s =    2.7/s Avg:  2234 Min:   383 Max:  6974 Err: 0 (0.00%) summary +    57 in  21.5s =    2.6/s Avg:  2548 Min:   618 Max:  4528 Err: 0 (0.00%) summary =    98 in  32.5s =    3.0/s Avg:  2416 Min:   383 Max:  6974 Err: 0 (0.00%)