How to create virtual env for Python

Virtual Environments (“virtualenvs”) keep your project dependencies separated.
They help you avoid version conflicts between packages and different versions of the Python at run time.

Before creating & activating a virtualenv:

$ which python
/usr/local/bin/python

Let’s create a fresh virtualenv using another version of Python (Python 3):
$ python3 -m venv ./venv

A virtualenv is just a “Python environment in a folder”:
$ ls ./venv
bin include lib pyvenv.cfg

Activating a virtualenv configures the current shell session to use the python (and pip) commands from the virtualenv folder instead of the global environment:
$ source ./venv/bin/activate
(venv) $ which python

/Users/dan/my-project/venv/bin/python3

To Install new libraries and frameworks

(venv) $ pip install requests

To get back to the actual Python environment:
(venv) $ deactivate

$ which python
/usr/local/bin/python

Advertisements

How to create list of List using List comprehension of Python

Here we are creating first a list using comprehension.

>>> l = [x for x in range(1,10)]
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Now we put 2 as fix element of sub list and it looks as below.
>>> l = [(x,2) for x in range(1,10)]
>>> l
[(1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2), (7, 2), (8, 2), (9, 2)]

Now lets create list of list.
>>> l = [(x,y) for x,y in zip(range(1,10),range(2,20,2))]
>>> l
[(1, 2), (2, 4), (3, 6), (4, 8), (5, 10), (6, 12), (7, 14), (8, 16), (9, 18)]

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)

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