Monday 6 August 2012

Eclipse plugin to generate dynamic text


Hello Everyone,

In this post, I would like to share my learning about how to create a eclipse plugin that generates the code in code editor dynamically. This was really a frustrating period for me, as I was working on this particular thing for more than 10 days. I did receive vital information from several blogs and websites - but the information were scattered and I had to assemble them. So, in this blog I am planning to explain the sequence of flow from the scratch to end in the process of creating an eclipse plugin that would generate/populate the code in code editor dynamically.

Step 1:

First step in this process is to create an eclipse plugin. I referred the blog post mentioned here to gather information, and this post provides us with the comprehensive information about the process of creating an eclipse plugin.

http://www.nakedtechnologist.com/?p=677
http://www.nakedtechnologist.com/?p=1179

Step 2:

After creating a plugin, we need to reference the external jars that we are planning to use in our program with the plugin project. With plugin project, we need to follow different procedure for referencing the jar files. Post mentioned below summarizes the steps involved in referencing external jars with plugin project:

http://stackoverflow.com/questions/5744520/adding-jars-to-a-eclipse-plugin

Step 3:

Next step is to make use of eclipse's IDocument class in our java program which would update our code editor with texts dynamically. Below code snippet can be used for it:


private IWorkbench wb = null;
private IWorkbenchWindow win = null;
private IWorkbenchPage page = null;
private IEditorPart part = null;
private CompilationUnitEditor compEditor = null;
private IEditorInput input = null;
private IDocumentProvider dp = null;
private IDocument doc = null;
int offset = 0;

wb = PlatformUI.getWorkbench();//.getActiveWorkbenchWindow().getActiveWorkbenchPage();
win = wb.getActiveWorkbenchWindow();
page = win.getActivePage();
part = page.getActiveEditor();
if (!(part instanceof CompilationUnitEditor))
return;
compEditor = (CompilationUnitEditor)part;
input = ((IEditorPart) compEditor).getEditorInput();  

dp = ((ITextEditor) compEditor).getDocumentProvider(); //editor.getDocumentProvider();
doc = dp.getDocument(input);//editor.getEditorInput());

try {
offset = doc.getLineOffset(doc.getNumberOfLines()-4);
    } catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
doc.replace(offset, 0, UpdateStr+"\n");
    } catch (BadLocationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

For further references, you can refer these posts:
http://wiki.eclipse.org/FAQ_How_do_I_insert_text_in_the_active_text_editor%3F

Step 4:
Usage of IDocument would require the reference of following jar files:

org.eclipse.jface.text;bundle-version="3.7.0",
 org.eclipse.ui.workbench.texteditor;bundle-version="3.7.0",
 org.eclipse.jdt.ui;bundle-version="3.7.0"

User has to reference these jar files as per the procedure mentioned in Step 2 of this post.

But even after referencing the jars, user has to ensure that these jar files are mentioned as part of 'Require bundle' package in 'Manifest.MF' file of eclipse plugin.

If these jar files are not mentioned as part of 'Require Bundle', user has to make them by following these steps:

1. Navigate to 'Dependencies' tab of 'Manifest.MF' file.
2. Under 'Required Plugins' section, search for the mentioned files and add them as required plugin.

These are the steps that I followed while developing the plugin. Please let me know if you face any difficulties during your setup.

Thanks!



Tuesday 26 June 2012

Selenium WebDriver - Browsermob Proxy Integration

Hello Everyone,

In this post, I would like to share my learning related with 'Selenium Webdriver' - 'Browsermob Proxy' integration. Browsermob proxy is one of the proxy that has been closely integrated with Selenium.

When we use browsermob proxy with Selenium, we can intercept the http requests and responses that pass through the established selenium browser session. Now, I would like to explain how to integrate Selenium Webdriver with browsermob proxy using java.

For any queries related to browsermob-proxy, you can refer the below website: http://opensource.webmetrics.com/browsermob-proxy/

Softwares required:

1. Latest Selenium java files from Selenium website (Version that I used here is Selenium 2.23).
2. Download the Browermob proxy zip file from the above mentioned website (Version that I used is Browsermob-proxy v6).
3. Mozilla Firefox version supported by Selenium (I used Firefox 8.0.1).

Steps:

1. Unzip the Browsermob-proxy zip file.
2. Create new project in Eclipse ide, lets say Browsermob.
3. In eclipse, right click on the project and choose 'Properties'.
4. Navigate to 'Java Build Path' and select the 'Libraries' tab.
5. Click on the 'Add External Jar' option.
6. Add all the jar dependencies of Selenium jar.
7. Then add the jar dependencies of Browsermob-proxy under 'lib' folder of the unzipped Browsermob-proxy zip file.
8. Now, create a new class and copy and paste the below code:


public class Test_One {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ProxyServer server = new ProxyServer(8105);
         server.start();
         server.setCaptureHeaders(true);
       
         server.setCaptureContent(true);
         server.newHar("test");
         DesiredCapabilities capabilities = new DesiredCapabilities();
         Proxy proxy = server.seleniumProxy();
         FirefoxProfile profile = new FirefoxProfile();
         profile.setAcceptUntrustedCertificates(true);
         profile.setAssumeUntrustedCertificateIssuer(true);
         profile.setPreference("network.proxy.http", "localhost");
         profile.setPreference("network.proxy.http_port", 8105);
         profile.setPreference("network.proxy.ssl", "localhost");
         profile.setPreference("network.proxy.ssl_port", 8105);
         profile.setPreference("network.proxy.type", 1);
         profile.setPreference("network.proxy.no_proxies_on", "");
         profile.setProxyPreferences(proxy);
         capabilities.setCapability(FirefoxDriver.PROFILE,profile);
         capabilities.setCapability(CapabilityType.PROXY, proxy);
         WebDriver driver = new FirefoxDriver(capabilities);
         driver.get("http://www.google.com");
         Har har1 = server.getHar();
         }
}

}


