Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juliandescottes/80732e3ad385e43763f552c3da385819 to your computer and use it in GitHub Desktop.
Save juliandescottes/80732e3ad385e43763f552c3da385819 to your computer and use it in GitHub Desktop.
test
# coding=UTF-8
import base64
import contextlib
import os
import pdb
import struct
import time
import urllib
from datetime import datetime
from urllib.parse import quote
from marionette_driver import By, errors, expected, Wait
from marionette_driver.addons import Addons
from marionette_driver.keys import Keys
from marionette_driver.marionette import ActionSequence, Alert
from marionette_harness import (
MarionetteTestCase,
WindowManagerMixin,
skip,
)
def iframe(doc):
return "<iframe src='%s'></iframe>" % inline(doc)
def inline(doc):
return "data:text/html;charset=utf-8,{}".format(quote(doc))
class TestMinimizedTestCase(WindowManagerMixin, MarionetteTestCase):
# class TestMinimizedTestCase(MarionetteTestCase):
insecure_tls = {"acceptInsecureCerts": True}
secure_tls = {"acceptInsecureCerts": False}
def setUp(self):
super(TestMinimizedTestCase, self).setUp()
# self.marionette.timeout.implicit = 1
self.local_testcase = "file:///Users/henrik/code/gecko/_a/testcase.html"
self.page_slow_resource = self.marionette.absolute_url("slow_resource.html")
self.marionette.delete_session()
self.marionette.start_session({
"acceptInsecureCerts": True,
# "moz:webdriverClick": False,
# "pageLoadStrategy": "none",
"webSocketUrl": True,
# "moz:windowless": True,
})
def tearDown(self):
if (hasattr(self, "close_all_tabs")):
self.close_all_tabs()
super(TestMinimizedTestCase, self).tearDown()
@contextlib.contextmanager
def safe_session(self):
try:
self.capabilities = self.marionette.start_session(self.secure_tls)
self.assertFalse(self.capabilities["acceptInsecureCerts"])
yield self.marionette
finally:
self.marionette.delete_session()
@contextlib.contextmanager
def unsafe_session(self):
try:
self.capabilities = self.marionette.start_session(self.insecure_tls)
self.assertTrue(self.capabilities["acceptInsecureCerts"])
yield self.marionette
finally:
self.marionette.delete_session()
@property
def alert_present(self):
try:
Alert(self.marionette).text
return True
except errors.NoAlertPresentException:
return False
def wait_for_alert(self, timeout=None):
Wait(self.marionette, timeout=timeout).until(lambda _: self.alert_present)
def crash(self, parent=True):
process = 'parent' if parent else 'content'
self.marionette.set_context("content")
self.marionette.navigate("about:crash{}".format(process))
@property
def active_element(self):
return self.marionette.execute_script("return document.activeElement")
def install_perfchaser(self):
self.marionette.set_pref("extensions.experiments.enabled", True)
addons = Addons(self.marionette)
addons.install("/Users/henrik/code/perfchaser/perfchaser.xpi", temp=True)
def test_case(self):
# self.marionette.navigate("https://example.com/")
driver1 = self.marionette
driver1.navigate("https://www.linkedin.com/")
for i in range(0, 50):
def context_menu_state():
with driver1.using_context("chrome"):
cm_el = driver1.find_element(By.ID, "contentAreaContextMenu")
return cm_el.get_property("state")
# time.sleep(2000)
touchHandler = ActionSequence(driver1, "pointer", "dev_mouse0", {"pointerType": "mouse"})
touchHandler.pointer_move(500, 248).pointer_down(button=2).pause(100).pointer_up(button=2).perform()
Wait(driver1).until(
lambda _: context_menu_state() == "open",
message="Context menu did not open",
)
time.sleep(0.1)
with driver1.using_context("chrome"):
cm_el = driver1.find_element(By.ID, "contentAreaContextMenu")
touchHandler.pointer_move(0, 20, origin=cm_el).pointer_down(0).pause(100).pointer_up(0).perform()
time.sleep(0.1)
with driver1.using_context("chrome"):
cm_el = driver1.find_element(By.ID, "contentAreaContextMenu")
driver1.execute_script(
"arguments[0].hidePopup()", script_args=(cm_el,)
)
Wait(driver1).until(
lambda _: context_menu_state() == "closed",
message="Context menu did not close",
)
time.sleep(0.1)
# touchHandler.pointer_move(0, 0).pointer_down().pointer_up().perform()
# time.sleep(2)
driver1.actions.release()
def tst_shadow_dom(self):
page = inline("""
<style>
custom-checkbox-element {
display:block; width:20px; height:20px;
}
</style>
<custom-checkbox-element id="checkbox"></custom-checkbox-element>
<script>
customElements.define('custom-checkbox-element',
class extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'}).innerHTML = `
<div><input type="checkbox"/></div>
`;
}
});
</script>""")
self.marionette.navigate(page)
self.marionette.execute_async_script("""
const resolve = arguments[0];
resolve(document.querySelector('custom-checkbox-element').shadowRoot);
""", sandbox=None)
# self.marionette.execute_script("""
# return document.querySelector('custom-checkbox-element').shadowRoot;
# """, sandbox=None)
def tst_focus_xul_dialog(self):
self.marionette.set_context("chrome")
while True:
dialog = self.open_chrome_window("chrome://marionette/content/test_dialog.xul")
self.marionette.switch_to_window(dialog)
self.marionette.chrome_window_handles
time.sleep(1)
textbox = self.marionette.find_element(By.ID, "text-box")
textbox.send_keys("")
self.assertEqual(self.active_element.get_property("localName"),
textbox.get_property("localName"))
#screenshot_focus = self.marionette.screenshot()
# self.marionette.execute_script("arguments[0].blur();", script_args=[textbox])
# screenshot_no_focus = self.marionette.screenshot()
time.sleep(1)
self.marionette.close_chrome_window()
self.marionette.switch_to_window(self.start_window)
time.sleep(1)
return
def tst_hang_after_switching_frames(self):
self.marionette.timeout.page_load = 5
frame_src = inline("<a href='{}'>click</a>".format(inline("<p>foo")))
self.marionette.navigate(inline("""<iframe src="{}">""".format(frame_src)))
frame = self.marionette.find_element(By.TAG_NAME, "iframe")
self.marionette.switch_to_frame(frame)
link = self.marionette.find_element(By.TAG_NAME, "a")
link.click()
self.marionette.switch_to_parent_frame()
# self.marionette.switch_to_frame(frame)
self.marionette.go_back()
def tst_open_window(self):
pass
# result = self.marionette.open(type="tab", focus=False)
# self.assertIn(result["handle"], self.marionette.window_handles)
# self.marionette.switch_to_window(result["handle"])
# self.marionette.navigate("about:about")
# result = self.marionette.open(type="window", focus=False)
# self.assertIn(result["handle"], self.marionette.window_handles)
# self.marionette.switch_to_window(result["handle"])
# self.marionette.navigate("about:about")
def tst_restart(self):
self.marionette.restart(in_app=True)
print("* waitpid (%s): %s" % (
self.marionette.process_id,
self.marionette.instance.runner.process_handler.proc._wait()))
self.marionette.restart(in_app=True)
self.marionette.quit()
def tst_http_auth(self):
self.marionette.navigate("https://jigsaw.w3.org/HTTP/Basic/")
alert = self.marionette.switch_to_alert()
alert.send_keys("guest\tguest")
# alert.send_keys("user" + Keys.TAB + "password")
time.sleep(3)
alert.accept()
def tst_typing(self):
self.marionette.navigate("https://jsbin.com/pilazapiko/1")
elem = self.marionette.find_element(By.TAG_NAME, "input")
elem.send_keys("f" + Keys.SHIFT + "bb" + Keys.SHIFT + "cc" + Keys.NULL + u"桜")
pdb.set_trace()
def tst_typing_incorrect_select_event(self):
self.marionette.navigate(inline("""<input onselect="alert('select event')">"""))
elem = self.marionette.find_element(By.TAG_NAME, "input")
elem.send_keys("f")
time.sleep(1)
def tst_crash(self):
with self.assertRaisesRegexp(IOError, "Process crashed"):
self.crash()
self.assertIsNotNone(self.marionette.instance.runner.wait(0))
self.marionette.start_session()
def tst_delayed_page_load(self):
self.marionette.timeout.implicit = 10
self.marionette.navigate("http://demo.litecart.net/admin/")
self.marionette.find_element(By.CSS_SELECTOR, "button[name=login]").click()
logout = self.marionette.find_element(By.CSS_SELECTOR, "a[title=Logout]")
logout.click()
def tst_screenshot(self):
self.marionette.navigate("https://www.blueorigin.com/")
# with self.marionette.using_context("chrome"):
screenshot = self.marionette.screenshot(full=False)
if not isinstance(screenshot, bytes):
screenshot = bytes(screenshot, encoding='utf-8')
image = base64.b64decode(screenshot)
width, height = struct.unpack(">LL", image[16:24])
print("Screenshot: {}x{}".format(width, height))
with open("screenshot.png", "wb") as file:
file.write(image)
def tst_close_window(self):
self.marionette.navigate(self.marionette.absolute_url("windowHandles.html"))
self.marionette.find_element(By.ID, "new-tab").click()
self.marionette.close()
self.marionette.close()
return
self.marionette.switch_to_window(self.marionette.window_handles[0])
self.marionette.navigate(self.marionette.absolute_url("clicks.html"))
time.sleep(1)
self.marionette.find_element(By.ID, "close-window").click()
# self.marionette.close()
print("*** clicking now")
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment