Sunday, April 21, 2019

pytest : All about pytest

pytest : All about pytest




Index :
  1.  Installation x 2
  2.  Project structure
  3. Sample code
  4. Important commands x 7
  5. Run specific test case
  6. Raise pytest exception (pytest.raises)
  7. labels (pytest.mark.label_name ,pytest.mark.skip,pytest.mark.skip_if)
  8. Parameterize (pytest.mark.parametrize('x,y,res',[(1,2,3),(0,3,3)]) )
  9. Fixture (pytest.fixture(scope="module") )
  10. conftest
  11. pdb
Installation:
Using pip
pip3 install pytest
pip install selenium

Using requirements.txt : project>requirements.txt
selenium >= 3.0
pytest >= 3.10.1
pytest-html >= 1.20.0

pip install -r requirements.txt
or
pipenv install -r requirements.txt #Inside virtual env
Project structure

add file : - project>tests>test_filename.py

Sample Code
from selenium import webdriver
def test_1():
 driver=webdriver.Chrome("chromedriverpath")
 driver.get(url)
 objs=driver.find_elements_by_xpath("//button")
 for i in objs:
  i.click()
  1. open terminal
  2. cd to project
  3. excute command "pytest"
Important commands:
1.    pytest --pdb #runs the debugger when an err is encountered
2.    pytest -s #to "print" result in console
3.    pytest -v #verbose 
4.    pytest --maxfail=2 # limit no of fails
5.    py.test --html=1.html
6.    pipenv run pytest
7.    pipenv run py.test --html=1.html

pytest -v -k "add or something" # runs only methods with add ,something
pytest -h # help
pytest -v -x #to stop after 1st fail
pytest -v -x --tb=no # do not display stack trace
pytest -q # quiet mode
pytest -rsx #report skipped tests 
pytest --lf #runs only tests that failed on last attempt
# in eclipse Window > Preferences > Pydev> PyUnit = Py.testRunner and add above Parameters 

Execute specific method of class
pytest test_filename.py::test_method

Exception
def test_mytest():
with pytest.raises(ZeroDivisionError):
  1/0
  
Markers(labels)
@pytest.mark.label_name" .To run "pytest -v -m label_name"
@pytest.mark.skip(reason="any reason") #skip methods
@pytest.mark.skip_if(sys.version_info < (3,3),reason="some reason")

Parametrize
import pytest
@pytest.mark.parametrize('x,y,res',[(1,2,3),(0,3,3)])
def test_add(x,y,res):
    assert classname.add(x,y,res)

Fixtures(similar to @Before annotations in Junit test used for - db connection , etc.,)

@pytest.fixture(scope="module") #scope =module/session/function def fix():
    pass    #setup code
    yield   #run till here
    print("done") # tear down code

def fn(fix):
    #fix runs 1st

example:
from selenium import webdriver
@pytest.fixture()
def test_setup():
    global driver
    driver =webdriver.Chrome(executable_path="C:/Users/driver.exe")# Windows
    driver.implicitly.wait(5)
    driver.maximise()
    yield()

    driver.close()
    driver.quit()

conftest.py
You can put all fixtures inside this file and this file will execute before test starts


Debugger :
import pdb;
pdb.set_trace() # to break

Others 

Eclipse Setup :
 (if u are using eclipse : Window->Preferences --> pydev --> PyUnit --> Change the Test runner to "Py.test runner".)
Right Click over the file. Run As --> Python Unit-Test
Or press Ctrl+F9:-
It will prompt you to select the test

User can use fixture or setup-teardown
setup
import pytest
def setup_module(module):


tiredown
 
import pytest
def teardown_module(module):

Allure report
pip install allure-pytest
pip list | grep -i allure


pytest --alluredir=/Users/Documents/reports
allure generate /Users/Documents/reports

setup.py
#Add this to setup.py file:
from setuptools import setup
setup(
    # ...,
    setup_requires=["pytest-runner", ...],
    tests_require=["pytest", ...],
    # ...,
)