Execute the above code. You can observe that proxy would have been created in the port 8105 and all the Http Requests and Responses would be passed via the proxy.

You can further refer about HAR files in order to obtain the required information about how to access http requests & responses from the HAR files.

Please let me know if you have any queries on this.





Thursday 19 April 2012

Selenium Android User Actions


In this article, I would describe about how to emulate the various user actions such as doubletap, singletap, flick etc using Selenium in Android Mobile Browser.

Before getting into the technical details, as most of us know, Selenium is the leading test automation tool for web application and it works using the inbuild javascript support of the browsers. As almost all the browsers support javascripts, selenium can be used to automate web applications in virtually any browsers. Same applies to the browsers in mobile devices as well.

Android devices as an inbuild browser called ‘Android Browser’, and Selenium has a separate module that can be used to automate ‘Android Browser’.

Details about how to use ‘Selenium Android Package’ for mobile automation can be found in this link: http://code.google.com/p/selenium/wiki/AndroidDriver

Above web page has the detailed description about how to automate Android using Selenium (ofcourse we need patience during the initial setup, as we would commit some inadvertent mistakes while setting up).  So, I would give you a code snippet that performs user actions such as doubletap, singletap,flick using Selenium in this article.


AndroidDriver driver = new AndroidDriver();
driver.get(www.testapp.com”);
WebElement elem = driver.findelement(By.id(“name”));
      Action TchAct = new TouchActions(driver).doubleTap(elem).build();
      TchAct.perform();
TchAct = new TouchActions(driver).singleTap(elem).build();
      TchAct.perform();
                TchAct = new TouchActions(driver) .flick(element,0,-400,FlickAction.SPEED_NORMAL).build();
      TchAct.perform();

Here we are using ‘Action’ and ‘TouchActions’ classes of Selenium to emulate the user actions. And these actions emulates the user behavior as expected. As mentioned above, Selenium supports other user actions such as longpress, scroll, up, down etc.

Please let me know if you have any clarifications on this article.

Thursday 12 April 2012

robots.txt file of web sites

Have you ever wondered how the search engines such as google.com and bing.com retrieve the web sites when we provide search queries?

Every search engines has a web robots (spider bots), which crawl over the internet and indexes various web sites based on the complex logic. From these indexed set of pages, search engines would retrieve the pages appropriate to our search query.

These spider bots retrieve a web site and traverses across every hyperlink that the website would have and indexes the site content. For example, spider bot of google.com is googlebot and the spider bot of bing.com is bingbot.

These spider bots interacts with the robots.txt file of the website and determine the website pages that are not to be indexed.

Robots.txt file contains the information about various pages of the web sites and specifies the following details:

1. Specifies the robots that web site owner do not/want to index his/her website
2. Specifies the web pages that the user does not want the spider to index

You can access the robots.txt file of various leading websites by as below: http://www.example.com/robots.txt


