Program to print all permutations of a given string

def displayString(l):
return ''.join(l)

def permutation(a, l, r):
if l==r:
print displayString(a)
else:
for i in xrange(l,r+1):
a[l], a[i] = a[i], a[l]
permutation(a, l+1, r)
a[l], a[i] = a[i], a[l]

This function is being called from here.
string = "biml"
n = len(string)
a = list(string)
permutation(a, 0, n-1)

Advertisements

Selenium Interview Question and answer.

Q. How to handle parent-child web locator of web page.
Ans.
Q. How to verify data of dynamic web table.
Ans. There are two types of HTML tables published on the web.
Static: Data is static i.e. Number of rows and columns are fixed
1. Find table say T
2. Find row say R using T.row[n]
3. Then find column say C of that row like R.col[n]
4. C.getText()

Dynamic:Data is dynamic i.e. Number of rows and columns are NOT fixed.
Q. How to handle java script Alert and popup.
Ans.
Q. What is exception and explain some most common used exceptions.
Ans.
Q. What is difference between elementNotFound and elementNotVisisble exception.
Ans.
Q. How to call external executable file in your selenium script.
Ans.
Q. How to handle windows dialog using selenium webdriver.
Ans.
Q. What is implicit and explicit wait in selenium.
Ans.
Q. How to handle multiple browser’s windows in selenium.
Ans.
Q. How to handle frames in selenium.
Ans.
Q. How to get and set text in Textarea element.
Ans.
Q. How to handle(select/deselect) drop down list using selenium.
Ans.
Q. How to handle multi select drop down list.
Ans.
Q. Difference between driver.quit() and driver.close().
Ans.
Q. Describe about ngWebDriver.
Ans. ngWebDriver – This is a way to automate angular based apps in selenium using java.
As we know we can use selenium webdriver and java to test angular js controls, but there are few limitations we need to stick to one locator strategy as selenium itself do not have locators method specific to handle angular controls like ngbinding, ngrepeater etc…), and sometime angular element’s actions lag behind the selenium line by line execution.
To overcome above issues angular js controls with selenium and java, a library known as ngWebDriver came out to add features to selenium and can write the scripts in java
ngwebdriver basically taken the advantage of other language like protractor and passing the javascript to browser to handle angular controls and also allows to write scripts in java language without any synchronization issue.

Let’s list our few angular controls that are different than normal html elements
ng-model
ng-repeat
ng-binding

If you observe, we can not use these controls value directly using selenium webdriver and java like id, tagname, link etc..

but ngwebdriver has capability to identify the above elements directly, let’s see
byAngular.binding(Str)
byAngular.model(str)
byAngular.options(str)
byAngular.repeater(str) – Angular web table

For setup simply add ngwebdriver-x.x.jar file to your existing selenium project and write below code.
You can download jar file from http://search.maven.org/#search%7Cga%7C1%7Cngwebdriver

public class NGDriverTest {
private static NgWebDriver ngDriver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "d:\\test\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://bimlesh.ultimate.com");
ngDriver = new NgWebDriver((JavascriptExecutor) driver);
ngDriver.waitForAngularRequestsToFinish();
driver.findElement(ByAngular.model("one")).sendKeys("1");
driver.findElement(ByAngular.model("two")).sendKeys("2");
//driver.findElement(By.id("forward")).click();
driver.findElement(ByAngular.buttonText("back!")).click();
}
}

Q. What type all type of options are available headers parts of http requests.
Ans.

Manage the test teams effectively

#1. Initiate the test planning activities for test case design and encourage the team to hold review meetings and ensure that the review comments are incorporated.

#2. During the testing cycle, monitor the test progress by constantly assessing the work assigned to each of the resources and re-balance or re-allocate them as required.

#3. Check if there might be any delays in achieving the schedule and hold discussions with testers to figure out the issues that they may be facing and strive hard to resolve them.

#4. Hold meetings within the test team to make sure everyone is aware of what the other fellow team members are doing.

#5. Present the timely status to the stakeholders & management and instil confidence about the work being done.

#6. Prepare any risk mitigation plans if in case any delays are foreseen.

#7. Bridge any gaps and differences between the testing team and the management in order to form a clean two-way interface channel.

ETags and Optimistic Concurrency Control

