Tuesday, December 1, 2015

Selenium python for webpage verification and validation

Selenium python for webpage verification and validation:
Selenium is used to test/automation the UI web-testing.
This below is to use selenium to test url( google.com) and search and submit the button.
if the searching element and submit button is not available then test fails and meanwhile it will send the query and click the submit button.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import ElementNotVisibleException

class Web:
    def __init__(self):
        self.driver = webdriver.Firefox()
        self.driver.wait = WebDriverWait(self.driver, 5)


class TestClass(unittest.TestCase):

    @classmethod    
    def setUpClass(cls):
        webObj=Web()
        cls.driver=webObj.driver

    def test_lookup(self):
        query="Selenium"        self.driver.get("http://www.google.com")
        try:
            box = self.driver.wait.until(EC.presence_of_element_located(
                (By.NAME, "q")))
            button = self.driver.wait.until(EC.element_to_be_clickable(
                (By.NAME, "btnK")))
            box.send_keys(query)
            try:
                button.click()
            except ElementNotVisibleException:
                button = self.driver.wait.until(EC.visibility_of_element_located(
                    (By.NAME, "btnG")))
                time.sleep(2)
                button.click()
        except TimeoutException:
            print("Box or Button not found in google.com")

    @classmethod    def tearDownClass(cls):
        cls.driver.close()

2. When you are using windows machine then you need to provide Firefox path
or else its give error



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
class Web_broser:
    def __init__(self):

        firefoxPath="C:\Program Files\Mozilla Firefox\/firefox.exe"

        self.binary = FirefoxBinary(firefoxPath)

        self.driver = webdriver.Firefox(firefox_binary=self.binary)
        # Firefox        

        self.driver = webdriver.Firefox()

    def get_web_driver(self):
        return self.driver

    def web_close(self):
        return self.driver.quit()

class TestCalss:
    def test_website(self):
        try:
            test_web=Web_broser()
            web=test_web.get_web_driver()
            #import pdb
            #pdb.set_trace()
            web.get('https://www.aweber.com/order.htm')
            monthly_radio =web.find_element_by_css_selector('#term_548')

            if monthly_radio.is_selected():
                assert 1            

            else:
                assert 0        

            finally:
                test_web.web_close()


self.driver.get('https://www.aweber.com')
order_tab = self.driver.find_element_by_css_selector('#ordertab>a')
order_tab.click()