For example:
http://www.espncricinfo.com/robots.txt
http://www.facebook.com/robots.txt
http://en.wikipedia.org/robots.txt
http://www.youtube.com/robots.txt

If you go through the above robots.txt file, you can observe that these websites specifically block certain spider bots and allows only certain advertisement related bots.

For more information, you can refer the below mentioned website: http://www.robotstxt.org/robotstxt.html


Tuesday 10 April 2012

Key combinations in Selenium 1 & 2


In this article, I would like to share my learning related to emulation of key combination press actions in Selenium 1 & 2. In our day to day activities, we would use the modifier keys number of times while working with the application. We would use ‘Alt+F4’ key to close the window, ‘Ctrl+C’ to copy the contents and several other key combinations for various activities.

In the aforesaid key combination (say for example-Ctrl+C), first part (Ctrl) of the combination is called as modifier key – as this key modifies the intended function of the second part of the key combination.

In the leading commercial test automation tools such as QTP and Silktest – required combination of key strokes can be easily achieved with the use of designated methods such as ‘type’ method and by using the appropriate constant that corresponds to the individual keys.

But in Selenium, using these key combination requires some better understanding about the intricacies of Selenium. So, in this article – I would explain about how to use these key combinations in ‘Selenium 1 & 2’.

In Selenium 1 – we can simulate the key combination action in a relatively simple way by using the following methods: KeyPress, KeyUp and KeyDown.

We just need to use the above mentioned methods in the instance of ‘DefaultSelenium’ object, with the appropriate ascii value of the key.

In case of more than 2 key combinations, such as ‘Shift+Alt+T’ - we can use the KeyDown and KeyUp methods to achieve the required key combinations, as below:

DefaultSelenium.KeyDown(<asciivalue of the key1>);
DefaultSelenium.KeyDown(<asciivalue of the key2>);
DefaultSelenium.KeyPress(<asciivalue of the key3>);

You can refer the below mentioned link to find the ascii value of various keys:  http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html

In Selenium 2 – to simulate the above mentioned key combinations, we would be required to use the ‘Actions’ class of Selenium Webdriver as below:

FirefoxDriver driver = new FirefoxDriver();           
driver.get("http://www.open2test.org");
Actions kpress = new Actions(driver);
kpress.keyDown(driver.findElement(By.id("name")), Keys.ALT).perform();
kpress.keyDown(driver.findElement(By.id("name")), Keys.SHIFT).perform();
kpress.sendKeys(driver.findElement(By.id("name")), "T").perform();
kpress.keyUp(driver.findElement(By.id("name")),Keys.SHIFT).perform();              kpress.keyUp(driver.findElement(By.id("name")),Keys.SHIFT).perform();

Here the objective is to simulate the press action of the key combination ‘Alt+Shift+T’. To perform this action, we are using the keyDown and SendKeys method of Selenium Action class.

I would post another article about F4 keys usage and action builder quite soon. If you have any questions in the above mentioned concepts, please let me know.

Monday 9 April 2012

Do Events - Visual Basic for Applications

This was one of my first learnings in test automation. It is around 3 years back when I started to learn test automation technique that I came across this method called 'Do Events'.

At that time, I was trying to write keyword driven framework for the tool 'eTester', which has a scripting language of 'Visual Basic for Application' (VBA). During the framework development, there was a requirement for for loop and I wrote the code for it.

Code was written in such a way that it would click my application's button and would get in to the for loop and then would perform some action in the screen resulting because of the click performed previously.

Strangely, when I executed this code - I found that my application did not proceed further after clicking on the button. But the code worked perfectly fine when I ran the same code in 'Debug' mode.

I was wondering what could be the reason and was going over the internet search engines to discover the root cause. Unfortunately, I could not find what is the exact cause. Then I discovered that the root cause of the issue is the loop function that I had written in VBA.

I came to know that VBA has a peculiar behavior of hogging the system resources big time whenever it executes For statements/While loops. VBA would not free the resources to the events present in OS message queues until the loop is complete.

To allow the processor to executing the pending events in parallel with the loop statements, VBA requires 'Do Events' statement to be placed within the loops.

You can find more about 'Do Events' definition and usage in the below links:

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents.aspx#Y0

http://www.mrexcel.com/forum/showthread.php?t=19342

http://office.microsoft.com/en-us/access-help/doevents-function-HA001228827.aspx

Please let me know if you have any further queries on this.