Another use for Etags is Concurrency Control, there we can use for Pessimistic and Optimistic.
Here i will explain specifically Optimistic Concurrency Control.

This helps solve the Lost Update problem which is what happens when multiple people edit a resource without knowledge of eachothers’ changes. In this scenario, the last person to update a resource “wins”, and previous updates are lost.

For Example
Let’s say that 2 API clients both GET a resource example.com/api/article/4, retrieving a blog article with an ID of 4:

$ curl -i example.com/api/article/4

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:09:50 GMT
Last-Modified: Sat, 02 Feb 2013 12:02:47 GMT
Content-Type: application/json

{
id: 4,
content: "Heres a blog article, it has lots of content...",
status: "published".
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 02 Feb 2013 12:02:47 GMT",
}

Now, client A notices a grammar error with the word ‘Heres’ and decides to fix it. Client A issues a PUT request, with updated text.

$ curl -i -X PUT -d "content=Here's a blog article, it has lots of content..." example.com/api/article/4

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:11:46 GMT
Last-Modified: Sat, 09 Feb 2013 16:11:46 GMT
Content-Type: application/json

{
id: 4,
content: "Here's a blog article, it has lots of content...",
status: "published".
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 09 Feb 2013 16:11:46 GMT",
}

Now, around the same time, client B decides to add some content. However, client B doesn’t know about client A’s change! Client B makes an update:

$ curl -i -X PUT -d "content=Heres a blog article, it has lots of content, and now has an extra sentence..." example.com/api/article/4

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:12:02 GMT
Last-Modified: Sat, 09 Feb 2013 16:12:02 GMT
Content-Type: application/json

{
id: 4,
content: "Heres a blog article, it has lots of content, and now has an extra sentence...",
status: "published".
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 09 Feb 2013 16:12:02 GMT",
}

The incorrect ‘Heres’ is back! Client A’s update is lost; We have experienced the Lost Update Problem.

How ETags can help
ETags can be used in combination with the If-Match header to let the server decide if a resource should be updated.

This works by letting a client know that a resource has been updated since the last time they checked. The server informs the client via a 412 Precondition Failed response.

>If a resource changes, so does it’s ETag. In this way, each “version” of the resource has a unique ETag. If a client attempts and update with a mismatching ETag, then the update is cancelled and a 412 status is given.

This is how the previous example would play out if ETags were employed.

The 2 API clients both GET the resource /api/article/4, retrieving the article. This time, an ETag is returned.

$ curl -i example.com/api/article/4

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:09:50 GMT
Last-Modified: Sat, 02 Feb 2013 12:02:47 GMT
ETag: c0947-b1-4d0258df1f625
Content-Type: application/json

{
id: 4,
content: "Heres a blog article, it has lots of content...",
status: "published".
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 02 Feb 2013 12:02:47 GMT",
}

Now, client A once again notices the grammar error and decides to fix it. Client A issues a PUT request, with updated text and the ETag received in the first request, in the form of the If-Match header.

$ curl -i -X PUT -d "content=Here's a blog article, it has lots of content..." -H "If-Match: c0947-b1-4d0258df1f625" example.com/api/article/4

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:11:46 GMT
Last-Modified: Sat, 09 Feb 2013 16:11:46 GMT
ETag: df35b-af-36f3b324a3ff6
Content-Type: application/json

{
id: 4,
content: "Here's a blog article, it has lots of content...",
status: "published".
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 09 Feb 2013 16:11:46 GMT",
}

What happened here? The ETag given by Client A matches the ETag of the resource, and so we know that the resource has not changed without Client A’s knowledge. The update is performed, and a new ETag is generated for that resource.

Around the same time, client B decides to add some content. However, client B doesn’t know about client A’s change! Client B attempts an update, with the old ETag:

$ curl -i -X PUT -d "content=Heres a blog article, it has lots of content, and now has an extra sentence..." -H "If-Match: c0947-b1-4d0258df1f625" example.com/api/article/4

HTTP/1.1 412 Precondition Failed
Date: Sat, 09 Feb 2013 16:12:02 GMT

Here we see that the update did not occur. The ETag used in the If-Match header did not match the ETag of the resource. Client B therefore received a 412 Precondition Failed response and then knows it needs to update it’s knowledge of the resource before attempting to update again.

