So my setup for running Safari .py tests is a little different than those for Chrome and FireFox (examples below). That being said, the tearDown for Safari must be different, but I cannot find any examples or figure it out.
Chrome setup example:
class ChromeAllLinks(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
self.base_url = "https://test.foo.com/"
self.verificationErrors = []
self.accept_next_alert = True
Safari setup example:
class SafariAllLinks(unittest.TestCase):
def setUp(self):
import webbrowser
browser = webbrowser.get('safari')
browser.open("https://test.foo.com/")
self.verificationErrors = []
self.accept_next_alert = True
So of course the tearDown for them should be different as well.
Chrome tearDown being used (which works fine of course):
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
And I can show what I am trying for the Safari tearDown, but it isn't working:
def tearDown(self):
browser = webbrowser.quit('safari')
Here is the error being thrown. Says not defined, but it is!
NameError: global name 'webbrowser' is not defined
Related
I am new in automation testing. I am learning automation using selenium with python(page object model). From learning YouTube I see that, log in is done for every test case, which is redundant. I would like to login once and execute for multiple test case. This is a sample code.
tests/test_dashboard.py
class TestDashboard:
def test_dashboard(self, setup):
self.driver = setup
self.driver.get(self.base_url)
self.lp = Login(self.driver)
self.lp.set_email(self.user_email)
self.lp.set_password(self.user_password)
self.lp.sign_in()
def test_dashboard_checking_fire(self, setup):
self.driver = setup
self.driver.get(self.base_url)
# self.driver.maximize_window()
self.lp = Login(self.driver)
self.lp.set_email(self.user_email)
self.lp.set_password(self.user_password)
self.lp.sign_in()
You see, for 2 teset cases i have to execute log in everytime. How I can login once and execute that 2 test cases at once? I am using pytest framework.How to do this with pytest. There is a setup method.
tests/conftest.py
#pytest.fixture()
def setup(browser):
if browser == "chrome":
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
elif browser == "firefox":
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))
else:
driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
return driver
Finally, I solve my Problem. I update my setup function like this:
#pytest.fixture(scope="class", autouse=True)
def setup(request, browser, url):
global driver
# my code
request.cls.driver = driver
yield
driver.close()
I am trying to inherit browser class in test. Can someone please point out what am i doing wrong here. I am new to python
This is my test class where I am trying to inherit browser class
import unittest
from Configurations.Browser import Browser
class GoogleTest(Browser):
def test_homepage(self):
driver = self.driver
self.driver.implicitly_wait(10) self.driver.find_element_by_xpath("/html/body/div/div[4]/form/div[2]/div[1]/div[1]/div/div[2]/input").send_keys("Test")
Browser.py:
import unittest
from selenium import webdriver
class Browser(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executablepath=r"C:\Setups\Selenium\Drivers\geckodriver.exe")
self.driver.implicitly_wait(10)
self.driver.maximize_window()
self.driver.get("https://www.google.com")
def tearDown(self):
if(self.driver != None):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
You have to change the "excutablepath" to "executable_path".(Browser.py)
like this
self.driver = webdriver.Firefox(executable_path=r"C:\Setups\Selenium\Drivers\geckodriver.exe")
I'm studying selenium with python and in a tutorial I found the following code.
from selenium import webdriver
from time import gmtime, strftime
import unittest
#from builtins import classmethod
class RegisterNewUser(unittest.TestCase):
##classmethod
def setUp(self):
self.driver = webdriver.Firefox
self.driver.implicitly_wait(30)
self.driver.maximize_window()
# navigate to the application home page
self.driver.get("http://demo-store.seleniumacademy.com/")
def test_register_new_user(self):
driver = self.driver
pass
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main(verbosity=2)
It throw an error:
File "register_new_user.py", line 10, in setUp
self.driver.implicitly_wait(30)
TypeError: implicitly_wait() missing 1 required positional argument: 'time_to_wait'
I try to add the code commented out (classmethod) but doesn't change anything. Without the test_register_new_user doesn't give error.
I'm using python 3.6.4, selenium 3.141 (and geckodriver 0.23)
Your problem is one line above:
self.driver = webdriver.Firefox
This does not create a browser object. It just sets self.driver to the class webdriver.Firefox, which means that self.driver.implicitly_wait(30) is trying to use implicitly_wait in the static way, ie webdriver.Firefox.implicitly_wait(30), so it is missing the instance, ie webdriver.Firefox.implicitly_wait(an_actual_browser, 30).
You are missing ():
self.driver = webdriver.Firefox() # which will potentially ask for a path to
# firefox/geckodriver if it is not in PATH,
# but that is out of the scope of this question
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
I have a test in Selenium IDE with Flow Control which submits a form and repeats the process with label start until I stop the app.
I have exported my code into python WebDriver.
Unfortunately my test only runs once.
How can I duplicate this process on python.
class Buy(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://test.mydomain.com"
def test_buy(self):
driver = self.driver
driver.get(self.base_url + "/Form")
Select(driver.find_element_by_id("DropDownOne")).select_by_visible_text("BLUE")
Select(driver.find_element_by_id("DropDownTwo")).select_by_visible_text("LIGHT")
driver.find_element_by_id("Quantity").send_keys("10")
driver.find_element_by_id("Button").click()