Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
2.7k views
by The go-to Tester (218 points)
retagged by
I have written code to assert title of a website. I have written test using Selenium Webdriver in Python language. My code is-:

import unittest
from selenium import webdriver

class TestTwo(unittest.TestCase):

    def setUp(self):
        chromedriver = "C:\Selenium driver\chromedriver"
        #os.environ["webdriver.chrome.driver"] = chromedriver
        self.driver = webdriver.Chrome(chromedriver)

    def test_url(self):
        self.driver.get("http://www.seleniumhq.org")
        assert "Selenium" in driver.title

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

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

I am getting the following error

ERROR: test_url (__main__.TestTwo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\Python Workspace\exampletest.py", line 14, in test_url
    assert "Selenium" in driver.title
NameError: global name 'driver' is not defined

Please help

1 Answer

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

You will have to declare and define driver = self.driver before you work with it. 

Eg.

def test_url(self):

        driver = self.driver
        self.driver.get("http://www.seleniumhq.org") 
        assert "Selenium" in driver.title

Or simply you can get title from self.driver

Eg.

def test_url(self):
        self.driver.get("http://www.seleniumhq.org") 
        assert "Selenium" in self.driver.title
 

Hope that helps!


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...