I'm having an issue with running python selenium for the first time :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class segfam(unittest.TestCase):
def setUp(self):
self.driver=webdriver.chrome("/Users/tomersegal/Downloads/chromedriver")
def test_blabla(self):
driver=self.driver
driver.get("https://www.google.co.il/")
assert "Google" in driver.title
This is my error :
Ran 0 tests in 0.000s
OK
Launching unittests with arguments python -m unittest discover -s /Users/tomersegal/PycharmProjects/pythonProject1 -t /Users/tomersegal/PycharmProjects/pythonProject1 in /Users/tomersegal/PycharmProjects/pythonProject1
Process finished with exit code 0
Empty suite
As you are using unittest framework you have to call it from the __main__ function as:
if __name__ == "__main__":
unittest.main()
So your effective code block will be:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class segfam(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome("/Users/tomersegal/Downloads/chromedriver")
def test_blabla(self):
driver=self.driver
driver.get("https://www.google.co.il/")
assert "Google" in driver.title
if __name__ == "__main__":
unittest.main()
PS: Note the change of chrome to Chrome
References
You can find a couple of relevant detailed discussions in:
Python + WebDriver -- No browser launched while using unittest module
What is unittest in selenium Python?
Related
I am trying run my tests in Selenium Grid 4.2.1. I created hub and node with Google Chrome. My setup method is in conftest file in 'tests' folder. I used fixture in setup method and when I run my tests, the output is:
test setup failed
request = SubRequest 'setup' for Function test_properly_loggining_url
WebDriverException: Message: Unable to find handler for (POST) /session.
import pytest as pytest
import webdriver_manager.chrome
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
conftest.py
#pytest.fixture()
def setup(request):
grid_url = "http://192.168.88.189:5555"
driver = webdriver.Remote(grid_url, DesiredCapabilities.CHROME)
driver.maximize_window()
yield
driver.stop_client()
driver.close()
driver.quit()
test_file.py
#pytest.mark.usefixtures("setup")
class TestLogin:
def test_properly_loggining_url(self):
Login = HomePage(self.driver)
Login.properly_loggining()
assert Login.get_current_url() == Login.home_page
Do you have any idea how can I solve this problem?
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class loginAvaliador(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver')
def login_avaliador(self):
driver = self.driver
driver.get("http://d3dyod5mwyu6xk.cloudfront.net/")
assert "FGV" in driver.title
cpf = driver.find_element_by_xpath('//input[#placeholder="CPF"]')
cpf.send_keys("27922797885")
password = driver.find_element_by_xpath('//input[#placeholder="SENHA"]')
password.send_keys("enccejaregular")
login = driver.find_element_by_tag_name('button')
login.click()
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
when i try to run this test script it returns "ran 0 tests in 0.000s" why this is happening? i'm new to python and writting this test scripts so i can't find the error
With the unittest module, you need to use their built in assertion methods. Instead of just writing
assert a not in b
you write
self.assertNotIn("No results found.",driver.page_source)
for example. For a list of all the assert methods look here:
https://docs.python.org/2/library/unittest.html#unittest.TestCase
While working through Python's unittest module with Selenium you have to consider a few facts as follows :
You need to take care of the indents. Indentation for class and test_method are different.
While you define the #Tests name the tests starting with test e.g.
def test_login_avaliador(self):
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Here is your own code with the required minor modifications :
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class loginAvaliador(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')
def test_login_avaliador(self):
driver = self.driver
driver.get("http://d3dyod5mwyu6xk.cloudfront.net/")
assert "FGV" in driver.title
cpf = driver.find_element_by_xpath('//input[#placeholder="CPF"]')
cpf.send_keys("27922797885")
password = driver.find_element_by_xpath('//input[#placeholder="SENHA"]')
password.send_keys("enccejaregular")
login = driver.find_element_by_tag_name('button')
login.click()
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Execution Result:
C:\path\to\PyPrograms>python -m unittest 52560471_unittest.py
DevTools listening on ws://127.0.0.1:12022/devtools/browser/078fc4e9-3ca6-4bbb-b318-0b8f04318d32
.
----------------------------------------------------------------------
Ran 1 test in 40.796s
OK
Going through a tutorial at Obey The Testing Goat and I'm getting errors using environmental variables. Using Django 1.11, Selenium 3, and Python 3.6.
(py36) C:\testgoat\superlists>STAGING_SERVER=superlists-staging.ottg.eu python manage.py test functional_tests
'STAGING_SERVER' is not recognized as an internal or external command,
operable program or batch file.
If I understand the tutorial correctly, an environmental variable, STAGING_SERVER, is used run tests using a real server instead of Django's test server.
django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import time
import unittest
import sys
import os
MAX_WAIT = 10
class NewVisitorTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Firefox()
staging_server = os.environ.get('STAGING_SERVER')
if staging_server:
setattr(self, 'live_server_url', 'http://' + staging_server)
[...]
Forgive me if this is a stupid question (new to coding) but I haven't been able to find documentation that explains how to do this in python. All I want is to be able to execute x number of concurrent tests for either the same browser or across different browsers.
I've setup my hub and node with maxInstance and maxSession = 5 so I was expecting that 5 browser windows would show up when I execute the test but only 1 opens and executes. Am I missing something?
I've seen examples in java where ppl set the DesiredCapabilites as a variable but I get errors when I try this - I can't figure out how to specify more than a single browser to execute my test.
How would I set something like this up to open and run on 2 chrome instances and 3 internet explorer instances?
import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
class GridTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',desired_capabilities=DesiredCapabilities.CHROME)
def test01_GridTest(self):
driver = self.driver
driver.get("http://www.google.com")
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
I have a functional test 'y1.py' which I have exported from the selenium IDE. It looks like:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class Y1(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://www.yahoo.com/"
self.verificationErrors = []
self.accept_next_alert = True
def test_y1(self):
driver = self.driver
driver.get(self.base_url)
driver.find_element_by_link_text("Weather").click()
driver.save_screenshot('out11.png')
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
I am trying to call this directly from within a python/django function. while investigating this I came across: AttributeError 'module' object has no attribute 'runserver' in django, where Udi states:
Are you trying to run unitest and selenium from a view? You should
consider launching a second process for that.
How can I do this?
You can start django server as new process with subprocess module.