Best Python code snippet using robotframework-anywherelibrary
S2LExtention4Ghostdriver.py
Source:S2LExtention4Ghostdriver.py
...21 lastPopupMessage = message;22 return;23 }24 """25 s2l.execute_javascript('%s' % js)26 def choose_ok_on_next_confirmation(self):27 """模æconfirmationçªå£ç¹å»okçæ
åµï¼åæ¶è®°å½çªå£ä¸çmessageã28 å½é¡µé¢åå¨window.confirm()æ¶ï¼å¿
é¡»å¨confirmationçªå£åºç°å使ç¨è¯¥å
³é®åã29 æ¯æ¬¡ææ°é¡µé¢å è½½ä¹åå¿
é¡»éæ°è°ç¨è¿ä¸ªå
³é®åæä¼çæã30 """31 s2l = BuiltIn().get_library_instance('Selenium2Library')32 js = """33 window.confirm = function(message) {34 lastPopupMessage = message;35 return true;36 }37 """38 s2l.execute_javascript('%s' % js)39 def choose_cancel_on_next_confirmation(self):40 """模æconfirmationçªå£ç¹å»cancelçæ
åµï¼åæ¶è®°å½çªå£ä¸çmessageã41 å½é¡µé¢åå¨window.confirm()æ¶ï¼å¿
é¡»å¨confirmationçªå£åºç°å使ç¨è¯¥å
³é®åã42 æ¯æ¬¡ææ°é¡µé¢å è½½ä¹åå¿
é¡»éæ°è°ç¨è¿ä¸ªå
³é®åæä¼çæã 43 """44 s2l = BuiltIn().get_library_instance('Selenium2Library')45 js = """46 window.confirm = function(message) {47 lastPopupMessage = message;48 return false;49 }50 """51 s2l.execute_javascript('%s' % js)52 def input_value_on_next_prompt(self, value=None):53 """模æpromptçªå£è¾å
¥valueåç¹å»okçæ
åµï¼åæ¶è®°å½çªå£ä¸çmessageã54 ä¸è¾å
¥valueåæ°æ¶ï¼å模æpromptçªå£å¨é»è®¤å¼æ
åµä¸è¢«ç¹å»okçæ
åµã55 å½é¡µé¢åå¨window.prompt()æ¶ï¼å¿
é¡»å¨promptçªå£åºç°å使ç¨è¯¥å
³é®åã56 æ¯æ¬¡ææ°é¡µé¢å è½½ä¹åå¿
é¡»éæ°è°ç¨è¿ä¸ªå
³é®åæä¼çæã57 """58 s2l = BuiltIn().get_library_instance('Selenium2Library')59 if value is None:60 js = """61 window.prompt = function(message, defaultValue) {62 lastPopupMessage = message;63 return defaultValue;64 }65 """66 s2l.execute_javascript('%s' % js)67 else:68 js_prefix = """69 window.prompt = function(message, defaultValue) {70 lastPopupMessage = message;71 return '"""72 js_suffix = "';}"73 s2l.execute_javascript('%s%s%s' % (js_prefix, value, js_suffix))74 def choose_cancel_on_next_prompt(self):75 """模æpromptçªå£ç¹å»cancelçæ
åµï¼åæ¶è®°å½çªå£ä¸çmessageã76 å½é¡µé¢åå¨window.prompt()æ¶ï¼å¿
é¡»å¨promptçªå£åºç°å使ç¨è¯¥å
³é®åã77 æ¯æ¬¡ææ°é¡µé¢å è½½ä¹åå¿
é¡»éæ°è°ç¨è¿ä¸ªå
³é®åæä¼çæã 78 """79 s2l = BuiltIn().get_library_instance('Selenium2Library')80 js = """81 window.prompt = function(message, defaultValue) {82 lastPopupMessage = message;83 return null;84 }85 """86 s2l.execute_javascript('%s' % js)87 def get_last_popup_message(self):88 """è·åæåä¸æ¬¡å¼¹åºæ¡(alert/confirmation/prompt)ä¸æ¾ç¤ºçæåã89 å½é¡µé¢åå¨window.alert()æè
window.confirm()æ¶ï¼å¨alert/confirmation/promptçªå£åºç°å使ç¨è¯¥å
³é®åã90 åªæå¨alert/confirmation/promptçªå£åºç°ä¹åè°ç¨è¿è¿ä¸ªåºç`Close On Next Alert`,91 `Choose Ok On Next Confirmation`, `Choose Cancel On Next Confirmation`, 92 `Input Value On Next Prompt`, `Choose Cancel On Next Prompt`è¿äºå
³é®åä¹åï¼è¿ä¸ªå
³é®åæè½èµ·ä½ç¨ã93 """94 s2l = BuiltIn().get_library_instance('Selenium2Library')95 js = """96 return lastPopupMessage;97 """...
power.py
Source:power.py
1# Copyright (c) 2013 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4import time5import numpy6from telemetry import test7from telemetry.page import page_measurement8from telemetry.core.platform.profiler.monsoon_profiler import MonsoonProfiler9def get_time():10 return "%s.%s" % (time.strftime("%H:%M:%S"), repr(time.time()).split('.')[1])11class MonsoonMeasurement(page_measurement.PageMeasurement):12 def __init__(self, *args, **kwargs):13 super(MonsoonMeasurement, self).__init__(*args, **kwargs)14 self._monsoon_profiler = None15 @property16 def results_are_the_same_on_every_page(self):17 return False18 def CustomizeBrowserOptions(self, options):19 options.no_power_monitor = True20 def _ExecuteStep(self, step, tab):21 if step["action"] == "wait":22 time.sleep(step["seconds"])23 elif step["action"] == "wait_for_javascript":24 tab.WaitForJavaScriptExpression(25 step["expression"], step.get("timeout", 60))26 elif step["action"] == "execute_javascript":27 tab.ExecuteJavaScript(step["expression"])28 def MeasurePage(self, page, tab, results):29 power_consumptions = []30 for i in range(self.repeats):31 print "Measuring power, repeat %d/%d, at %s" \32 % (i+1, self.repeats, get_time())33 self._monsoon_profiler = MonsoonProfiler(None, None, "/dev/null", None)34 for step in self.steps:35 self._ExecuteStep(step, tab)36 print "Steps measured at", get_time()37 self._monsoon_profiler.CollectProfile()38 power_consumption = self.CalculatePower(self._monsoon_profiler.results)39 power_consumptions.append(power_consumption)40 results.Add('Power consumption', 'J', power_consumptions)41 @staticmethod42 def CalculatePower(results):43 x_timestamps, currents, voltages = zip(*results)44 y_powers = [a*v for a, v in zip(currents, voltages)]45 return numpy.trapz(y_powers, x_timestamps)46 @staticmethod47 def WithSteps(name, steps, repeats):48 """Factory method for creating preconfigured multipage measurement with49 given name, repeat count and steps"""50 members = {"steps": steps, "repeats": repeats}51 return type(name, (MonsoonMeasurement,), members)52 @staticmethod53 def WithSinglePageLoadSteps(name, url, wait_after_load_event=2, repeats=20):54 steps = [55 {"action": "execute_javascript",56 "expression": "window.location.href=\"about:blank\""},57 {"action": "execute_javascript",58 "expression": "window.location.href=\"%s\"" % url},59 {"action": "wait_for_javascript",60 "expression": "performance.timing.loadEventStart", "timeout": 60*5},61 {"action": "wait", "seconds": wait_after_load_event},62 ]63 return MonsoonMeasurement.WithSteps(name, steps, repeats)64class EspnLoadPowerTest(test.Test):65 test = MonsoonMeasurement.WithSinglePageLoadSteps("loading_espn",66 url="http://m.espn.go.com")67 page_set = 'page_sets/samsung_loading_espn.json'68class WikipediaLoadPowerTest(test.Test):69 test = MonsoonMeasurement.WithSinglePageLoadSteps("loading_wikipedia",70 url="http://www.wikipedia.com")71 page_set = 'page_sets/samsung_loading_wikipedia.json'72class YoutubeLoadPowerTest(test.Test):73 test = MonsoonMeasurement.WithSinglePageLoadSteps("loading_youtube",74 url="http://m.youtube.com")75 page_set = 'page_sets/samsung_loading_youtube.json'76class OneMinutePowerTest(test.Test):77 steps = [78 {"action": "execute_javascript",79 "expression": "window.location.href=\"http://m.youtube.com\""},80 {"action": "wait", "seconds": 15 },81 {"action": "execute_javascript",82 "expression": "window.location.href=\"http://www.google.com\""},83 {"action": "wait", "seconds": 5 },84 {"action": "execute_javascript",85 "expression": "window.location.href=\"http://m.espn.go.com\""},86 {"action": "wait", "seconds": 15 },87 {"action": "execute_javascript",88 "expression": "window.location.href=\"http://www.google.com\""},89 {"action": "wait", "seconds": 5 },90 {"action": "execute_javascript",91 "expression": "window.location.href=\"http://www.wikipedia.com\""},92 {"action": "wait", "seconds": 14 },93 ]94 test = MonsoonMeasurement.WithSteps("1min", steps, repeats=5)...
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!!