I'm creating a sample test using Selenium and the python bindings and running it with nose. I know I'm doing something wrong because the test opens two browsers (when setup is run, a Firefox window opens up and closes immediately, and then when the test runs driver.get, another window opens). I have the following project:
/test_project
/config
config.ini
/pages
__init__.py
test_page.py
/test_scripts
script.py
__init__.py
base.py
config_parser.py
config.ini:
[Selenium]
browser: firefox
base_url: http://www.google.com/
chromedriver_path:
base.py
from selenium import webdriver
from config_parser import Config
class TestCase(object):
def setup(self):
self.config = Config()
if self.config.read_config('Selenium', 'browser').lower() == 'firefox':
self.driver = webdriver.Firefox()
elif self.config.read_config('Selenium', 'browser').lower() == 'chrome':
self.driver = webdriver.Chrome(self.config.read_config('Selenium', 'chromedriver_path'))
def teardown(self):
self.driver.quit()
test_page.py
from config_parser import Config
class TestPage(object):
def __init__(self, driver):
self.driver = driver
self.config = Config()
def open(self):
self.driver.get(self.config.read_config('Selenium', 'base_url'))
import time
time.sleep(3)
script.py
from pages import test
from base import TestCase
class RandomTest(TestCase):
def test_foo(self):
x = test.TestPage(self.driver)
x.open()
assert 1 == 1
Can someone help me understand why two browser windows are opening and what I can do to correct this issue?
Thank you in advance.
This is because your base TestCase class is being recognized by nose test runner as a test too.
Mark it with #nottest decorator:
from selenium import webdriver
from config_parser import Config
from nose.tools import nottest
#nottest
class TestCase(object):
...
Related
this is the confest.py file present in the test cases file
import pytest
from selenium import webdriver
#pytest.fixture()
def setup():
driver = webdriver.Chrome()
return driver
this is login file present in the page package
import pytest
from selenium import webdriver
class Loginchatbot():
username_xpath = '//input[#id="username"]'
password_xpath = '//input[#id="password"]'
login_xpath = '//span[contains(text(),"Log in")]'
def __init__(self,driver):
self.driver = driver
def set_username(self,username):
#launching the browser
#login in the application
self.wait.driver.find_element_by_xpath(self.username_xpath).clear()
self.wait.driver.find_element_by_xpath(self.username_xpath).click().send_keys(username)
def set_password(self, password):
# launching the browser
# login in the application
self.wait.driver.find_element_by_xpath(self.password_xpath).clear()
self.wait.driver.find_element_by_xpath(self.password_xpath).click().send_keys(password)
def clicklogin(self):
# launching the browser
# login in the application
self.wait.driver.find_element_by_xpath(self.login_xpath).click()
this is the test_login file present in the test cases folder
import pytest
from selenium import webdriver
from p username = "shubhamgupta191190#gmail.com"ages.login import Loginchatbot
class test001_login:
baseurl = "https://teacher-learning-test.ef.com/"
password = "freeBugs!"
def test_login1(self,setup):
self.driver = setup
self.driver.get(self.baseurl)
self.lp = Loginchatbot(self.driver)
self.lp.set_username(self.username)
self.lp.set_password(self.password)
self.lp.clicklogin()
I am trying to design automation framework and I am new to selenium and I am using python with selenium and pytest and while executing the above script written in test_login and function name as test_login1 getting the error message as
platform win32 -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0
rootdir: C:\\Users\\GREEN EARTH\\PycharmProjects\\Chatbot
collected 0 items
================================================================ no tests ran in 0.01s \
Option 1. Change class name
Your test class is called test001_login.
pytest by default allows class names that start with Test.
Consider renaming your test class to Test001Login.
Source
Option 2. Change pytest naming convention
If you want to stick with test001_login class name and change the standard naming convention then you need to define it in your pytest.ini file like this:
[pytest]
python_classes = test
Source
I am trying to run Selenium test in Python on Linux Ubuntu environment.
Geckodriver is located in my project root folder.
I run the file named siteTest.py from PyCharm command line:
python3 siteTest.py
However, I do not see any output from Selenium.
The test worked before I divided it into setUp, test and tearDown and added self as a parameter.
Any suggestions what I am doing wrong?
Thanks in advance.
import os
import unittest
from selenium import webdriver
class siteTest:
def setUp(self):
ROOT_DIR = os.path.abspath(os.curdir)
self.driver = webdriver.Firefox(executable_path=ROOT_DIR + '/geckodriver')
def test(self):
driver = self.driver
driver.get('https://google.com/')
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Your program was near perfect. You just need to annotate the siteTest class as unittest.TestCase. So effectively, you need to rewrite the line:
class siteTest:
as:
class siteTest(unittest.TestCase):
You probably need to annotate your set up and tear down methods.
#classmethod
def setUp(self)
.
.
#classmethod
def tearDown(self)
.
.
Here, I have annotated as class method so it will run only once for the class.
I'm new with Pytest-bdd. I'm trying to run a BDD test but I find always the same problem with the fixture. This is my code.I've tried without the #pytest.fixture and with only the step of #given gherkin step I get same result. fixture 'self'not found
import os
from appium import webdriver
from time import sleep
import pytest
from pytest_bdd import scenario, given, when, then, parsers
#pytest.fixture(autouse=True, scope='module')
def Setup (self):
"Setup for the test"
desired_caps = {}
self.driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
def Teardown():
self.driver.quit()
#scenario('features.feature','Prueba1')
#given('step 1')
def test_single_player_mode(self):
element1=self.driver.find_element_by_id("com.nestle.bagzielicious.beta:id/on_button_vegan")
element1.click()
sleep(1)
Usually those methods are all inside a class and then self refers to the class.
For example:
from unittest import TestCase
class DriverTestCase(TestCase):
Give the following code a try using Pytest:
import os
from appium import webdriver
from time import sleep
import pytest
from pytest_bdd import scenario, given, when, then, parsers
#pytest.fixture(autouse=True, scope='module')
def driver():
"Setup for the test"
desired_caps = {}
driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desired_caps)
yield driver
driver.quit()
#scenario('features.feature','Prueba1')
#given('step 1')
def test_single_player_mode(driver):
element1=driver.find_element_by_id("com.nestle.bagzielicious.beta:id/on_button_vegan")
element1.click()
sleep(1)
Is there any proper way to using getting url in cmd as argument along testcases.py file?
I am running below commmand in cmd to run test cases of python file:
testcases.py "any url"
testcases.py have coding:
class JSAlertCheck(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome("E:\chromedriver.exe")
self.url = sys.argv[1]
def test_Case1(self):
driver = self.driver
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main(sys.argv[1])
As per the discussion Python unittest passing arguments the Python Pundits seems to convey that:
Unit tests should be stand-alone which will have no dependencies outside of their setUp() and tearDown() methods. This is to make sure that each tests has minimal side-effects and reactions to the other test. Passing in a parameter defeats this property of unittest and thus makes them sort of invalid. Using a Test Configuration would have been the easiest way and more appropiate as a unittest should never rely on foreign data to perform the test.
If you still want to do so, here is one of the working solution:
Code Block:
from selenium import webdriver
import unittest
import sys
class MyTest(unittest.TestCase):
URL = "foo"
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver = self.driver
driver.get(self.URL)
def test_Case1(self):
driver = self.driver
print(driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
if len(sys.argv) > 1:
MyTest.URL = sys.argv.pop()
unittest.main()
CLI Command:
python unittest_cmdline_urlASarguments.py http://www.python.org
Output:
C:\Users\AtechM_03\LearnAutmation\PythonProject\readthedocs>python unittest_cmdline_urlASarguments.py http://www.python.org
[4448:5632:0606/205445.017:ERROR:install_util.cc(589)] Unable to create registry key HKLM\SOFTWARE\Policies\Google\Chrome for reading result=2
DevTools listening on ws://127.0.0.1:5634/devtools/browser/40cc6c16-1e52-4f49-a54f-08fac3ff7abc
Welcome to Python.org
.
----------------------------------------------------------------------
Ran 1 test in 9.534s
OK
C:\Users\AtechM_03\LearnAutmation\PythonProject\readthedocs>
Commandline Snapshot:
I am creating an automation framework from scratch using selenium with python and would really like some input and advice on the best way to do so.
So far I have the following but my test script will not run. Please help!
script.py
from selenium import webdriver
class WebDriver(object):
def __init__(self, driver=None):
"""
__init__ setup webdriver test script class
"""
self.driver = driver
def setup(self):
self.driver = webdriver.Chrome()
def teardown(self):
self.driver.quit()
test.py
import script
class Test(script.WebDriver):
def search(self):
self.driver.get("www.google.com")
self.driver.find_element_by_id("lst-ib").clear()
I'm assuming you are trying to run this with python unittest. If so, your class should inherit from unittest.TestCase in order to mark that it contains test cases:
class WebDriver(unittest.TestCase)
...
class Test(script.WebDriver)
And second missing piece is "boiler plate code to run the test suite" (see explanation here) in test.py:
if __name__ == "__main__":
test.main()