FlutterLover
FlutterLover

Reputation: 671

UnboundLocalError: local variable : On Python

I have this code to run test on Jenkins cloud, the run works fine but on my machine a get this error : UnboundLocalError: local variable My code :

# -*- coding: utf-8 -*-
import time
import unittest
import os
from selenium import webdriver
from com.POMProject.Pages.homePage import HomePage
from com.POMProject.Pages.loginPage import LoginPage
from com.POMProject.Setup.urls import beta_url
from com.POMProject.Setup.users import *

class LoginTest(unittest.TestCase):

@classmethod
def setUp(cls):
    if os.getenv('CHROMEWEBDRIVER'):
        chromewebdriverbin = os.getenv('CHROMEWEBDRIVER')
    cls.driver = webdriver.Chrome(
        executable_path=chromewebdriverbin)
    cls.driver.implicitly_wait(50)
    cls.driver.delete_all_cookies()
    cls.driver.set_window_size(1224, 800)

def test_login_valid_user_password(self):
    driver = self.driver
    driver.get(beta_url)

    login = LoginPage(driver)
    home = HomePage(driver)

    home.click_user_icon()
    home.click_login_button()
    login.enter_user(user_email)
    login.click_next_button()
    login.enter_password(user_password)
    login.click_submit_button()
    driver.quit()
    print('Test Completed')

The error message:

E       UnboundLocalError: local variable 'chromewebdriverbin' referenced before assignment

Upvotes: 0

Views: 157

Answers (2)

FlutterLover
FlutterLover

Reputation: 671

Fixed by adding chromedriver in local machine :

    def setUp(cls):
    if os.getenv('CHROMEWEBDRIVER'):
        chromewebdriverbin = os.getenv('CHROMEWEBDRIVER')
    else:
        chromewebdriverbin = '/usr/bin/chromedriver'
    cls.driver = webdriver.Chrome(
        executable_path=chromewebdriverbin)

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193188

This error message...

UnboundLocalError: local variable 'chromewebdriverbin' referenced before assignment

...implies that the varible was referenced even before it was assigned a value.


Analysis

The main issue is, the following condition fails:

if os.getenv('CHROMEWEBDRIVER'):

Hence, the following line:

chromewebdriverbin = os.getenv('CHROMEWEBDRIVER')

never gets executed and the variable chromewebdriverbin is never assigned any value.

In this circumstance, when you refer to chromewebdriverbin in the line:

cls.driver = webdriver.Chrome(executable_path=chromewebdriverbin)

The above mentioned error is raised.


Reason

The most probable reason is, the environment variable CHROMEWEBDRIVER isn't set on the failing machine.

Upvotes: 2

Related Questions