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()
I am trying to familiarize myself with Selenium and am running into an issue finding an element, I am noticing this a lot actually.
I want to go to yahoo.com, click on Sports, and assert the page title is correct. The following throws an "Unable to locate element" error message. I have tried ID, xPath etc. I have also tried other page elements, Mail, News etc...all throw the same error. Am I missing something here?
import unittest
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# go to Yahoo home page, click on Sports and assert title is correct
class searchMaps(unittest.TestCase):
# def setup as class method
#classmethod
def setUpClass(inst):
inst.driver = webdriver.Chrome()
inst.driver.maximize_window()
inst.driver.get("http://www.yahoo.com")
inst.driver.implicitly_wait(20)
def test_click_sports_page(self):
self.search_field = self.driver.find_element_by_id('yui_3_18_0_3_1527260025921_1028')
self.search_field.click()
actual_title = driver.getTitle()
expected_title = 'Yahoo Sports | Sports News'
assertEquals(actual_title, expected_title)
#classmethod
def tearDownClass(inst):
inst.driver.quit()
if __name__ == '__main__':
unittest.main()
The #id of target link is dynamic, so it will be different each time you open the page.
Try to use link text to locate element:
self.search_field = self.driver.find_element_by_link_text('Sports')
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.
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.
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()