Best Python code snippet using Selenium-Requests_python
test_window_handling.py
Source: test_window_handling.py
1from helium import write, click, switch_to, TextField, Text, get_driver, \2 Link, wait_until3from tests.api import BrowserAT, test_browser_name4from unittest import skipIf5class WindowHandlingTest(BrowserAT):6 def get_page(self):7 return 'test_window_handling/main.html'8 def test_write_writes_in_active_window(self):9 write("Main window")10 self.assertEqual("Main window", self._get_value('mainTextField'))11 self._open_popup()12 write("Popup")13 self.assertEqual("Popup", self._get_value('popupTextField'))14 def test_write_searches_in_active_window(self):15 write("Main window", into="Text field")16 self.assertEqual("Main window", self._get_value('mainTextField'))17 self._open_popup()18 write("Popup", into="Text field")19 self.assertEqual("Popup", self._get_value('popupTextField'))20 def test_switch_to_search_text_field(self):21 write("Main window", into="Text field")22 self.assertEqual("Main window", TextField("Text field").value)23 self._open_popup()24 write("Popup", into="Text field")25 self.assertEqual("Popup", TextField("Text field").value)26 switch_to("test_window_handling - Main")27 self.assertEqual("Main window", TextField("Text field").value)28 def test_handles_closed_window_gracefully(self):29 self._open_popup()30 get_driver().close()31 is_back_in_main_window = Link("Open popup").exists()32 self.assertTrue(is_back_in_main_window)33 def test_switch_to_after_window_closed(self):34 self._open_popup()35 get_driver().close()36 switch_to('test_window_handling - Main')37 def setUp(self):38 super().setUp()39 self.main_window_handle = self.driver.current_window_handle40 def tearDown(self):41 for window_handle in self.driver.window_handles:42 if window_handle != self.main_window_handle:43 self.driver.switch_to.window(window_handle)44 self.driver.close()45 self.driver.switch_to.window(self.main_window_handle)46 super().tearDown()47 def _get_value(self, element_id):48 return self.driver.find_element_by_id(element_id).get_attribute('value')49 def _open_popup(self):50 click("Open popup")51 wait_until(self._is_in_popup)52 def _is_in_popup(self):53 return get_driver().title == 'test_window_handling - Popup'54class WindowHandlingOnStartBrowserTest(BrowserAT):55 def get_page(self):56 return 'test_window_handling/main_immediate_popup.html'57 @skipIf(test_browser_name() == 'firefox', 'This test fails on Firefox')58 def test_switches_to_popup(self):...
Check out the latest blogs from LambdaTest on this topic:
Being an open-source framework allowed Selenium to be compatible with multiple test automation frameworks for different programming languages and if we talk about Automation testing with Selenium and JavaScript, there is a particular framework that never fails to take the spotlight and that is the Nightwatch.js. This is why I decided to come up with Nightwatch.js tutorial for beginners.
There are cases where test scenarios may fail unexpectedly with Selenium commands (e.g., click operation on the button web element does not result in a click even though the button is enabled). Such issues are more pertinent when creating an XHR request or when attempting to access another frame. For overcoming such issues, you can execute JavaScript in Selenium through the JavaScriptExecutor interface.
Do you know the test automation market is all set to hit $35 billion by 2026? And when it comes to cross browser testing, JavaScript leads from the front? Javascript is probably the best alternative for Selenium automation, considering its protocol transformation to the W3C standard. In order to make the most of it, the first step is to choose the best test automation frameworks. Among all the JavaScript testing frameworks, two frameworks are most popular- Nightwatch and Protractor.
Selenium, a project hosted by the Apache Software Foundation, is an umbrella open-source project comprising a variety of tools and libraries for test automation. Selenium automation framework enables QA engineers to perform automated web application testing using popular programming languages like Python, Java, JavaScript, C#, Ruby, and PHP.
CI/CD pipelines are here to stay and contribute tremendously to continuous integration and delivery across all global projects. This article will be a guide to configure, set up builds and tests with “GitHub Actions”, primarily using Selenium WebDriver. This article shall also cover some of the most generic GitHub Actions examples, and user flows.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!