Wednesday, October 4, 2017

Creating Exe file from a python Tkinter file application.

Today we will discuss about creating exe file from python for making windows application.
For this the basic requirement is Python to be installed and working on SUT and then the pyinstaller being install in the system.

To install pyinstaller we can do -

pip install pyinstaller for windows user
sudo pip install pyinstaller for Linux user.

This pyinstalled builds an exe from the py file which user can run in windows platform.

To make this happen we need a python script that is made with Tkinter which is used for Python UI development for windows platform.

For Python 2.7 the name of the package is Tkinter for Python 3 its called tkinter.
Now after we have all our requirement fulfilled next step is to create the exe.

For that the simple command is

pyinstaller.exe --onefile --windowed  file_name.py

This is will start the packaging for the exe file. Note here one file means it will make a one file installer, other type of packaging system is also available with pyinstaller like folder package etc.
Windowed parameter tells tells the pyinstaller that the exe is a windowed process rather than a background process.

Two folder will be generated - dist is the main folder which will the have the exe file and which we can export or sell to customers.


Something look like below -





Using python 2.7 with selenium and Unittest to automate a test step for a website.

Today i will tell how to Write a unit test for a test step involving checking 3 dynamic links that changes over time and the validity of those links.

For this the requirement is very easy we need Python 2.7 and selenium installed in system under test.
To install Python one can download from

https://www.python.org/download/releases/2.7.7/


After successfully installing python go to your cmd prompt and check for python by typing python and pressing enter.
If the python interpreter starts we are ready to go to next step else one needs to put the bin directory path in the system path. Google " how to set python path in system path" if python is not set in the system path.

Then we need to install selenium by simply using pip -

pip install selenium for windows
sudo pip install selenium for linux users.

After this we are ready to automate the site say "Example Site"

Next create a python file and name it as -TC_001.py

Write click on the file and open with IDLE.
For using unittest we have to use setup, test and teardown methods for the unittest to understand what we intend to do with the class.

In the teardown we have to create instance of the gecko or chrome driver which ever we are working with like this.


def setUp(self):
        # create a new Firefox session
        self.driver = webdriver.Firefox()
     
This tells the unittest to create a firefox driver session to interact with the browser.

Next we have to write the test method that has the core logic of test steps.

def test_verify_links_not_same(self):
        log.warning("Starting test of verifying links are not same below search box")
        self.driver.get("https://examplesite.com/")
        log.warning("opening site")

Now we need to parse the links and open each page individually to confirm that the links are working and then take a screenshot for our confirmation.

        temp = ""
        target = self.driver.find_elements_by_xpath('//*[@id="search-bar"]/div/div[1]/span')
       
        for paths in target:
            temp = temp + paths.text
           
        links =  ' '.join(temp.split(' ')[3:])
        log.warning("the links below the search box are " + str(links))

        path1 = self.driver.find_element_by_xpath('//*[@id="search-bar"]/div/div[1]/span/a[1]').click()
        time.sleep(3)
        self.driver.save_screenshot("Link1.png")
        self.driver.back()
        time.sleep(3)       
        path2 = self.driver.find_element_by_xpath('//*[@id="search-bar"]/div/div[1]/span/a[2]').click()
        time.sleep(3)
        self.driver.save_screenshot("Link2.png")
        self.driver.back()
        time.sleep(3)
        path3 = self.driver.find_element_by_xpath('//*[@id="search-bar"]/div/div[1]/span/a[3]').click()
        time.sleep(3)
        self.driver.save_screenshot("Link3.png")
        self.driver.back()
        time.sleep(3)

       
        log.warning("test_verify_links_not_same completed")



Here a thread sleep is put for any server lag that may happen while running the test.
Note: We are not handling any negative scenarios that may arise while running the test.
we can use try catch or explicit waits or even web driver wait before each element is interacted with (driver find by element).
After this we need to clean the test after running i.e close the driver which is running in teard own method.

         def tearDown(self):
       
               # close the browser window
                self.driver.quit()

This is how we can write a python unit test to automate a test step of verifying links in webpage and take a screenshot of each link after opening.