0

I am new to pytest-bdd. I have to implement the automation framework from the scratch. SO I have followed some material and implemented with small framework very basic. When I run the test I can see webdriver is not getting started at all and I don't see any error. I am not sure how to fix this issue. Could anyone help me what is the root cause of the issue I need some examples with pytest-bdd page object model implementation?

** My project structure** [![projectstructure][1]][1]

Inside the tests directory I have features and step_def directory is there. Inside the steps_def I have a test_file_name.py and init file is there.

Inside the page objects directory I have basepage and loginpage python file is there

Basepage.py

"""
Base page which has base URL and locators and parent class for other classes

"""
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Base_Page:
    "Base page which has locators and URL"
    def __init__(self,driver):
        self.driver = driver

    def do_click(self,by_locator):
        WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(by_locator)).click()

    def do_send_keys(self,by_locator,text):
        WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(by_locator)).send_keys(text

Loginpage

"""
Login page actions
"""
from page_objects.base_page import Base_Page 
from selenium.webdriver.common.by import By
from config import config

class Login_page(Base_Page):
    "Class to run the login page"
    user_name = (By.XPATH,"//input[@id='username']")
    password = (By.XPATH,"//input[@id='password']")
    check_box = (By.NAME,"remember")
    login_button = (By.XPATH,"//input[@id='login']")
    base_url = config.BASE_URL
  
    
    def set_username(self):
        "Set the username in the username field"
        try:
            username = self.do_send_keys(self.user_name,"1111111")
            self.result_flag = True
        except Exception as e:
            self.result_flag = False
            print(f"Could not set the username. Got an error {e}")
        
        return self.result_flag

Conftest

import pytest
from selenium import webdriver
from pytest_bdd import given

@pytest.fixture(params=['Chrome','Firefox'], scope="class")
def init_driver(request):
    if request.param == 'Chrome':
        web_driver = webdriver.Chrome()
    if request.param == 'Firefox':
        web_driver = webdriver.Firefox()
    request.cls_driver = web_driver
    yield 
    web_driver.close()

@given('Navigate to the URL')
def navigate_url(init_driver):
    "Pass the username"
    init_driver.get("https://www.example.com")

my test file which is inside the step_defs

from pytest_bdd import given, when, then, scenario, parsers
from page_objects.base_page import Base_Page
from page_objects.login_page import Login_page
import pytest



scenario("../features/login.feature",'Login to the app')
def test_pass():
    pass


@when(parsers.cfparse('Enter the username {username:Number}',extra_types=dict(Number=int)))
def get_username(init_driver):
    "Set the phone number as a username"
    log = Login_page(init_driver)
    log.set_username()
    

My feature file ''' Feature: Navigate to the Practo app and Login with credentials

Scenario Outline: Login to the app Given Navigate to the URL When Enter the username '' And ENter the password And De-select the checkbox And Click the login button Then Verify we have logged into the app


When I run pytest test_file_name.py I am not getting any error but it's not starting the webdriver also ,it just gives collected 1 item and it's passed. 


can someone help me how to fix this issue?


  [1]: https://i.stack.imgur.com/BnA6C.png
vaanumalar
  • 57
  • 1
  • 11
  • Hey, I am working with Selenium webdrivers with Python, but using Python behave. I have never encountered an isssue like this. Using Pytest are you able to launch a browser first ? have you passed in the PATH for the webdriver? i dont see that in your code. – PSR Jan 20 '22 at 13:55
  • No I am not able to launch the browser. I am not passing the path for the webdriver. is it necessary to do it? – vaanumalar Jan 21 '22 at 01:12
  • I have included the geckodriver/chrome executable path then also the browser is not getting instantiated. – vaanumalar Jan 21 '22 at 01:44
  • yes, you have to. without access to a webdriver, how do you expect an instance of a webrowser to automatically open?. I shall post an answer on how to get it to open a browser, please go through it and get back to me if any doubts – PSR Jan 21 '22 at 04:20

1 Answers1

0
from selenium import webdriver
driver = webdriver.Firefox(executable_path = "Path to geckodriver")
driver.get("https://google.com")
time.sleep(10)
driver.close()
PSR
  • 249
  • 1
  • 10
  • make sure path to geckodriver is also added as a environment variable – PSR Jan 21 '22 at 04:24
  • I did what you have mentioned but I am facing the same issue – vaanumalar Jan 21 '22 at 06:58
  • did you run the above code in a seperate window ? – PSR Jan 21 '22 at 07:07
  • I did what you have mentioned but I am facing the same issue => what is the issue please post a screenshot – PSR Jan 21 '22 at 07:07
  • When I run the test there is no error and I can see test is passed 100%. But the browser itself is not getting started. I can say that test is showing passed without running. – vaanumalar Jan 21 '22 at 08:33
  • Ok see ... i have sent you some code, create a new tester.py file and paste my code there, run it and attach a screenshot of that in your question. otherwise i dont even know what you are doing. – PSR Jan 21 '22 at 08:37
  • I am not interested in the test code. jsut run the above code i have sent – PSR Jan 21 '22 at 08:39