#And create an alias into setup.cfg file:
[aliases]
test=pytest
[tool:pytest]
addopts = --verbose
python_files = testing/*/*.py


#If you now type:
python setup.py test



Links 
#https://docs.pytest.org/en/latest/goodpractices.html#goodpractices
https://docs.pytest.org/en/latest/parametrize.html
https://docs.pytest.org/en/latest/example/parametrize.html#paramexamples
https://pypi.org/project/pytest-html/
https://pypi.org/project/pytest-csv/

 important read
 https://bytes.yingw787.com/posts/2018/12/10/data_driven_testing_three/
 https://gitlab.com/qalabs/blog/pytest-parametrize-example

important watch **
https://www.youtube.com/watch?v=o9pEzgHorH0
https://www.youtube.com/watch?v=RdE-d_EhzmA

hooks
https://stackoverflow.com/questions/21930858/pytest-parameterize-row-from-csv-as-a-testcase?rq=1 

Running pytest inside eclipse ide
Ref :https://stackoverflow.com/questions/37985589/how-to-debug-or-run-pytest-scripts-using-eclipse


Friday, April 5, 2019

mac : Adding custom paths

Adding custom paths


Permanently

>nano ~/.profile
any_name="/path"
save and close
>$any_name



Temporary (only for the current session)

>alias any_Name="cd /path"
>any_Name


Saturday, February 23, 2019

Python: Strptime


strptime

String to datetime object = strptime
datetime object to other formats = strftime
Jun 1 2005 1:33PM is equals to %b %d %Y %I:%M%p
%b  - Month as locale’s abbreviated name(Jun)
%d   - Day of the month as a zero-padded decimal number(1)
%Y   - Year with century as a decimal number(2015)
%I   - Hour (12-hour clock) as a zero-padded decimal number(01)
%M  - Minute as a zero-padded decimal number(33)
%p   - Locale’s equivalent of either AM or PM(PM)
 Use slicing to remove unwanted characters if necessary.

Example :
>>> import time
>>> time.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
time.struct_time(tm_year=2005, tm_mon=6, tm_mday=1,
                 tm_hour=13, tm_min=33, tm_sec=0,
                 tm_wday=2, tm_yday=152, tm_isdst=-1)

Sunday, February 17, 2019

Linux - Format USB Flash Drive

Linux - Format USB Flash Drive


  1. sudo apt-get install gparted
  2. By default it goes to the hard drive of your computer. 
  3. go to the top-right corner of the application and choose the removable media
  4. Before you go for formatting, unmount the disk by right-clicking on the partiti$
  5. right-clicking on it will show you the Format To option. Select FAT32 (Windows Compatible and Audio/Video Compatible).
  6. Edit > Apply all  Operations







Friday, February 15, 2019

Python : Simple CSV reader writer using Pandas

Sample CSV reader /Writer using pandas



import  pandas,os
p="/home/deepak/Python/testData/1.csv"

sheet=pandas.DataFrame( {"rollno":[1,2],"name":["a",'b']})
sheet.to_csv(p)

if(os.path.exists(p)):  
    Obj=pandas.read_csv(p)
    print(Obj.columns) #print all columns
    print(Obj['name'][1]) #Print 1 row 1 col value
    Obj.set_value(0,"name","deepak")
    Obj.to_csv(p)

Python : Change default 2.x version to 3.x in Linux

python : Change default 2.x version to 3.x in Linux



To open python 3x
python3

To install packages
pip3 install <package>




To change default

$python --verion
Python 2.4


# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode
 
 # update-alternatives --install /usr/bin/python python /usr/bin/python3.4 2
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python (python) in auto mode
 
 
$python --verion
Python3.6

#select versions
update-alternatives --config python

Saturday, February 9, 2019

Version Control: GIT basic Commands in Linux


 GIT basic Commands in Linux


https://rubygarage.org/blog/most-basic-git-commands-with-examples
~~~~~~~~~~Clone Repository~~~~~~~~~~~~~~~
cd Documents
git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~give new name to your repository~~~~~~~~~~~~~~
git remote add gitPython https://github.com/j-thepac/Python_snippets.git #set git
cd to git folder
git remote
-------------------------------

~~~~~~~~~~~~~~~~~~~~~~~~Add new file into Git repository~~~~~~~~~~~~~~~

git add file #staging area ready to commit
#git add --all # to add all files to staging area
git status
git commit
#enter the title of the file
git push #push it to current version
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~Remove files from stagin area~~~~~~~~~~~~~
git rm --cached my-file.ts # remove file from staging area
#git reset
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
git mv [file-original] [file-renamed] # rename file

Wednesday, January 30, 2019

Security:Nmap

NMAP 



Nmap : Is a Port scanner of a particular server / Machine
#apt-get install namp
#pip install python-nmap

#python
import nmap
n=nmap.PortScanner()
res=n.scan('scanme.nmap.org','22')
print(res)

#Shell
nmap 'scanme.nmap.org' -p 22,80

#Use Zenmap for GUI 

Tuesday, January 29, 2019

Security:Wireshark

WireShark Tut


Summary :
    Wireshark is the network Packet Analyzer .Used to Analyize individual packets in an network by filtering the data accumulated by running it.
The main power lies in filtering the packet accumumulated. The Filtering as below:
1. Open Wireshark
2. Select the wireless/wired network
3. CLick Start Capture  - Wirestarts starts capturing data in new window
4. Select any packet in the packet list screen . Each packet contains data wrt OSI layer is shown in Packet details window
5. Open the tree in the Packet Details window and select last child and notice the status bar at the bottom gives filter condition which can be use
Eg:If u have selected a packet
Now in the Packet details window , Open Ethernet > Source >Select Address>
Bottom part gives you "eth.addr" which can be used in the filter field.


Basics:
1. Wireshark > Pref >capture > Promiscuous mode  (Monitor Mode)
2. protocol:
    udp:not reliable,used in chat /vid Conf, Very Fast
    tcp:
    icmp : ping
3. DNS(tcp /udp): port 53
4. Adding Filter :
    udp.port == 53|| tcp.port == 53
    ip.src == ip addr
    ip.dst == ip addr
    tcp.srcport == 53
    tcp.dst == xx
    tcp.port == xx
    udp.prt == xx
    ip.addr == 192.168.1.2 && (tcp.port == 53 || udp.port == 53)
5. Filter : http (packet sniffing http: video time 1:40-"The Complete Wireshark Course: Beginner to Network Admin!" )
6. iptables -I OUTPUT -p icmp -j DROP # stop ur PC to reply to pings
7. detect malicious traffic in ur network.
    lsof -i :port
    kill psd
8.Wireshark Command Mode :
a.tshark -h # help
b.tshark -D # List Interfaces
c.tshark -i wlan1mon #listen on multiple interfaces : tshark -i wlan1mon -i wlan0mon
d.tshark -i wlan1mon -w /tmp/result.pcap #write to file , can be opned only using wireshark
note: Can only be written into temp folder
e.tshark -i wlan1mon -a duraton:10 -w /tmp/duration.pcap #run for 10 secs
f.tshark -i wlan1mon -f "port80 or port 53 or port 443" -b filesize:5 -a files:3 -w /tmp/result.pcap
#filter ports , Max file size is 5kbs ,autostop after 3 files
9.watch -a 1 "ls -la" # Monitor a folder for every second
10. iwconfig # tells the mode of the card
11. ifconfig #ip addrs and mac address of network card
12. lspci #list all pci devices

~~~~~~~~~~~~WireShark Monitor Mode~~~~~~~~~~~~~~~~~~
Wireshark > Pref >capture > Promiscuous mode  (Monitor Mode)

1.Activate on Network Card  Monitor MOde
#Method 1
ifconfig wlan1 down
iwconfig wlan1 mode monitor
ifconfig wlan1 up
#Method2 (make sure aircrack-ng is installed)
airmon-ng start wlan1
2. airodump-ng wlan1mon #survillance of network traffic
3. airodump-ng --channel channel_no --bssid bssid_no --path/file #saves files in pcap and other formats
4. wireshark path/file # to open file

~~~~~~~~~~~~~~~~~~~~~Nmap with Wireshark -----------
Nmap : Is a Port scanner of a particular server / Machine
#apt-get install namp
#pip install python-nmap

#python
import nmap
n=nmap.PortScanner()
res=n.scan('scanme.nmap.org','22')
print(res)

#Shell
nmap 'scanme.nmap.org' -p 22,80

Wifi Monitot Mode : AirCrack-Ng


Wifi Monitot Mode : AirCrack-Ng 

Pre Req :
  1. ifconfig wlan0 down
  2. iwconfig # mode : Managed
  3. airmon-ng start wlan1



Steps :
1. airodump-ng wlan1mon
Here :
#mac address of NETGEAR : 20:xx:xx:xx:xx:xx
#BSSID: MAC address , PWR=Power,CH=Channel,PSK=Pre-shared Key,ESSID= Nw Name

2.
airodump-ng --bssid 20:xx:xx:xx:xx:EA --channel 6 --showack -w log wlan1mon
#Stations = Mac Address of devices , Frames = Device Active
#Mac Address of Laptop  : 74:xx:xx:xx:xx:53

#-w to write the results into current directory  
3 aireplay-ng -0 40 -a 20:xx:xx:xx:xx::EA -c 74:xx:xx:xx:xx:53 wlan1mo
#Here 0: Diconnection code , 40: Packets , -a router , -c target mac

4. Use wireshark to analyze the log.cap file

Method 2:
 1. Use Airgeddon instead

Sunday, December 16, 2018

Python : Data Driven Automation Framework using JSON Loader

Python : Data Driven Automation Framework using JSON Loader



import json


class json_py():

    def __init__(self,jsonFilePath,**kwargs):

        self.d= self.__readJson(jsonFilePath)

        

    def get_values(self,testCaseName):

        result_dict=self.d[testCaseName][0]

        return(result_dict)

    

    def __readJson(self,sPath):

        try:

            with open(sPath,"r") as F:

                return(json.loads(F.read()))

        except FileNotFoundError:

            print("Please provide correct file path")

        except ValueError:

            print("JSON is invalid")


sPath='D:\\personal\\json_python_2.json'

j=json_py(sPath)


print(j.get_values("testcase3")["data2"])



OutPut:
['hi', 'deepak']

---------------------------------------------------------------------------------------
json_python_2.json

{
"testcase1":[{ "id":1, "data1":["hi","deepak"]}],
"testcase2":[{ "id":2, "data1":["data"]}],
"testcase3":[{ "id":3,
                "data1":["hi","deepak"],
                "data2":["hi","deepak"],
                "data3":[{"key":"value"}]

}]
}