Best Python code snippet using playwright-python
AttachTest.py
Source: AttachTest.py
...29 the expected callback).30 """31 dut = self.android_devices[0]32 dut.droid.wifiAwareAttach(False)33 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)34 autils.fail_on_event(dut, aconsts.EVENT_CB_ON_IDENTITY_CHANGED)35 @test_tracker_info(uuid="82f2a8bc-a62b-49c2-ac8a-fe8460010ba2")36 def test_attach_with_identity(self):37 """Functional test case / Attach test cases / attach with identity callback38 Validates that attaching to the Wi-Fi Aware service works (receive39 the expected callbacks).40 """41 dut = self.android_devices[0]42 dut.droid.wifiAwareAttach(True)43 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)44 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_IDENTITY_CHANGED)45 @test_tracker_info(uuid="d2714d14-f330-47d4-b8e9-ee4d5e5b7ea0")46 def test_attach_multiple_sessions(self):47 """Functional test case / Attach test cases / multiple attach sessions48 Validates that when creating multiple attach sessions each can be49 configured independently as to whether or not to receive an identity50 callback.51 """52 dut = self.android_devices[0]53 # Create 3 attach sessions: 2 without identity callback, 1 with54 id1 = dut.droid.wifiAwareAttach(False, None, True)55 time.sleep(10) # to make sure all calls and callbacks are done56 id2 = dut.droid.wifiAwareAttach(True, None, True)57 time.sleep(10) # to make sure all calls and callbacks are done58 id3 = dut.droid.wifiAwareAttach(False, None, True)59 dut.log.info('id1=%d, id2=%d, id3=%d', id1, id2, id3)60 # Attach session 1: wait for attach, should not get identity61 autils.wait_for_event(62 dut, autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, id1))63 autils.fail_on_event(64 dut,65 autils.decorate_event(aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id1))66 # Attach session 2: wait for attach and for identity callback67 autils.wait_for_event(68 dut, autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, id2))69 autils.wait_for_event(70 dut,71 autils.decorate_event(aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id2))72 # Attach session 3: wait for attach, should not get identity73 autils.wait_for_event(74 dut, autils.decorate_event(aconsts.EVENT_CB_ON_ATTACHED, id3))75 autils.fail_on_event(76 dut,77 autils.decorate_event(aconsts.EVENT_CB_ON_IDENTITY_CHANGED, id3))78 @test_tracker_info(uuid="b8ea4d02-ae23-42a7-a85e-def52932c858")79 def test_attach_with_no_wifi(self):80 """Function test case / Attach test cases / attempt to attach with wifi off81 Validates that if trying to attach with Wi-Fi disabled will receive the82 expected failure callback. As a side-effect also validates that the83 broadcast for Aware unavailable is received.84 """85 dut = self.android_devices[0]86 wutils.wifi_toggle_state(dut, False)87 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)88 dut.droid.wifiAwareAttach()89 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACH_FAILED)90 @test_tracker_info(uuid="7dcc4530-c936-4447-9d22-a7c5b315e2ce")91 def test_attach_with_doze(self):92 """Function test case / Attach test cases / attempt to attach with doze on93 Validates that if trying to attach with device in doze mode will receive the94 expected failure callback. As a side-effect also validates that the95 broadcast for Aware unavailable is received.96 """97 dut = self.android_devices[0]98 asserts.assert_true(utils.enable_doze(dut), "Can't enable doze")99 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)100 dut.droid.wifiAwareAttach()101 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACH_FAILED)102 asserts.assert_true(utils.disable_doze(dut), "Can't disable doze")103 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)104 @test_tracker_info(uuid="2574fd01-8974-4dd0-aeb8-a7194461140e")105 def test_attach_with_location_off(self):106 """Function test case / Attach test cases / attempt to attach with location107 mode off.108 Validates that if trying to attach with device location mode off will109 receive the expected failure callback. As a side-effect also validates that110 the broadcast for Aware unavailable is received.111 """112 dut = self.android_devices[0]113 utils.set_location_service(dut, False)114 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)115 dut.droid.wifiAwareAttach()116 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACH_FAILED)117 utils.set_location_service(dut, True)118 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)119 @test_tracker_info(uuid="7ffde8e7-a010-4b77-97f5-959f263b5249")120 def test_attach_apm_toggle_attach_again(self):121 """Validates that enabling Airplane mode while Aware is on resets it122 correctly, and allows it to be re-enabled when Airplane mode is then123 disabled."""124 dut = self.android_devices[0]125 # enable Aware (attach)126 dut.droid.wifiAwareAttach()127 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED)128 # enable airplane mode129 utils.force_airplane_mode(dut, True)130 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_NOT_AVAILABLE)131 # wait a few seconds and disable airplane mode132 time.sleep(10)133 utils.force_airplane_mode(dut, False)134 autils.wait_for_event(dut, aconsts.BROADCAST_WIFI_AWARE_AVAILABLE)135 # try enabling Aware again (attach)136 dut.droid.wifiAwareAttach()...
ble_characteristic.py
Source: ble_characteristic.py
...62 elif event == _IRQ_GATTS_INDICATE_DONE:63 print("_IRQ_GATTS_INDICATE_DONE", data[-1])64 if event not in waiting_events:65 waiting_events[event] = None66def wait_for_event(event, timeout_ms):67 t0 = time.ticks_ms()68 while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms:69 if event in waiting_events:70 return waiting_events.pop(event)71 machine.idle()72 raise ValueError("Timeout waiting for {}".format(event))73# Acting in peripheral role.74def instance0():75 multitest.globals(BDADDR=ble.config("mac"))76 ((char_handle,),) = ble.gatts_register_services(SERVICES)77 print("gap_advertise")78 ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY")79 multitest.next()80 try:81 # Write initial characteristic value.82 ble.gatts_write(char_handle, "periph0")83 # Wait for central to connect to us.84 conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS)85 # A86 # Wait for a write to the characteristic from the central,87 # then reply with a notification.88 wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS)89 print("gatts_write")90 ble.gatts_write(char_handle, "periph1")91 print("gatts_notify")92 ble.gatts_notify(conn_handle, char_handle)93 # B94 # Wait for a write to the characteristic from the central,95 # then reply with value-included notification.96 wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS)97 print("gatts_notify")98 ble.gatts_notify(conn_handle, char_handle, "periph2")99 # C100 # Wait for a write to the characteristic from the central,101 # then reply with an indication.102 wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS)103 print("gatts_write")104 ble.gatts_write(char_handle, "periph3")105 print("gatts_indicate")106 ble.gatts_indicate(conn_handle, char_handle)107 wait_for_event(_IRQ_GATTS_INDICATE_DONE, TIMEOUT_MS)108 # Wait for the central to disconnect.109 wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS)110 finally:111 ble.active(0)112# Acting in central role.113def instance1():114 multitest.next()115 try:116 # Connect to peripheral and then disconnect.117 print("gap_connect")118 ble.gap_connect(*BDADDR)119 conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS)120 # Discover characteristics.121 ble.gattc_discover_characteristics(conn_handle, 1, 65535)122 value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS)123 wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS)124 # Issue read of characteristic, should get initial value.125 print("gattc_read")126 ble.gattc_read(conn_handle, value_handle)127 wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)128 # Write to the characteristic, which will trigger a notification.129 print("gattc_write")130 ble.gattc_write(conn_handle, value_handle, "central0", 1)131 wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS)132 # A133 wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS)134 print("gattc_read") # Read the new value set immediately before notification.135 ble.gattc_read(conn_handle, value_handle)136 wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)137 # Write to the characteristic, which will trigger a value-included notification.138 print("gattc_write")139 ble.gattc_write(conn_handle, value_handle, "central1", 1)140 wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS)141 # B142 wait_for_event(_IRQ_GATTC_NOTIFY, TIMEOUT_MS)143 print("gattc_read") # Read value should be unchanged.144 ble.gattc_read(conn_handle, value_handle)145 wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)146 # Write to the characteristic, which will trigger an indication.147 print("gattc_write")148 ble.gattc_write(conn_handle, value_handle, "central2", 1)149 wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS)150 # C151 wait_for_event(_IRQ_GATTC_INDICATE, TIMEOUT_MS)152 print("gattc_read") # Read the new value set immediately before indication.153 ble.gattc_read(conn_handle, value_handle)154 wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)155 # Disconnect from peripheral.156 print("gap_disconnect:", ble.gap_disconnect(conn_handle))157 wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS)158 finally:159 ble.active(0)160ble = bluetooth.BLE()161ble.active(1)...
stress_log_filesystem.py
Source: stress_log_filesystem.py
...84 elif event == _IRQ_GATTS_READ_REQUEST:85 print("_IRQ_GATTS_READ_REQUEST")86 if event not in waiting_events:87 waiting_events[event] = None88def wait_for_event(event, timeout_ms):89 t0 = time.ticks_ms()90 while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms:91 periodic_log_write()92 if event in waiting_events:93 return waiting_events.pop(event)94 machine.idle()95 raise ValueError("Timeout waiting for {}".format(event))96# Acting in peripheral role.97def instance0():98 global log_file99 log_file = open(LOG_PATH_INSTANCE0, "w")100 write_log("start")101 ble.active(1)102 ble.irq(irq)103 multitest.globals(BDADDR=ble.config("mac"))104 ((char_handle,),) = ble.gatts_register_services(SERVICES)105 multitest.next()106 try:107 for repeat in range(2):108 print("gap_advertise")109 ble.gap_advertise(50_000, b"\x02\x01\x06\x04\xffMPY")110 # Wait for central to connect, do a sequence of read/write, then disconnect.111 wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS)112 for op in range(4):113 wait_for_event(_IRQ_GATTS_READ_REQUEST, TIMEOUT_MS)114 wait_for_event(_IRQ_GATTS_WRITE, TIMEOUT_MS)115 wait_for_event(_IRQ_CENTRAL_DISCONNECT, 2 * TIMEOUT_MS)116 finally:117 ble.active(0)118 log_file.close()119 os.unlink(LOG_PATH_INSTANCE0)120# Acting in central role.121def instance1():122 global log_file123 log_file = open(LOG_PATH_INSTANCE1, "w")124 write_log("start")125 ble.active(1)126 ble.irq(irq)127 multitest.next()128 try:129 for repeat in range(2):130 # Connect to peripheral and then disconnect.131 print("gap_connect")132 ble.gap_connect(BDADDR[0], BDADDR[1], 5000)133 conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS)134 # Discover services.135 print("gattc_discover_services")136 ble.gattc_discover_services(conn_handle)137 start_handle, end_handle = wait_for_event(_IRQ_GATTC_SERVICE_RESULT, TIMEOUT_MS)138 wait_for_event(_IRQ_GATTC_SERVICE_DONE, TIMEOUT_MS)139 # Discover characteristics.140 print("gattc_discover_characteristics")141 ble.gattc_discover_characteristics(conn_handle, start_handle, end_handle)142 value_handle = wait_for_event(_IRQ_GATTC_CHARACTERISTIC_RESULT, TIMEOUT_MS)143 wait_for_event(_IRQ_GATTC_CHARACTERISTIC_DONE, TIMEOUT_MS)144 for op in range(4):145 print("gattc_read")146 ble.gattc_read(conn_handle, value_handle)147 wait_for_event(_IRQ_GATTC_READ_RESULT, TIMEOUT_MS)148 wait_for_event(_IRQ_GATTC_READ_DONE, TIMEOUT_MS)149 print("gattc_write")150 ble.gattc_write(conn_handle, value_handle, "{}".format(op), 1)151 wait_for_event(_IRQ_GATTC_WRITE_DONE, TIMEOUT_MS)152 # Disconnect.153 print("gap_disconnect:", ble.gap_disconnect(conn_handle))154 wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, 2 * TIMEOUT_MS)155 finally:156 ble.active(0)157 log_file.close()158 os.unlink(LOG_PATH_INSTANCE1)...
Playwright auto-scroll to bottom of infinite-scroll page
How to quickly find out if an element exists in a page or not using playwright
Playwright on google colab : Attempt to free invalid pointer 0x29000020c5a0
How to download PDF files with Playwright? (Python)
Playwright: click on element within one/multiple elements using Python
Scraping Google images with Python3 (requests + BeautifulSoup)
How to quickly find out if an element exists in a page or not using playwright
how to get element from a table, to be selected in playwright python
Using Playwright for Python, how do I select an option from a drop down list?
Can't import name 'sync_playwright' from 'playwright.sync_api' on MacOS and ubutunOS
So I found a working solution.
What I did was to combine Javascript with python Playwright code.
I start the setInterval with a timer of 200ms to scroll down on the page with page.evaluate()
and then I follow it up with a python loop that checks every second whether the total height of the page (scroll included) has changed. If it changes it continues to scroll and if it hasn't changed than the scroll is over.
This is what it looks like:
page.evaluate(
"""
var intervalID = setInterval(function () {
var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
}, 200);
"""
)
prev_height = None
while True:
curr_height = page.evaluate('(window.innerHeight + window.scrollY)')
if not prev_height:
prev_height = curr_height
time.sleep(1)
elif prev_height == curr_height:
page.evaluate('clearInterval(intervalID)')
break
else:
prev_height = curr_height
time.sleep(1)
See the below answer using the new mouse.wheel(x, y) feature for an up to date way to scroll using playwright. Combine my answer with his to lessen the need to use JS.
Check out the latest blogs from LambdaTest on this topic:
Ruby is a programming language which is well suitable for web automation. Ruby makes an excellent choice because of its clean syntax, focus on built-in library integrations, and an active community. Another benefit of Ruby is that it also allows other programming languages like Java, Python, etc. to be used in order to automate applications written in any other frameworks. Therefore you can use Selenium Ruby to automate any sort of application in your system and test the results in any type of testing environment
Are you looking to get started with DevOps or willing to shift gears in your professional career by adding DevOps as a skill? If your answer is yes, you have arrived at the right place!
A productive workspace is crucial in crafting code rather than just finding the right IDE. After several generations of IDEs and code editors, Visual Studio Code is considered one of the best web development IDEs used by developers.
The web development industry is growing, and many Best Automated UI Testing Tools are available to test your web-based project to ensure it is bug-free and easily accessible for every user. These tools help you test your web project and make it fully compatible with user-end requirements and needs.
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!