Notes
This type of concurrency control puts a higher onus on the client. The client would need to implement ETags, using If-Match headers and handling 412 responses.
Note that the server needs decide to what to do if no If-Match header is present – Does it enforce its use, or only use concurrency control when it has the needed headers? The answer to this may boil down to your particular needs.

API development: ETags and Conditional Get

My curiosity about ETags (Entity Tags) was first piqued when reading over Github’s API documentation. Looking into them, I saw that there explanation often pointed to them as a “cacheing” mechanism.

In fact, it is – or at least can be. There are two uses for ETags:

Conditional GET (the so-called “cacheing mechanism”)
Concurrency Control
I will cover Conditional GETs here.

Conditional GETs allow a client to ask a server if a resource has changed. If it has not changed, it can assume it’s current knowledge is up to date. If it has changed, the server will send the resource back to the client.

An Example
Let’s say you have a resource. This resource is a To Do item in your To Do list. It’s reachable at example.com/api/todo/4.

A client request might look like:

$ curl -i example.com/api/todo/4
The servers response:

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:09:50 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Sat, 02 Feb 2013 12:02:47 GMT
ETag: "c0947-b1-4d0258df1f625"
Content-Type: application/json

{
id: 4,
item: "take out the trash",
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 02 Feb 2013 12:02:47 GMT",
}

Pretty standard. We ask for a resource and get it returned to us. Notice, however, that the result has an ETag. How can a client use that information?

Well, if our client stored this To Do response, it can later use the ETag to see if that To Do has changed.

To accomplish this, the client uses the “If-None-Match” header with the ETag set as the value. It’s asking the server “Return the resource if it’s ETag does not match mine.” Because an ETag changes when a resource has changed, this effectively asks the server to return the resource only if it has changed.

Note that a “If-Match” and “If-Range” header is also defined in the HTTP 1.1 spec.

Let’s see an example of that.

Here’s what the client requests:

$ curl -i -H "If-None-Match: c0947-b1-4d0258df1f625" example.com/api/todo/4
If we suppose that the To Do items has NOT changed, we may see this result:

HTTP/1.1 304 Not Modified
Date: Sat, 09 Feb 2013 16:09:50 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Sat, 02 Feb 2013 12:02:47 GMT
ETag: "c0947-b1-4d0258df1f625"
If, however, the To Do items HAS changed, we may see this result:

HTTP/1.1 200 OK
Date: Sat, 09 Feb 2013 16:29:24 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Sat, 02 Feb 2013 14:33:21 GMT
ETag: "c7493-d7-a6b64d37f6cc3" # New ETag!
Content-Type: application/json

{
id: 4,
item: "Take out the trash, TODAY!",
created: "Sat, 02 Feb 2013 08:29:53 GMT",
updated: "Sat, 02 Feb 2013 14:33:21 GMT",
}

If the server matches an ETag given by a client to the Etag on a server (for a given resource), then the server knows the resource hasn’t changed since the last time the client checked for it. The server can return a 304 Not Modified response, rather than the resource itself.

This process is called a conditional get.

Implementation notes
The devil is in the details with implementing ETags.

Generating an ETag
The generation of ETags is not specified in any HTTP spec. However, it is common to use a hashing mechanism, such as MD5, or a SHA-* variant.

An ETag’s job, in context of a conditional get is to differentiate a resource from another version of itself. If a resource is updated (changed), so to must its ETag.

Therefore, generating an ETag needs to be tied to the value of the resource and/or related meta data. An example might be to generate an md5 based on ID, content and update timestamp.

// Find a To Do item
$todo = Todo::find($id);

// Generate its ETag
$etag = md5( $todo->id . $todo->description . $todo->updated_at );

Performance Gains
On the face of it, ETags have 2 advantages:

Saving in bandwidth by returning 304 response with no body content
Saving in query/server computation time
Bandwidth
This is straight-forward. Bandwidth savings are real in this implementation as a server can send back a bodyless 304 response.

Query/Computation [server load]
This is where it gets interesting. On each request, the server needs to have an ETag to check against.

In order to know if your content has changed, the server needs to generate an ETag for it. This is what I did in the example above.

