Pytest: test fails while getting URL

Dear Geeks
I am new to pytest. I am following a youtube channel. I am writing the same code.
learning to test login functionality of an ecommerce application
i have written testcase file. please have a look at it. while running the testcase i am getting error in the line self.driver.get(self.baseURL)
error messaeg
test_login.py:10 (Test_001_Login.test_homePageTitle)
self = <testCases.test_login.Test_001_Login object at 0x000001CC152D38E0>

def test_homePageTitle(self):
    self.driver=webdriver.Chrome()
self.driver.get(self.baseURL)

it is blocking to move further learning as it is the first test
The original code ::
import pytest
from selenium import webdriver
from pageObjects.LoginPage import LoginPage

class Test_001_Login:
baseURL = “Your store. Login
username="admin@yourstore.com"
password=“admin”

def test_homePageTitle(self):
    self.driver=webdriver.Chrome()
    self.driver.get(self.baseURL)
    actual_title=self.driver.title
    if actual_title=="Your store. Login":
        assert True
        self.driver.close()
    else:
        assert False
        self.driver.close()

def test_Login(self):
    self.driver=webdriver.Chrome()
    self.driver.get(self.baseURL)
    self.lp=LoginPage(self.driver)
    self.lp.setUserName(self.username)
    self.lp.setPassword(self.password)
    self.lp.clickLogin()
    actual_title=self.driver.title
    if actual_title == "Dashboard / nopCommerce administration":
        assert True
        self.driver.close()
    else:
        assert False
        self.driver.close()

Thank you

2 Likes

Corresponding tweet for this thread:

Share link for this tweet.

2 Likes

The code you shared, are they part if a class? What does that class inherit? Usually with cases like this with pytest one should create a fixture which souls contain web driver specific functionalities.

2 Likes

Ah i see you have some code that escaped the code block lol. I will try this code in a few minutes once i am in front of a laptop.

2 Likes

One thing though, can you ensure that you have chrome driver installed and it’s on your path? Although in that case, you would be getting more error instead of just the error you mentioned. Something like: ... selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. ... somewhere along the line.

2 Likes

Okay so I tried running your code and the first test passes, however, I cannot tell if LoginPage had a problem which caused it to tail as that code isn’t given here :slight_smile: also the error message you shared seem to be blaming test_homePageTitle) which isn’t the case as it passes fine in my case. Here’s the code I tried (I omitted the other test as I don’t have the module).

from selenium import webdriver


class Test_001_Login:
    baseURL = "https" + "://admin-demo.nopcommerce.com/login?ReturnUrl=%2Fadmin%2F"
    password = "admin"

    def test_homePageTitle(self):
        self.driver = webdriver.Chrome()
        self.driver.get(self.baseURL)
        actual_title = self.driver.title
        if actual_title == "Your store. Login":
            assert True
            self.driver.close()
        else:
            assert False
            self.driver.close() # This code will be unreachable

A few things here though (Not connected to your error though). You should close the driver right after you captured the title. And then instead of separating assert True and assert False, you can add assertion for the conditional. Something like the following:

from selenium import webdriver


class Test001Login:
    baseURL = "{}//admin-demo.nopcommerce.com/login?ReturnUrl=%2Fadmin%2F".format("https:")
    password = "admin"

    def test_home_page_title(self):
        self.driver = webdriver.Chrome()
        self.driver.get(self.baseURL)

        actual_title = self.driver.title
        self.driver.close()

        assert actual_title == "Your store. Login"

Also, try to see if this thing passes after removal of the second test case. Then we could explore and see if the error lied in LoginPage module.

2 Likes

Also, if you are interested to learn PyTest, and have any question, you can ask here or DM me. It’d be good if you share the video you’re learning from, I’d be interested to see what drove them to flaunt that code in a tutorial :slight_smile:

2 Likes

Thank you so much mafinar for your great help. I will try your suggestions and let you know. actually i am getting error in both the cases. The error page is too long, if you are interested i will also send that too
this is the tutorial i follow Part 1: Selenium with Python | Hybrid Framework Design from scratch | PyTest, POM & HTML Reports - YouTube

2 Likes

still didnt work

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot determine loading status
E from unknown error: missing or invalid ‘entry.level’
E (Session info: chrome=99.0.4844.82)
E (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.19043 x86_64)

1 Like

i tried with google.com to capture the title and i did the assertion. it worked

1 Like

Dear Mafinar
I hardcoded the chrome path and now the test passed. I know it is not a good practice.
I have added the chromedriver path to system and also environment variable but still it is not recognise it.
please let me know how to resolve it

“’%s’ executable needs to be in PATH. %s” % (
os.path.basename(self.path), self.start_error_message)
E selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see ChromeDriver - WebDriver for Chrome


\nopcommercr\venv\lib\site-packages\selenium\webdriver\common\service.py:81: WebDriverException

Thank you for your support

1 Like

Mafinar
I ran the test in terminal it works fine without manually entering the URL path. when i run from pycharm it throws the error that i mentioned earlier

Thank you again
I have one more question
can you please explain me the syntax pytest -v -s filepath

2 Likes

Hello, im glad you made some progress. I know how frustrating it is when you meet a wall when you are just starting to learn.

I wonder why adding to system file is not working for you, it was for me. Unfortunately I do not use Windows for development purpose so cannot readily answer to it.

You can use a site like dpaste or pastebin to share large snippets or stacktraces and paste it here.

As for your question regarding the switches: -v is verbose, as in, you will get more detailed treatment of the your test run if you have this enabled. -s would turn capture mode on. As an example, if you have logs or prints in your test, and run the terst without -s you will not see the logs or prints, but if you have -s (or --capture=yes I guess) then you shall see those on screen.

2 Likes

You can test if it actually is in the PATH, if you open a cmd and type in chromedriver and hit Enter. If Starting ChromeDriver 2.15.322448 is appearing, the PATH is set appropriately and there is something else going wrong. Then try the following:

Download ChromeDriver

Then you have multiple options:

  • Add it to your system path
  • Put it in the same directory as your python script
  • Specify the location directly via executable_path

driver = webdriver.Chrome(executable_path='C:/path/to/chromedriver.exe')

Before you add the chromedriver to your path, make sure it’s the same version as your browser.

If not, you will need to match versions: either update/downgrade you chrome, and upgrade/downgrade your webdriver.