Thursday, September 29, 2011
While doing an automation I came up with a problem where I need to get a list of all files in a Directory.
So just though to share the same with you the program to do so. I had made used of recursion in the following programs to get the list of files. Following are the two programs one will give you a ArrayList of files with their absolute path where as another will give you an ArrayList of File object of respective files.
Following program will store the absolute path of all the files inside a directory to the fileList argument provided.
Following program will store the File object of the files that are found into the respective fileList Array provided as an argument to the function
So just though to share the same with you the program to do so. I had made used of recursion in the following programs to get the list of files. Following are the two programs one will give you a ArrayList of files with their absolute path where as another will give you an ArrayList of File object of respective files.
Following program will store the absolute path of all the files inside a directory to the fileList argument provided.
Following program will store the File object of the files that are found into the respective fileList Array provided as an argument to the function
Friday, September 23, 2011
As there are more and more development happening on the android front, automation of the developed applications has also become a necessity. Android within its API supports a testing framework which can be used for testing applications, but writing and developing cases using that API is not easy. We may need a framework that makes it easy to write and develop test cases for our applications.
One of such framework is "NativeDriver". It is built over the Selenium webdriver automation framework.
As you must be knowing that selenium is vastly used open-source functional automation tool.
In this write-up I will tell you on how to automate an android application using NativeDriver
Following things are required before starting:
- Android SDK 2.2 or later - download
- Eclipse version 3.5 or later (Eclipse IDE for Java Developers recommended) - download
- Android Development Toolkit (ADT) plug-in - Installing ADT
- Ant - download
- JDK 1.6 or later - download
Building the NativeDriver libraries:
- Checkout the NativeDriver code:
svn checkout https://nativedriver.googlecode.com/svn/trunk nativedriver --username {Google account e-mail address}
- Build the NativeDriver code:
$ cd nativedriver/android $ ant
The libraries are built and stored in the nativedriver/android/build directory:
- server-standalone.jar - this should be linked to your Android application, and runs on the Android device (or emulator). This is the NativeDriver server, and listens for requests to automate the application, such as “start an activity” or “click a UI element”
- client-standalone.jar - this should be linked to the test, which is the NativeDriver client. It implements a WebDriver API which communicates with the server.
Adding the NativeDriver jar to your application
- Import your android application code into the eclipse by using the File -> Import functionality of eclipse.
- Add the server-standalone.jar to the build-path of your android application by Right clicking on the Android application project -> Configure build path -> Add jars or Add external jars (browse to the server-standalone.jar file, select and add it).
- To the application AndroidMainfest.xml file, add the following to the
element.
<instrumentation android:targetPackage="{app_package_name}"
android:name="com.google.android.testing.nativedriver.server.ServerInstrumentation" />
android:name="android.permission.INTERNET" />
android:name="android.permission.WAKE_LOCK" />
android:name="android.permission.DISABLE_KEYGUARD" />
Here {app_package_name} needs to be replaced with the name of the package as specified in the mainfest element's package attribute.- Build the application and install it on the device.
- Now go to the commandline and start the instrumentation using the following line:
adb shell am instrument {app_package_name}/com.google.android.testing.nativedriver.server.ServerInstrumentationHere again the {app_package_name} needs to be replaced with the name of the package as specified in the mainfest element's package attribute.
- Enable port forwarding by using the following command:
adb forward tcp:54129 tcp:54129
Writing your Test Cases
For writing your test cases you just have to create a new Java project and start writing code for it. I will explain this using a code example. The following code is done to automate an opensource K9 mail application on android:
Important Notes:-
- As there is no recording feature available, we need to write the code manually for automating our scenarios.
- For identifying the element id to interact on the UI, you can get the ID's of the elements from the R.java file generated inside your android application code.
- Also as we are writing our cases in Java code we can use our own test framework and generate html reports for out automation execution.
Note: Most of the material in this blog has been taken from the original site of the NativeDriver. The credit for such material goes to the original author.
Following is the link to it. http://code.google.com/p/nativedriver
Wednesday, August 17, 2011
Nowadays android is the latest thing in the market and many of the companies are working on android app development. With development comes the requirement to test an application too. As we all tester know that automation is one of the important part of testing, and is mainly used to reduce the time taken to execute the testcases. This gives time to testers to concentrate more on exploratory testing. Recently I was going through some group, and found out about some open-source frameworks that makes it possible to automate an Android native application. I went through two of such frameworks and will write about what I felt about them. The two frameworks I am going to talk about is Nativedriver and Robonium.
Nativerdriver- Is an opensource framework written over Selenium-2.0 Webdriver implementation. As we all know that selenium is a great functional automation tool for web-applications and gives you a cross-browser and cross-platform supports. The support for testing native application has enhanced the value of selenium. Nativedriver can be used when you are actually working along with the development team and has access to the source code of the andorid application that needs to be tested.
Robonium – Is also a great tool and is written by extending the Internal Android testing framework. This framwork supports a lot of functions which can be used to auotmate your application test. The main advantage of this framework is that , you can automate any application using this without actully having the source code of the app.
Advantages of Nativedriver:
- Built over the selenium webdriver hence can be easliy used by existing selenium users.
- Test are run as a normal Java unit test. And hence other frameworks can also be used for test-case execution or reporting.
- Ant or maven can also be used for execution of cases.
- Fast execution and reporting of testing results
- Need access to the source code of the application that needs to be tested.
- Sometime difficult to find element as support for finding elements by index is not available
Advantages of Robonium:
- Don't require the source code of the application for testing.
- Elements are easy to identify and work on.
- Tests are simple to write.
- Setup is simple.
- Can only be executed suing eclipse.
- Slow execution.
- Typing a normal text is not supported at this point.
Monday, August 8, 2011
Somebody recently asked me on how to scroll a page which is dynamically loaded. Something like the Facebook page where the updates gets loaded as you scroll down the page. As I never had such a requirement , I didn't knew a solution to it.
While searching I found people suggesting the "selenium.focus" function to be used as a solution, but that does not solve the problem I am talking about. The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page.
After lot of searching I was unable to get a proper answer to my problem and finally got an idea for implementing it. Its just a 4 to 5 lines of code that can be used in your code for scrolling on the page.
While searching I found people suggesting the "selenium.focus" function to be used as a solution, but that does not solve the problem I am talking about. The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page.
After lot of searching I was unable to get a proper answer to my problem and finally got an idea for implementing it. Its just a 4 to 5 lines of code that can be used in your code for scrolling on the page.
Following is the code:
The above code uses the JavaScript method "scrollBy" to scroll on the page. The for loop runs for 60 sec and calls the scrollBy function every second. This makes the selenium to scroll on the page.
If you have a test where you need to scroll on the page and check whether an element is loaded dynamically or not you can put a isElementPresent like function after the "driver.executeScript" to check visibility of your element.
Following is a test method written in webdriver that you can use to test the above function on the Facebook page:
The following code is written for Selenium-1.0 users:
Thursday, July 28, 2011
While going through Selenium-2.0 I found out that WebDriver does not have a function called isElementPresent(). This was one of the important functions that was used in Selenium-1.0. It was mainly used for waiting for an element to be available to take an action on it. To implement this in WebDriver you just need to write a method as mentioned below. You can then use this function with any type of "By"(By.id, BY.name, etc.)
The above function will return true in case the element is found on the page, else it will return false.
In case your want to wait for a element to be present you can implement it in the following way.
In case your want to wait for a element to be present you can implement it in the following way.
Monday, July 25, 2011
While doing automation testing many of us had thought that it is good to send the excetuion report mail after your test execution. In this blog I am going to tell two ways using which you can send a mail with to multiple E-mail addresses with your execution report attached.
Using ant:
Ant supports a mail task which can be used as one of your ways for sending a mail. Following target can be used to send a mail using ant:
In the above target you need to mention the following things:
mailhost – this the sender host of your mail box. You can this from your outgoing configuration of account in you mail client. For gmail it is “smtp.gmail.com”
port – this is the port at which the above mailhost support sending of email. This configuration also you can check in your mailbox configuration. By default it is “25” but for SSL and TLS support it will be different for different hosts.
User/password – some mail hosts need the username and password of the account mail box to authenticate the user while sending a mail. You need to provide this in the “user” & “password” attributes.
ssl – if you want to use ssl for contacting the mailbox you need to set this attribute value to “true”
subject – Subject of the mail you want to give.
to – you need provide the address of the email recepients here using the “address” attribute. You use a property also here. This property you can setusing the properties file and then importing to the build.xml
attachments – this can be used to attach your report as part of the mail and then send it.
Please note: If you get a MIME error or java mail error while running the above target. Please download a jar of “java mail” and put it to the lib folder of your ant installation.
Using Java code:
When I had this problem of sending the execution report as mail, I did it in a hard way by writing the following java code. In the following code I had used the javax mail API for composing and sending the mail.
Using ant:
Ant supports a mail task which can be used as one of your ways for sending a mail. Following target can be used to send a mail using ant:
In the above target you need to mention the following things:
mailhost – this the sender host of your mail box. You can this from your outgoing configuration of account in you mail client. For gmail it is “smtp.gmail.com”
port – this is the port at which the above mailhost support sending of email. This configuration also you can check in your mailbox configuration. By default it is “25” but for SSL and TLS support it will be different for different hosts.
User/password – some mail hosts need the username and password of the account mail box to authenticate the user while sending a mail. You need to provide this in the “user” & “password” attributes.
ssl – if you want to use ssl for contacting the mailbox you need to set this attribute value to “true”
subject – Subject of the mail you want to give.
to – you need provide the address of the email recepients here using the “address” attribute. You use a property also here. This property you can setusing the properties file and then importing to the build.xml
attachments – this can be used to attach your report as part of the mail and then send it.
Please note: If you get a MIME error or java mail error while running the above target. Please download a jar of “java mail” and put it to the lib folder of your ant installation.
When I had this problem of sending the execution report as mail, I did it in a hard way by writing the following java code. In the following code I had used the javax mail API for composing and sending the mail.
Thursday, July 21, 2011
People has kept asking me how to capture the width and height of an image/element using selenium. I tried to get an answer to it and the solution is very simple. The solution comes with Selenium itself.
Selenium-1 consists of certain functions for storing element values like getElementHeight, getElementWidth, getElementPositionLeft, getElementPositionRight, getElementPositionTop.
Similarly in Selenium-2(WebDriver) you can use the getSize() function which will return a "Dimension" class object. You can then use the getHeight() and getWidth() functions over it for getting the width and height of the element.
We can use these functions to get the position of the element and also its width and height.
Following is an example for getting the height and width of an image/element on the browser using Selenium-1.
Following is an example for getting the height and width of an image/element on the browser using Selenium-2(WebDriver).
Selenium-1 consists of certain functions for storing element values like getElementHeight, getElementWidth, getElementPositionLeft, getElementPositionRight, getElementPositionTop.
Similarly in Selenium-2(WebDriver) you can use the getSize() function which will return a "Dimension" class object. You can then use the getHeight() and getWidth() functions over it for getting the width and height of the element.
We can use these functions to get the position of the element and also its width and height.
Following is an example for getting the height and width of an image/element on the browser using Selenium-1.
height = selenium.getElementHeight("locaton to the image on the browser.(xpath or id)")
width = selenium.getElementWidth("location to the image on the browser.(xpath or id)")
Following is an example for getting the height and width of an image/element on the browser using Selenium-2(WebDriver).
height = driver.findElement("location to the image on the browser.(xpath or id)").getSize().getHeight();
width = driver.findElement("location to the image on the browser.(xpath or id)").getSize().getWidth()
Subscribe to:
Posts
(
Atom
)