However, you may note that this (potentially) requires querying data on each request, whether the server sends back a response body or not.

In that respect, our savings seems to only be in bandwidth, and not necessarily in query or computation (server load). Based on this assumption, ETags for Conditional GETs become a feature of scale, where cost savings would be most realized.

Surely, however, ETags can provide benefit for query/computation time? Indeed.

One strategy is to generate and store an ETag anytime a resource is updated or created (on any PUT or POST request).

This results in an ETag always being available to check against. The query on each request then becomes one of looking up and matching the ETag, rather than generating an ETag on the fly. While there is still some querying to do, this is generally less than what it would take to generate an ETag on each request, especially if an in-memory cache is available for use.

Generator to get value of key from any dictionary(json)

This will return list of value that matches to key we provided. This will accept only json/dict, if not then need to json.loads(str) to make dict.


def search_value_from_json(json_str="This could be a json/dict", search_key="what to search from given json/dict"):
if search_key in json_str:
yield json_str[search_key]
for key in json_str:
if isinstance(json_str[key], dict):
for searched_key in search_value_from_json(json_str[key],search_key):
yield searched_key

elif isinstance(json_str[key], list):
for item in json_str[key]:
for searched_key in search_value_from_json(item,search_key):
yield searched_key

USAGES:
list_value = list(search_value_from_json(json_str, key_to_find))

How to use headless Chrome browser with selenium(python)

Using Chrome Headless

The first thing you need to do to switch to Chrome is to install Chrome if you don’t have it yet (obviously!), and then you need to install ChromeDriver, which is the little bit of glue that allows Selenium to send commands to Chrome and automate it. If you are on a Mac, then brew install chromedriver is all you need to do. On other platforms, download an installer from the ChromeDriver site: https://sites.google.com/a/chromium.org/chromedriver/downloads.

To switch to Chrome, you just need to change the initialization of the client variable:

try:
client = webdriver.Chrome()
except:
pass

But this will still use a regular Chrome window. If you want to use the headless option, you have to add options:

options = webdriver.ChromeOptions()
options.add_argument('headless')
try:
client = webdriver.Chrome(chrome_options=options)
except:
pass

If you want to switch back to the regular mode, just comment out the options.add_argument('headless') line and you’ll get a visible window that you can watch while the tests run.

How to Install ROBOT framework(python)

