Sunday, 30 July 2017

Python | Verify test Util

Here is the code for VerifyTetsUtil in python

class VerifyTestUtil(object):
    def __init__(self):
        self.results = []
    def verify(self,condition,  message):
        if (condition==False):
            self.results.append(message)

    def addMessage(self,message):
        self.results.append(message)

    def getMessages(self):
        return self.results

==================================

Usage:

import urllib.request
import os
from selenium import webdriver
import unittest 
import HTMLTestRunner
from VerifyTestUtil import VerifyTestUtil

class Test1 (unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome("C:/forSelenium/chromedriver.exe")
        self.driver.get("https://www.amazon.com")
        print("testtttt")

    def test_login(self):
        verifyTestUtil = VerifyTestUtil()
        driver=self.driver
        verifyTestUtil.addMessage("msg1")
        self.assertEqual('bar', 'bar',"resultText")
        driver.find_element_by_id("twotabsearchtextbox").send_keys("toys and games")
        verifyTestUtil.addMessage("msg2")        
        driver.find_element_by_css_selector("input[value='Go']").click()
        resultText=driver.find_element_by_id("s-result-count").text
        c1=resultText.split("of")
        c2=c1[1].split("results")
        verifyTestUtil.addMessage("msg3")
        c3=c2[0].replace("results","").replace(",","").replace(" ","")
        results=verifyTestUtil.getMessages();
        for s in results:
            print(s)

    def tearDown(self):
        self.driver.quit()

if __name__=='__main__':
    HTMLTestRunner.main()

No comments:

Post a Comment