This question already has answers here:
What is unittest in selenium Python?
(2 answers)
Closed 2 years ago.
This test case checks the error that can occur during login functionality
from selenium import webdriver
import unittest
class LoginCheck(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def login_error_check(self):
browser = self.driver
browser.get('https://www.saucedemo.com/')
browser.maximize_window()
usr = browser.find_element_by_id('user-name')
password = browser.find_element_by_id('password')
button = browser.find_element_by_xpath('//input[#value="LOGIN"]')
print('Hello')
usr.send_keys('standard_user')
password.send_keys('password')
button.click()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
I am getting below message
Ran 0 tests in 0.000s
OK
repl process died unexpectedly
was expecting that it will be success but couldn't see anything. I'm learning selenium.
test ran by prefixing the 'test' to the method
from selenium import webdriver
import unittest
class LoginCheck(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_login_error_check(self):
browser = self.driver
browser.get('https://www.saucedemo.com/')
browser.maximize_window()
usr = browser.find_element_by_id('user-name')
password = browser.find_element_by_id('password')
button = browser.find_element_by_xpath('//input[#value="LOGIN"]')
print('Hello')
usr.send_keys('standard_user')
password.send_keys('password')
button.click()
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
This will work.
Related
I need help with my pytest/selenium code.
I have the following code in my conftest.py file.
import pytest
from base.webdriverfactory import WebDriverFactory
from pages.login_page import LoginPage
#pytest.fixture(scope="class")
def oneTimesetUp(request, browser):
print("Running one time setUp")
wdf = WebDriverFactory(browser)
driver = wdf.getWebDriverInstance()
lp = LoginPage(driver)
lp.login("haykpo", "Aaaa4321")
if request.cls is not None:
request.cls.driver = driver
yield driver
lp.log_out_from_wp_page()
driver.quit()
print("Running one time tearDown")
And here is my test file where I use this fixture
#pytest.mark.usefixtures("oneTimesetUp", "tearDown")
class TestPublishWithOfflinewAsset:
#pytest.fixture(autouse=True)
def objectSetup(self, oneTimesetUp):
self.pub = TestPublishofflineWOMedia(self.driver)
def test_publish_with_off_w_media(self):
self.pub.select_project('Test cases - Avallain')
self.pub.check_lo_plays_normally(15292)
I know that whatever comes after yield is meant to be code as tearDown. So I have written these 2 lines in order to log out from the app and quit the browser at the end of the test execution.
lp.log_out_from_wp_page()
driver.quit()
However, the problem is that the log out part (not the quit one interestingly) is always being executed after few steps when my test starts and when browser focus switch to iframe during test execution (or may be this is just a coincidence am not sure if iframe is guilty here). How can I compose my code to run my teardown log out or whatever I want after yield as intended but not during test execution?
For more details here is the detailed files with code.
The below one is my Login page
from base.basepage import BasePage
class LoginPage(BasePage):
def __init__(self, driver):
super().__init__(driver)
self.driver = driver
### Locators ###
_name_field = "form1:Name"
_password_field = "form1:Password"
_login_button = "form1:loginButton"
_work_packages = "wpButton"
_user_logout_dropdown_button = 'rolebutton_button'
_logout_button = 'blogout'
def log_out_from_wp_page(self):
self.elementClick(locator=self._user_logout_dropdown_button)
self.driver.implicitly_wait(5)
self.elementClick(locator=self._logout_button)
And below one is webdriver factory
class WebDriverFactory:
def __init__(self, browser):
self.browser = browser
def getWebDriverInstance(self):
"""
Get WebDriver Instance based on the browser configuration
Returns:
'WebDriver Instance'
"""
global driver
baseURL = "https://example.com"
if self.browser == "iexplorer":
# Set ie driver
driver = webdriver.Ie()
elif self.browser == "firefox":
driver = webdriver.Firefox()
elif self.browser == "chrome":
# Set chrome driver
driver = webdriver.Chrome()
#driver.set_window_size(1440, 900)
else:
driver = webdriver.Chrome()
# Setting Driver Implicit Time out for An Element
driver.implicitly_wait(10)
# Maximize the windows
driver.maximize_window()
# Loading browser with App URL
driver.get(baseURL)
return driver
The below one is BasePsge
class BasePage(SeleniumDriver):
def __init__(self, driver):
super(BasePage, self).__init__(driver)
self.driver = driver
I am using unittest example in selenium python
tried google did not get correct solution
from selenium import webdriver
import unittest
#import HtmlTestRunner
class googlesearch(unittest.TestCase):
driver = 'driver'
#classmethod
def setupClass(self):
self.driver = webdriver.Chrome(chrome_options=options)
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def test_search_automationstepbystep(self):
self.driver.get("https://google.com")
self.driver.find_element_by_name("q").send_keys("Automation Step By step")
self.driver.find_element_by_name("btnk").click()
def test_search_naresh(self):
self.driver.get("https://google.com")
self.driver.find_element_by_name("q").send_keys("Naresh")
self.driver.find_element_by_name("btnk").click()
#classmethod
def teardownClass(self):
self.driver.close()
self.driver.quit()
print("Test completed")
if __name__== "__main__":
unittest.main()
As mentioned #Error - Syntactical Remorse , driver is a string due to your first line of code in your class.
If you are planning to access the driver globally make sure to declare the driver as global.
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import HtmlTestRunner
class Environment(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="F:\\automation\\chromedriver.exe")
# login
def test_login(self):
driver = self.driver
driver.maximize_window()
driver.get("http://localhost/dashboatd")
self.driver.find_element_by_id('uemail').send_keys('xyz#gmail.com')
self.driver.find_element_by_id('upwd').send_keys('1234567890')
self.driver.find_element_by_id('upwd').send_keys(Keys.RETURN)
# login page
def going_first_page(self):
going_first_page = self.find_element_by_class_name('color7')
self.execute_script("arguments[0].click();", going_first_page)
new_notification = self.driver.find_element_by_class_name('fa-paper-plane')
self.driver.execute_script("arguments[0].click();", new_notification)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='F:\\automation\\reports'))
As per python guidelines-
"A testcase is created by subclassing unittest.TestCase. The three individual tests are defined with methods whose names start with the letters test. This naming convention informs the test runner about which methods represent tests."
So, you should rename it to test_going_first_page(self) from going_first_page(self)
from selenium import webdriver
import unittest
import HtmlTestRunner
from selenium.webdriver.common.keys import Keys
class Environment(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="D:\\auto\chromedriver.exe")
def test_login(self):
driver = self.driver
driver.get("htt://localhost/dashboard/user/login")
username = driver.find_element_by_id("uemail")
username.send_keys("xyz#abc.com")
password = driver.find_element_by_id("upwd")
password.send_keys("1234567890")
self.driver.find_element_by_id('upwd').send_keys(Keys.RETURN)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
Try below code:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class PythonOrgSearch(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path="C:\\Users\\dipak.bachhav\\Downloads\\New folder (2)\\chromedriver_win32\\chromedriver.exe")
def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
I'm VERY new to automation, fair warning.
I have an automation script to verify that the canonical tag of a page is present. I also need to assert that it's all lower case. Would I just create an assert after "driver.find_element..." to assert islower() ?
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class homepage_canonical(unittest.TestCase):
def setUp(self):
global driver
driver = webdriver.Firefox()
driver.get("websiteurlhere")
def test_hpcanonical(self):
WebDriverWait(driver, 10)
driver.find_element_by_css_selector("link[href='canonicalurlhere'][rel='canonical']")
def tearDown(self):
driver.quit()
if __name__ == "__main__":
unittest.main()
I would use assertTrue :
def test_hpcanonical(self):
wait = WebDriverWait(driver, 10)
element = driver.find_element_by_css_selector("link[href='canonicalurlhere'][rel='canonical']")
self.assertTrue(element.text.islower())