Sunday, 6 December 2015

ssh db connectivity

manual steps:
login forticlient

putty prodip port 2022
ssh ip2
redirects to mysql ip3

with SSHTunnelForwarder(
         ('ip1', 2022),
         ssh_password="user1",
         ssh_username="pwd1",
         remote_bind_address=('ip3', 3306)) as server:
            db = MySQLdb.connect(host='127.0.0.1',
                       port=server.local_bind_port,
                       user='sql_user',
                       passwd='sql pwd',
                       db='sql db')
            with db:
                cur = db.cursor()

Uncertain alert

 try:
            WebDriverWait(self.driver, 5).until(EC.alert_is_present(),
                                   'Timed out waiting for PA creation ' +
                                   'confirmation popup to appear.')

            alert = self.driver.switch_to_alert()
            alert.accept()
            print "alert accepted"
        except TimeoutException:
            print "no alert"

Authenticate during redirection

autoit.win_wait_active("Authentication Required",20)     ie window name is Authentication Required
 autoit.send("username{TAB}password{TAB}{ENTER}")

Thursday, 3 December 2015

Authentication at website level

self.driver.get("https://username:password@abcd.html")

Writing to blinking inputbox

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

actions = ActionChains(self.driver)
        actions.send_keys('dummylogin')
        actions.perform()

Current URL

b=self.driver.current_url

DB connection

db = MySQLdb.connect(host="205.23.1.45",user="testuser",db="nameofdb",port=3306,passwd="pwd")
        with db:
            cur = db.cursor()
               cur.execute("select * from abc where def=%s",order_no)
            rows = cur.fetchone()
            my_result=rows[0]
            self.assertEqual('101',str(rows[1]) )

Alert

alert = self.driver.switch_to.alert
alert.accept()

Functions outside test

def fill_data_1(self):
    a = self.driver.find_element_by_id('visa')
    a.click()

class SprintTests(unittest.TestCase):
   
    def setUp(self):
       
        self.driver = webdriver.Firefox()
        self.driver.get("http://abcd.html")
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
       
    def test_Amount_Orderid_CardDetails_checks(self):
            a = self.driver.find_element_by_xpath("/html/body/div/div/a")
        a.click()
        fill_data_1(self)

Automation Framework

htmlreport.py
************************************************************************
import unittest
import HTMLTestRunner
import os
from tests import SprintTests

dir = os.getcwd()

search_tests = unittest.TestLoader().loadTestsFromTestCase(SprintTests)

smoke_tests = unittest.TestSuite([search_tests])

outfile = open(dir + "\TestReport_Feature.html", "w")

runner = HTMLTestRunner.HTMLTestRunner(
    stream=outfile,
    title='Test Report',
    description='Feature Sanity'
    )

runner.run(smoke_tests)
*************************************************************************

tests.py

*************************************************************************
from random import randint
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from sshtunnel import SSHTunnelForwarder
import MySQLdb
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import autoit
                                                       
class SprintTests(unittest.TestCase):
   
    def setUp(self):

       
        self.driver = webdriver.Firefox()
        self.driver.get("https://abcd.html")
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
       
       
    def test_Amount_Orderid_CardDetails_checks(self):
        ## amount -ive
        #self.driver.get("https://abc.html")
        print "Sub test: Amount negative"
       
    def tearDown(self):
              self.driver.quit()
      
       
if __name__ == '__main__':
    unittest.main(verbosity=2)
*************************************************************************
       

Thursday, 27 August 2015

Upload file

a = driver.find_element_by_xpath('random_xpath')
a.send_keys("C:\Users\user123\Downloads\myimage.jpg")

back button

driver.execute_script("window.history.go(-1)")

Image found or not

if source code of image is:
<img src="/images/abc.png" />

code:
a=driver.find_element_by_xpath("//img[contains(@src,'/images/abc.png')]") if a.is_displayed(): b="Element found" else: b="Element not found"

Using select box

a.find_element_by_xpath("//select[@name='Food']/option[text()='Veg']").click()

Clearing a field

a = driver.find_element_by_name("phoneno")
a.clear()

Reading attributes of elements

a = driver.find_element_by_name("Password")
b=a.get_attribute('placeholder')

Entering data into elements

a = driver.find_element_by_id("username")
a.send_keys("i love python...n selenium")

Saving screeshot

driver.save_screenshot('test1_1.png')

Reading text of an element

a = driver.find_element_by_xpath("/html/body/div[4]/div/ul/li[1]/a")
b=a.text

Clicking an element

a = driver.find_element_by_xpath("/html/body/div[4]/div/ul/li[1]/a")
a.click()

Different methods to find an element

-By Xpath
a = driver.find_element_by_xpath("/html/body/div[4]/div/ul/li[1]/a")

-By ID
a = driver.find_element_by_id("username")

-By Link text
a = self.find_element_by_link_text("Sign Up!")

-By Name
a = driver.find_element_by_name("email")