Function testing with playwright and python - python

I'm kinda new in coding and was asked to do a test for the company web login, they want me to implement the module unitestt and playwright test generator tool. This is what I have so far. I had to separate run from test_1 since unittest failed while reading the chromium line but now it only opens the browser so what can I do in order for it to run the whole test?
from playwright.sync_api import Playwright, sync_playwright
from locators import Locators_evou
import unittest
class Test(unittest.TestCase):
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(channel ="chrome", headless=False,slow_mo=500)
context = browser.new_context()
page = context.new_page()
page.goto("http://localhost:3000/")
def test_1(page):
page.click(Locators_evou.user_Log)
page.fill(Locators_evou.user_Log, "Liliana")
page.click(Locators_evou.password_log)
page.fill(Locators_evou.password_log, "1234")
page.check(Locators_evou.session_Log)
page.click(Locators_evou.login_log)
assert page.is_visible("¡Bienvenido!")
with sync_playwright() as playwright:
run(playwright)
if __name__=="__main__":
unittest.main()

Related

Trouble in Accepting the Dialog Box using Playwright Python

Hoe can I accept the dialog using python playwright. For your kind information I have already tried this code but it doesn't seems to work for me. Any other solution other than that will be appreciable. Thanks
from playwright.sync_api import sync_playwright
def handle_dialog(dialog):
print(dialog.message)
dialog.dismiss()
def run(playwright):
chromium = playwright.chromium
browser = chromium.launch()
page = browser.new_page()
page.on("dialog", handle_dialog)
page.evaluate("alert('1')")
browser.close()
with sync_playwright() as playwright:
run(playwright)

Html report is not generating for selenium Test case not even blank report

I tried a simple selenium program to generate the HTML report. The test case run perfectly but the HTML report is not generating.
from selenium import webdriver
import unittest
import HtmlTestRunner
# Keyword
url = 'https://amazon.in'
path = 'C:/bin/chromedriver.exe'
class amazonTest(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome(executable_path=path)
cls.driver.maximize_window()
def test_openWebsite(self):
self.driver.get(url)
print(self.driver.title)
#classmethod
def tearDownClass(cls):
cls.driver.close()
cls.driver.quit()
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output="C:/Users/tester/Desktop/PYTHON/sampleReport"))

Automate the send email test report of test cases using selenium and python

How do i automate send email test report to recipient from selenium webdriver using python. I'm currently using pycharm IDE to automate test cases.
I want send email to someone at last when this test case ran by me. Mail should have success and failed count. I tried to find the solution but failed.This is my test case. Please give me the solution.
from selenium import webdriver
from generic_functions.FindElement import HandyWrappers
from generic_functions.takescreenshots import Screenshot
from generic_functions.error_handle import CatchExceptions
import os
import time
import unittest
class TestHome(unittest.TestCase):
driverLocation = "C:\\Users\\Sales\\Desktop\\Automation\\ABCD\\libs\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = driverLocation
driver = webdriver.Chrome(driverLocation)
driver.maximize_window()
driver.implicitly_wait(10)
def test_home(self):
try:
baseURL = "https://portal.abcd.com"
driver = self.driver
driver.get(baseURL)
hw = HandyWrappers(driver)
# regionLogin
username = hw.getElement(".//form[#id='login-form']/fieldset/section[1]/label[2]/input[#name='email']",
locatorType="xpath")
username.send_keys("abcd#live.com")
time.sleep(2)
password = hw.getElement(".//form[#id='login-form']/fieldset/section[2]/label[2]/input[#name='password']",
locatorType="xpath")
password.send_keys("abcd")
signIn = hw.getElement(".//form[#id='login-form']/footer/button[contains(text(),'Sign in')]",
locatorType="xpath")
signIn.click()
Screenshot.takeScreenshot(driver)
# endregion
time.sleep(2)
driver.execute_script("window.scrollBy(0,300);")
time.sleep(1)
# Switch to Iframe from main page
driver.switch_to.frame("ifrmClaimSummary")
# regionScrolling Right and left
driver.execute_script("window.scrollBy(100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(100,0);")
time.sleep(1)
# Scrolling Left
driver.execute_script("window.scrollBy(-100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(-100,0);")
time.sleep(1)
driver.execute_script("window.scrollBy(-100,0);")
time.sleep(1)
# endregion
driver.switch_to.default_content()
driver.execute_script("window.scrollBy(0,-300);")
time.sleep(2)
driver.quit()
except Exception:
CatchExceptions.PrintException()
Screenshot.takeScreenshot(driver)
if __name__ == '__main__':
unittest.main()
Based on your comments, here's an approach you can follow to accomplish test reporting and email results:
Consider leveraging unittest for test reporting - Selenium has an example of this in their docs.
Then, you can use smtplib to send emails. There are a myriad of resources for this; here's one tutorial on smtplib with Gmail.
In a nutshell:
Use unittest to set up a test suite
Access the test results via unittest's API to do so
Build whatever string you want to send, and send it off with smtplib

How to merge existing python selenium code to RIDE framework

I have already written my test cases in python selenium. Sample given below
import unittest
from selenium import webdriver
class SprintTests(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://samplesite.com")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_sample_test_1(self):
a = self.driver.find_element_by_id('orderId')
order_no=a.get_attribute('value')
print order_no
self.assert(some_condition)
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main(verbosity=2)
Now i want to use Robot Framework RIDE for all these cases, but i dont want to write them again. Is there any way to use already written python code to work in RIDE.

Mocking Selenium responses in order to test UI controller

I'm using the Selenium interface in order to drive a UI, so as to have a programmatic API for something that's currently only a web UI. I'd like to write unit tests for this selenium-based application. How do I mock out calls to the original Selenium API in order to verify that the app is correct?
Here's a minimum example of the Selenium app:
class MyClass(object):
def run_app(self):
self.driver = webdriver.Firefox()
self.driver.get("http://www.google.com")
assert "Google" in driver.title
self.driver.close()
return driver.title
def main():
test_class = MyClass()
results = test_class.run_app()
if __name__ == '__main__':
main()

Categories

Resources