How to simulate touch actions using webdriver in Iphone or ipad

June 1, 2012 31 comments

I have been working with webdriver for last 6-7 months now. The project that am working on now includes opening the website in iphone/ipad and testing the features in mobile safari browser. Webdriver and Grid 2.0 supports running ipad/iphone tests in Grid. This essentially means that you can remotely trigger the tests that needs to be run in the device. It looks really cool, when you see the tests being run remotely that too through the WIFI in these devices.

Everything was running smooth for some time, until my development decided to add touch actions to iphone. This meant that they added doubleTap, singleTap actions for the elements in the page. Its while trying to handle these events in Webdriver that I came to know that IPhoneDriver doesn’t support any touchevents. You cannot use the ui.interactions.actions classes too since its not supported in IphoneDriver. After trying the webdriver events out for some days without any success, I moved on to using javascript to trigger these events.

Javascripts were getting successfully executed in the mobile safari. All I need to get was the correct javascript. After some trial and error and help from lukeis in selenium (who maintains iphonedriver) I could figure out some of the touch actions. Given below is an utility class which has this implemented. This will work any locator as its using the position of the element in the page.

Note:- This will work “only” if the touchevents are implemented using  jQuery. Apps that were created using Sencha will not work using this code. For details see comments

public class CustomTouchAction {

//Javascripts to trigger the touch events. This will be appended to the javascript string

private static String DOUBLETAP= “.trigger(‘touchend’).trigger(‘touchend’);”;
private static String SINGLETAP=”.trigger(‘tap’);”;

private static String SWIPERIGHT=”.trigger(‘swiperight’);”;
private static String SWIPELEFT=”.trigger(‘swipeleft’);”;

private WebDriver driver;
public CustomTouchAction(WebDriver driver) {
this.driver = driver;
}

/**
* Return the x coordinate of the element in the screen
* @param elementToLocate
* @return
*/
private int getElementXPosition( String elementToLocate){
return driver.findElement(elementToLocate).getLocation().getX();
}

/**
* Return the y coordinate of the element in the screen
* @param elementToLocate
* @return
*/
private int getElementYPosition( String elementToLocate){
return driver.findElement(By.xpath(elementToLocate).getLocation().getY();
}

/**
* Get the common part of all touch action javascript trigger
* @param elementToTap
* @return
*/
private String addCommonScript(String elementToTap){
return “window.jQuery(document.elementFromPoint(“
+ getElementXPosition(elementToTap) + “,”
+ getElementYPosition(elementToTap) + “))”;
}

/**
* Simulate double tap on the element in the screen
* @param elementToTap
*/
public void doubleTap(String elementToTap){
String javascriptToExecute =addCommonScript(elementToTap) +DOUBLETAP;
executeScript(javascriptToExecute);
}

/**
* Simulate single tap on the element in the screen
* @param elementToTap
*/
public void singleTap(String elementToTap){
String javascriptToExecute = “window.jQuery(document.elementFromPoint(“
+ getElementXPosition(elementToTap) + “,”
+ getElementYPosition(elementToTap) + “))”+SINGLETAP;
executeScript(javascriptToExecute);
}

/**
* Simulate the swipe action. This will swipe from right to left from the position of the element
* @param elementToSwipe
*/
public void swipeLeft(String elementToSwipe){
String javascriptToExecute = “window.jQuery(document.elementFromPoint(“
+ getElementXPosition(elementToSwipe) + “,”
+ getElementYPosition(elementToSwipe) + “))”+SWIPELEFT;
executeScript(javascriptToExecute);
}

/**
* Simulate the swipe action. This will swipe from left to right from the position of the element
* @param elementToSwipe
*/
public void swipeRight(String elementToSwipe){
String javascriptToExecute = “window.jQuery(document.elementFromPoint(“
+ getElementXPosition(elementToSwipe) + “,”
+ getElementYPosition(elementToSwipe) + “))”+SWIPERIGHT;
executeScript(javascriptToExecute);
}

/**
* Execute the javascript code using javascript driver
* @param javascriptToExecute
*/
private void executeScript(String javascriptToExecute){

((JavascriptExecutor)driver).executeScript(javascriptToExecute);
}

}

How to get the maximum RC utilization from Selenium Grid

July 24, 2011 Leave a comment

Like most of the people who start using selenium grid, I was also looking at ways to increase the Remote Control (RC) utilization of grid. My selenium hub console would show a lot of RCs under the available section, but they never become active. Even when there are many tests pending to be executed why would hub not use the available RCs. Initially I thought this would be a limitation with hub. But that was not the case. The problem was in the number of tests which I could trigger at the same time.

The basic rule of Grid utilization

In simple words, we can say that RC utilization is directly proportional to the number of tests that are triggered. You can have 100 RCs registered to your hub, but if your code doesn’t have the logic to trigger 100 tests simultaneously you are not going to utilize all the RCs.

Setting up Grid

To do this, first you need to make sure that the Grid is totally separate from the test code. It should be an independent setup which just receives input and processes it. So you should start your Grid separately and get it running. Now you can trigger multiple tests at the same time all pointing to the same hub. If you are using Hudson/Jenkins to trigger your tests then you can trigger multiple tests at once all pointing to the same hub so that Hub is going to have more requests coming in.

UI for selenium grid

March 13, 2011 Leave a comment

Its been more than an year since I started using selenium grid. My client requirements made me look into the source code of selenium grid – to understand how this works. Selenium grid is like an infrastructure in testing. You need it to be up and running all the time. This by itself has become another task for me and my team. The main problem with grid is its stability over time. All my tests run in Ubuntu machines. After running for around 15 to 20 hours the RC’s would become unresponsive. It would start giving null pointer exceptions saying RC’s are not available. Current solution is restarting all the RC s and Hubs.

The testing which I am working for very dynamic in nature. It would suddenly require more number of Remote controls to run the test. Same way, we would want to remove/add/restart RC. I was thinking it would be a good thing to have an application in place which would do this. How about having a web app which can be deployed to add and remove RC. Basically an UI for selenium grid.

My requirements are these

1. Able to provide IP address and portnumber of the RC and Hub

2. Remove the RC from the grid

3. Add RCs to the grid

4. Restart an RC.

Am planning to use RMI to implement this. In a different way though. Am halfway into it, but it got stalled due to my laziness :-(. I will be reworking on this from this week. Lets see how far this goes..

 

Update :- Seems like I wont need this. Thanks to Grid 2.0, we can do all this by extending selenium classes.

Categories: Uncategorized

What is Selenium Grid

March 6, 2011 1 comment

Selenium Grid is an open source tool which helps in web testing. It is something that uses Selenium tool at its core but provides few enhancements which makes it one of the best open source web testing tool out there in the market. Its capabilities include:

  1. Allows to run multiple tests in parallel and there by reduce the test time
  2. These multiple tests can be run against different browsers. So all you need to do is write one script change few config values and you can run your tests in different browsers.
  3. Allows to run tests on different OS platforms. Just like how you can run on different browsers you can run on multiple operating systems too

As with any test automation tool, there are limitations for grid too. To list out a few,

  • It has selenium at its core. So all limitations that apply for Selenium applies for grid too
  • Selenium grid by itself cannot run multiple tests in parallel. We need to use something like testNG or JUnit to provide multiple tests to the grid

You can find more about grid, and how to run the grid here http://selenium-grid.seleniumhq.org/



Categories: Uncategorized

How to run selenium tests using Selenium Grid in FF4

February 16, 2011 3 comments

So you have all your tests in selenium 1.x code and you want to run the tests in Firefox 4. Thanks to Selenium 2.0 (which supports FF4 and IE9), this can be achieved easily. Here is what you have to do

1. Download Selenium 2.0 standalone server from here

2. Copy this jar file to /seleniumgridfolder/vendor

3. Delete the existing jar file which starts with selenium-server*.jar

4. Open the grid_configuration.yaml file under the seleniumgrid installation folder

5. Add a new browser under environments as given below

- name:    “*firefox4″

browser: “*firefox C:\Program Files\Mozilla Firefox 4.0 Beta 11\firefox.exe (Or whatever is the path to Firefox.exe”

6. Launch your Hub.

7. Launch RC with environment as *firefox4

That’s it! Your tests will now run in Firefox 4.

Follow

Get every new post delivered to your Inbox.