How to merge existing python selenium code to RIDE framework - python

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.

Related

Assertions in Selenium Python

I am starting automation testing in selenium python .how we assert value java if java is available on the website or not .
I have added a pic of the website.
Website link: https://www.tutorialspoint.com/java/index.htm
class Test(unittest.TestCase):
def testName(self):
s =Service("C:\Program Files (x86)\chromedriver.exe")
driver= webdriver.Chrome(service=s)
driver.get("https://www.tutorialspoint.com/java/index.htm")
actualString = driver.find_element(By.TAG_NAME, "a").text
actualString.assertTrue(actualString.contains("java"));
if __name__ == "__main__":
unittest.main()

Function testing with playwright and 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()

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"))

Selenium Hub, Launches 2 browser only runs test in 1 of them (selenium python)

TL/DR: Right now it launches 2 browsers but only runs the test in 1. What am I missing?
So I'm trying to get selenium hub working on a mac (OS X 10.11.5). I installed with this, then launch hub in a terminal tab with:
selenium-standalone start -- -role hub
Then in another tab of terminal on same machine register a node.
selenium-standalone start -- -role node -hub http://localhost:4444/grid/register -port 5556
It shows up in console with 5 available firefox and chrome browsers.
So here's my code. In a file named globes.py I have this.
class globes:
def __init__(self, number):
self.number = number
base_url = "https://fake-example.com"
desired_cap = []
desired_cap.append ({'browserName':'chrome', 'javascriptEnabled':'true', 'version':'', 'platform':'ANY'})
desired_cap.append ({'browserName':'firefox', 'javascriptEnabled':'true', 'version':'', 'platform':'ANY'})
selenium_server_url = 'http://127.0.0.1:4444/wd/hub'
Right now I'm just trying to run a single test that looks like this.
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from globes import *
class HeroCarousel(unittest.TestCase):
def setUp(self):
for driver_instance in globes.desired_cap:
self.driver = webdriver.Remote(
command_executor=globes.selenium_server_url,
desired_capabilities=driver_instance)
self.verificationErrors = []
def test_hero_carousel(self):
driver = self.driver
driver.get(globes.base_url)
hero_carousel = driver.find_element(By.CSS_SELECTOR, 'div.carousel-featured')
try: self.assertTrue(hero_carousel.is_displayed())
except AssertionError, e: self.verificationErrors.append("home_test: Hero Carousel was not visible")
def tearDown(self):
self.driver.close()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Right now it launches both Firefox and Chrome, but only runs the test in Firefox. Chrome opens and just sits on a blank page and doesn't close. So I figure there's something wrong with how I wrote the test. So what am I missing? I apologize if this is obvious but I'm just learning how to setup hub and just learned enough python to write selenium tests a couple weeks ago.
I think Hubs working as it launches both, but I did try adding a second node on the same machine on a different port and got the same thing. Just in case here's what hub prints out.
INFO - Got a request to create a new session: Capabilities [{browserName=chrome, javascriptEnabled=true, version=, platform=ANY}]
INFO - Available nodes: [http://192.168.2.1:5557]
INFO - Trying to create a new session on node http://192.168.2.1:5557
INFO - Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platform=MAC}
INFO - Got a request to create a new session: Capabilities [{browserName=firefox, javascriptEnabled=true, version=, platform=ANY}]
INFO - Available nodes: [http://192.168.2.1:5557]
INFO - Trying to create a new session on node http://192.168.2.1:5557
INFO - Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=firefox, maxInstances=5, platform=MAC}
Forgive me if I am way off as I haven't actually worked with selenium, this answer is purely based on the issue related to only keeping the reference to the last created driver in setUp
Instead of keeping one self.driver you need to have a list of all drivers, lets say self.drivers, then when dealing with them instead of driver = self.driver you would do for driver in self.drivers: and indent all the relevent code into the for loop, something like this:
class HeroCarousel(unittest.TestCase):
def setUp(self):
self.drivers = [] #could make this with list comprehension
for driver_instance in globes.desired_cap:
driver = webdriver.Remote(
command_executor=globes.selenium_server_url,
desired_capabilities=driver_instance)
self.drivers.append(driver)
self.verificationErrors = []
def test_hero_carousel(self):
for driver in self.drivers:
driver.get(globes.base_url)
hero_carousel = driver.find_element(By.CSS_SELECTOR, 'div.carousel-featured')
try: self.assertTrue(hero_carousel.is_displayed())
except AssertionError, e: self.verificationErrors.append("home_test: Hero Carousel was not visible")
def tearDown(self):
for driver in self.drivers:
driver.close()
self.assertEqual([], self.verificationErrors)
You need to use self.driver.quit() because otherwise the browser will not quit and will only close the current window.
You will soon end-up with multiple browser running, and you will have to pay for them.

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