Wednesday, October 4, 2017

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.






Wednesday, September 27, 2017

Appium integration with windows platform.

Today i will talk about Appium which is automation tool and is open sourced, used to automate mobile applications. Appium is written in node.js and supports multiple languages to be implemented for iOS or Android mobile devices. We can test web/native applications using appium in mobile device.

This post is regarding how to integrate appium with Windows platform as it has many dependencies to get installed.

Dependencies of installing appium on widows -

  • JDK
  • Android SDK (adb/uiautomator)
  • Node JS
  • MS .Net Framework
  • Appium Client
  • An ide to work with code.
Installing all of the above will ensure that appium has a working environment in windows to be runnable.

After Installing the Appium client and its dependency launch the appium client. It will look like this in the below picture.


Basically it has 3 tabs Single, advanced and presets.

Single - 

You can select the host and port ip addresses here generally its kept as 0.0.0.0 and 4723 as default values.

Advanced - 

This will have some extra options like saving log path, binary path for chromedriver in android, executeAsync callback host and port in iOS. We can also save our presets or settings into a single name and run it every time with the selected settings every time we select the named preset.

Preset -

Contains a list of saved preset saved by user with required settings. User can load the preset and the saved settings will be loaded.

Start Server starts the appium server with the selected settings.

A started server will look like below picture -



Now environment is running all that is needed is a android device to connect and some code to run.
Remember to verify the developer options is turned on in your android device and as well usb debugging is turned on. After connecting the device do a adb device on the command prompt to check if device is detected by the system. If no device is shown you need to install device driver for your mobile device on your system.

If everything goes right the device can now be accessible through the appium intermediate where you can test application through code and automate user steps on mobile device.

P.S. Android setup installs adb and uiautomator which are useful tools while automating through appium.