Log File Parsing In Python

In this tutorial you will learn how to create log file parser in python. Parsing a log file or any type of text file in order to extract specific information is not that hard if you know a bit of python and regex. Python itself is perfect for this kind of use and does not require any third party modules. Also, if you are new to python world, I wrote a quick python setup on windows tutorial in which you can look into if you want to quickly setup python environment.

In my day job, I was working on testing Skype for Business iOS application and it came to the point where I had to go through iOS application’s log files in order to see all the requests and received responses. I had to look for specific properties like:

<code><property name="saveMessagingHistory">Enabled</property></code>

Usually they were buried under bunch of other not so important text. While I was looking for specific things in those log files, I realized that it’s going to be really time consuming to go through log files manually. In order to save time I wrote the following python script. If you want to test this script with similar text file, you can download SfB iOS log file here.

Continue reading “Log File Parsing In Python”

Compile Standalone exe in Python

In this tutorial you will learn how to compile standalone exe in python with pyinstaller module. This is great for PyGame and PySide, but works for any python script. The best thing about pyinstaller is that creates one file and you don’t have to have options script file to compile something. Everything can be done through console with single command line.

There are couple of ways you can do this, but as far as I found, things like py2exe, cx_Freeze and others require specific options script file. It just took me too much time, all I wanted to do was simply compile python script to standalone executable without any dependencies and pyinstaller does exactly that.

Continue reading “Compile Standalone exe in Python”

Add friends on facebook (python)

This is the follow up to my first selenium post which gave a good example of how one could scrape facebook with python. If we go more into detail, the first post gave an example of how to invite friends to your facebook page with python using selenium package. As this was a good exercise, I wanted to do a follow up. The following python code mainly uses selenium with a bit of help from BeautifulSoup, which by the way is another great web scraping tool which I will start to use more often.

The code might be pretty straight forward for someone who’s familiar with python and selenium, but if you are not sure about something you can look for explanation below. Also, if you are new to python world, I wrote a quick python setup on windows tutorial in which you can look into if you want to quickly setup python environment.

By the way, when I was writing this I encountered a number of new ideas where I could go with selenium and beautifulsoup combined together. Here’s a video example of how the script actually works:

Requirements:
* Install BeautifulSoup4 pip install beautifulsoup4
* Install win32api, win32con pip install pypiwin32
* Install selenium pip install selenium
* Include type_text.py in project’s folder
* Download and install Mozilla Firefox (some versions doesn’t work with selenium, I used 41.0.1)
* Use MousePos.exe to adjust coordinates if necessary
* Set your facebook user’s language to English (US)

Whole project can also be downloaded here
Continue reading “Add friends on facebook (python)”

How to setup python on Windows

Apparently a lot of people are struggling with simple setup of python environment on Windows. When I first started coding with python, the environment setup took me longer than it should. I’m going to give simple fast instructions with easiest ways to setup python on Windows.

Install canopy

If you quickly need to setup python and write some code, I would suggest to go with Canopy. It’s great for scientific computing, data analysis and engineering. It’s free and includes 200+ most popular python packages.

Simply download the installer click next, next.. and you are half way done. When installation and environment setup is completed, select Editor from Welcome to Canopy window. Create a new file, write some code and run it by clicking on green triangle at the top menu or use Ctrl+R

Install python packages in canopy

To install python package in canopy, simply start canopy and from welcome menu select Package Manager. Select Available tab from the left side and type selenium in search field at the top. That should display selenium package. Select selenium package and click on blue install button. Now you can import selenium package in your scripts if you are using canopy.

Continue reading “How to setup python on Windows”

Invite friends to your facebook page with python (selenium)

Selenium is mostly used for writing test cases, but it can be used to automate repetitive actions on web. The selenium package itself doesn’t include a testing tool or framework, that’s why we’re going to use Python’s unittest module. If you haven’t got your python environment setup on windows already, here’s a tutorial on how to quickly setup python.

Requirements:
* Install win32api, win32con pip install pypiwin32
* Install selenium pip install selenium
* Include type_text.py in project’s folder
* Download and install Mozilla Firefox (some versions doesn’t work with selenium, I used 41.0.1)
* Use MousePos.exe to adjust coordinates if necessary
* Set your facebook user’s language to English (US)

Whole project can also be downloaded here Continue reading “Invite friends to your facebook page with python (selenium)”

Find and replace strings with python

python-big

The following code was designed for situation where you have a folder with about 50 subfolders and a script file in each subfolder. With a bit of changes code can be adjusted to similar situations and effectively used to save time. If you haven’t got your python environment setup on windows already, here’s a tutorial on how to quickly setup python.

import fileinput
import re
from tempfile import mkstemp
from shutil import move
from os import remove, close
import os

def main():
    folders = getFolderNames(r'C:\main_folder');

    for folder in xrange(0, len(folders)):
        files = getFileNames(r'C:\main_folder\%s' %folders[folder], '.py');

        for file in xrange(0, len(files)):
            findAndReplace(r'C:\main_folder\%s' %folders[folder], files[file]);

if __name__ == '__main__':
    main()

The code won’t work without getFolderNames, getFileNames, replace and findAndReplace functions which are explained below. In order for this script to work properly add necessary functions after def main() function or download find_and_replace.py from pythonicways GitHub page.

Continue reading “Find and replace strings with python”

Dynamic objects in squish (python)

b140e522-60b2-46da-8dd6-b73b41fe4481

Squish is a great automation tool to write test cases for Qt applications. By default it offers to find objects by symbolic name, which basically is the name of the object generated depending on it’s properties.

The problem is that with more complicated Qt applications there might be more complicated objects – dynamic objects. In simple terms –  you record a set of actions and squish generates a code for you, where each object is named using symbolic names. When you try to run just recorded test case it fails, because squish couldn’t find the object with the same properties. The actual object in software has changed it’s properties, but object under symbolic name that’s used in a test case has the old properties.

Another reason why you would want to use the following code is that a lot of times in development process object’s properties are changed. Once the new build comes out test automation developer has to update test scripts. It may not be that complicated if there’s only couple of them, but it becomes very annoying if you have more than 50 to edit.

Froglogic offers a couple of solutions, but in my case they didn’t work. So I had to come up with my solution. Using below code it’s possible to return a list of all visible object names.

Continue reading “Dynamic objects in squish (python)”