Install python 2.7 (not 3.0)
Install wxPython
To Install Pip(curl -O https://bootstrap.pypa.io/get-pip.py) and other packages or C:\pip_script>python get-pip.py
follow steps from this link: http://simpledeveloper.com/how-to-install-easy_install/
you get pip package: https://bootstrap.pypa.io/get-pip.py
easy_install pip
Once above pip is completed then you can install any package using pip command.
Install robotframework and their support files now:
pip install robotframework
pip install decorator
pip install selenium
Download and install pycrypto (PyCrypto 2.6 for Python 2.7 64bit) http://www.voidspace.org.uk/python/modules.shtml#pycrypto.
– pip install pycrypto(this is not required if above is ok)
pip install paramiko
pip install robotframework-selenium2library(https://github.com/rtomac/robotframework-selenium2library/downloads)
pip install robotframework-sshlibrary
pip install robotframework-databaselibrary
pip install pyodbc (http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc)

export PATH=$PATH:/usr/bin:/opt/python/latest/usr/local/bin:/usr/bin/gcc:/opt/python/latest/usr/local/lib/python2.7/site-packages

 

Install PIP on linux environment(centOS)

Prerequisite: Python must be installed.

Download pip installer:
wget https://bootstrap.pypa.io/get-pip.py

If some certificate issue comes then use:
wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate

Go to download dir and run command:
python get-pip.py

This will give some warning – ignore that.

More about that can be found at:
https://pip.pypa.io/en/stable/installing/

Example of Thread based programming in C++

Example of Thread based programming in C++:

Designing a Thread Class in C++

This section is written by Ryan Teixeira and the document is located here .
Introduction

Multi threaded programming is becoming ever more popular. This section presents a design for a C++ class that will encapsulate the threading mechanism. Certain aspects of thread programming, like mutexes and semaphores are not discussed here. Also, operating system calls to manipulate threads are shown in a generic form.
Brief Introduction To Threads

To understand threads one must think of several programs running at once. Imagine further that all these programs have access to the same set of global variables and function calls. Each of these programs would represent a thread of execution and is thus called a thread. The important differentiation is that each thread does not have to wait for any other thread to proceed. All the threads proceed simultaneously. To use a metaphor, they are like runners in a race, no runner waits for another runner. They all proceed at their own rate.

Why use threads you might ask. Well threads can often improve the performance of an application and they do not incur significant overhead to implement. They effectively give good bang for a buck. Imagine an image server program that must service requests for images. The program gets a request for an image from another program. It must then retrieve the image from a database and send it to the program that requested it. If the server were implemented in a single threaded approach, only one program could request at a time. When it was busy retrieving an image and sending it to a requestor, it could not service other requests. Of course one could still implement such a system without using threads. It would be a challenge though. Using threads, one can very naturally design a system to handle multiple requests. A simple approach would be to create a thread for each request received. The main thread would create this thread upon receipt of a request. The thread would then be responsible for the conversation with the client program from that point on. After retrieving the image, the thread would terminate itself. This would provide a smooth system that would continue to service requests even though it was busy serviceing other requests at the same time.
Basic Approach

The create a thread, you must specify a function that will become the entry point for the thread. At the operating system level, this is a normal function. We have to do a few tricks to wrap a C++ class around it because the entry function cannot be a normal member function of a class. However, it can be a static member function of a class. This is what we will use as the entry point. There is a gotcha here though. Static member functions do not have access to the this pointer of a C++ object. They can only access static data. Fortunately, there is way to do it. Thread entry point functions take a void * as a parameter so that the caller can typecast any data and pass in to the thread. We will use this to pass this to the static function. The static function will then typecast the void * and use it to call a non static member function.
The Implementation

It should be mentioned that we are going to discuss a thread class with limited functionality. It is possible to do more with threads than this class will allow.

class Thread
{
public:
Thread();
int Start(void * arg);
protected:
int Run(void * arg);
static void * EntryPoint(void*);
virtual void Setup();
virtual void Execute(void*);
void * Arg() const {return Arg_;}
void Arg(void* a){Arg_ = a;}
private:
THREADID ThreadId_;
void * Arg_;

};

Thread::Thread() {}

int Thread::Start(void * arg)
{
Arg(arg); // store user data
int code = thread_create(Thread::EntryPoint, this, & ThreadId_);
return code;
}

int Thread::Run(void * arg)
{
Setup();
Execute( arg );
}

/*static */
void * Thread::EntryPoint(void * pthis)
{
Thread * pt = (Thread*)pthis;
pthis->Run( Arg() );
}

virtual void Thread::Setup()
{
// Do any setup here
}

virtual void Thread::Execute(void* arg)
{
// Your code goes here
}

It is important to understand that we are wrapping a C++ object around a thread. Each object will provide an interface to a single thread. The thread and the object are not the same. The object can exist without a thread. In this implementation, the thread does not actually exist until the Start function is called.

Notice that we store the user argument in the class. This is necessary because we need a place to store it temporarily until the thread is started. The operating system thread call allows us to pass an argument but we have used it to pass the this pointer. So we store the real user argument in the class itself and when the execute function is called it can get access to the argument.

Thread(); This is the constructor.

int Start(void * arg); This function provides the means to create the thread and start it going. The argument arg provides a way for user data to be passed into the thread. Start() creates the thread by calling the operating system thread creation function.

int Run(void * arg); This is a protected function that should never be tampered with.

static void * EntryPoint(void * pthis); This function serves as the entry point to the thread. It simply casts pthis to Thread * and

virtual void Setup(); This function is called after the thread has been created but before Execute() is called. If you override this function, remember to call the parent class Execute().

virtual void Execute(void *); You must override this function to provide your own functionality.
Using The Thread Class

To use the thread class, you derive a new class. you override the Execute() function where you provide your own functionality. You may override the Setup() function to do any start up duties before Execute is called. If you override Setup(), remember to call the parent class Setup().
Conclusion

This section presented an implementation of a thread class written in C++. Of course it is a simple approach but it provides a sound foundation upon which to build a more robust design.

If you have comments or suggestions, email to bimleshsharma@gmail.com